接前面一篇文章,前一篇文章中只有一个页面,并且只有一个js文件,所以打包的时候会把那个js打包成一个bundle.js文件。但是假如我们有多个页面,且每个页面需要使用的js文件也不同,那么我们应该怎样打包呢。
这里准备做个小的演示程序,首先安装一些这个演示程序需要看的第三方库
$ npm install --save-dev electron
$ npm install --save-dev electron-packager
$ npm install --save-dev webpack
$ npm install --save-dev webpack-dev-server
$ npm install --save-dev webpack-target-electron-renderer
$ npm install --save-dev is-electron-renderer
$ npm install --save-dev css-loader
$ npm install --save-dev style-loader
$ npm install --save-dev extract-text-webpack-plugin
** 注:这里使用的webpack是2.5.1版本 **
这个演示程序包含的文件结构如下所示:
myapp
|-- main.js
|-- package.json
|-- src
| |-- home.html
| |-- home.js
| |-- about.html
| |-- about.js
| |-- contact.html
| |-- contact.js
| |-- user.js
| `-- style.css
`-- webpack.config.js
{
"name": "myapp",
"version": "1.0.0",
"description": "",
"main": "main.js",
"scripts": {
"electron": "webpack && electron .",
"packager": "webpack && electron-packager . --platform=linux --electron-version=1.6.6 --overwrite"
},
"author": "",
"license": "ISC",
"devDependencies": {
"css-loader": "^0.28.1",
"electron": "^1.6.7",
"electron-packager": "^8.7.0",
"extract-text-webpack-plugin": "^2.1.0",
"is-electron-renderer": "^2.0.1",
"style-loader": "^0.17.0",
"webpack": "^2.5.1",
"webpack-dev-server": "^2.4.5",
"webpack-target-electron-renderer": "^0.4.0"
}
}
/* eslint strict: 0 */
'use strict';
const path = require('path');
const webpack = require('webpack');
module.exports ={
target: 'electron-renderer',
entry: {
home: "./src/home.js",
about: "./src/about.js",
contact: "./src/contact.js"
},
output: {
path: path.join(__dirname, 'build'),
publicPath: path.join(__dirname, 'src'),
filename: '[name].bundle.js',
},
module: {
rules: [{
test: /\.css$/,
use: [ 'style-loader', 'css-loader' ]
}]
}
};
Electron App 的入口 js 文件,启动一个窗口并加载 src/index.html 文件。
/* eslint strict: 0 */
'use strict';
const electron = require('electron');
const app = electron.app;
const BrowserWindow = electron.BrowserWindow;
let mainWindow = null;
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') app.quit();
});
app.on('ready', () => {
mainWindow = new BrowserWindow({ width: 1024, height: 768 });
mainWindow.loadURL(`file://${__dirname}/src/home.html`);
mainWindow.webContents.openDevTools();
mainWindow.on('closed', () => {
mainWindow = null;
});
})
home.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Home</title>
</head>
<body>
<script src="../build/home.bundle.js"></script>
<h1>This is home!</h1>
<ul>
<li><a href="./contact.html">Contact us</a></li>
<li><a href="./about.html">About</a></li>
</ul>
</body>
</html>
**注:其中引用的js变成了 home.bundle.js **
home.js
import './style.css';
console.log('This is home!');
var User = require('./user');
var user1 = new User('kongxx');
console.log("Hi, I am " + user1.getName());
var user2 = new User('ken');
console.log("Hi, I am " + user2.getName());
console.log('running in electron: ', require('is-electron-renderer'));
about.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>About</title>
</head>
<body>
<script src="../build/about.bundle.js"></script>
<h1>This is about!</h1>
<a href="./home.html">Back</a>
</body>
</html>
**注:其中引用的js是 about.bundle.js **
about.js
import './style.css';
console.log('This is about!');
contact.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Contact</title>
</head>
<body>
<script src="../build/contact.bundle.js"></script>
<h1>This is contact!</h1>
<a href="./home.html">Back</a>
</body>
</html>
**注:其中引用的js是 contact.bundle.js **
contact.js
import './style.css';
console.log('This is contact!');
一个自定义的模块文件
module.exports = User;
function User(name) {
this.name = name;
}
User.prototype.getName = function() {
return this.name;
}
一个自定义的 css 文件
body {
background: grey;
font-size: 20px;
}