本章节以 Mac OS X下 Hosts 文件管理 App 为例。
技术栈包含:
需要注意Webpack
2.x.x 版本与 1.x.x 版本发生了不少改动。可以参考迁移文档: https://webpack.js.org/guides/migrating/ 进行学习。
该插件 NPM 最新版本为1.0.1,不支持 Webpack2,所以需要通过安装 RC3 版本来获得对应支持。
yarn add --dev extract-text-webpack-plugin@2.0.0-rc3
或
npm i --save-dev extract-text-webpack-plugin@2.0.0-rc3
会导致 extract-text 插件报错崩溃。
解决方法,修改 .babelrc
:
{
"presets": [
"latest"
],
"plugins": [
[
"transform-runtime",
{
"helpers": false,
"polyfill": false,
"regenerator": true
}
]
],
"compact": true
}
其中 设置compact
属性还能阻止 500kb 限制的警告。
目前无解,去掉了postcss-loader
的使用。
下一阶段尝试。
首先,需要在 webpack config 中设置:
target: 'electron'
向 Electron 注册插件:
import Vue from 'vue';
import electron from 'electron';
Vue.use({
install: (vue) => {
vue.prototype.$electron = electron;
}
});
前端向 IPC 发送消息:
this.$electron.ipcRenderer.send('resizePreferencesWindow', { width: 400, height: 300 });
使用插件:https://github.com/Teamwork/node-auto-launch
import AutoLaunch from 'auto-launch';
const hostsAutoLauncher = new AutoLaunch({
name: 'Hosts.js',
path: '/Applications/Hosts.js.app'
});
exports.isEnabled = async () => {
const result = await hostsAutoLauncher.isEnabled();
return result;
};
exports.enable = () => {
hostsAutoLauncher.enable();
};
exports.disable = () => {
hostsAutoLauncher.disable();
};
可以直接通过插件实现,地址:https://github.com/MarshallOfSound/electron-devtools-installer
devtools.js:
import installExtension, { VUEJS_DEVTOOLS } from 'electron-devtools-installer';
module.exports = installExtension(VUEJS_DEVTOOLS);
窗口加载:
if (process.env.NODE_ENV === 'development') {
// 开发模式加载 devtools
const devtools = require('../lib/devtools');
devtools.then(() => { // 注意调用方式
this.preferencesWindow.loadURL('http://localhost:3000/build/app.html');
});
} else {
this.preferencesWindow.loadURL(`file://${path.join(__dirname, '../../build/app.html')}`);
}
特点:
[
{
name: 'Default',
order: 1,
id: 'xxx',
type: 'item',
enabled: true
},
{
namename: 'Folder',
order: 2,
id: 'xxx',
type: 'folder',
children: [
{
name: 'Sub Item 1',
order: 1,
id: 'xxx',
type: 'item',
enabled: false
},
{
name: 'Sub Item 2',
order: 2,
id: 'xxx',
type: 'item',
enabled: true
},
]
}
]
排序算法:
const reorder = arr => arr.sort(x => x.order).map(x => {
if (x.type === 'folder') {
x.children = x.children.sort(y => y.order);
}
return x;
});
Hosts.js项目源码: https://github.com/js-cool/Hosts.js