全局配置
打開 ~\build\webpack.base.conf.js ,找到entry,添加多入口
entry: {
main: './src/main.js',
main2: './src/main2.js',
main3: './src/main3.js',
},
run dev 開發環境
修改 webpack.dev.conf.js
打開 ~\build\webpack.dev.conf.js ,在plugins下找到new HtmlWebpackPlugin,在其后面添加對應的多頁,並為每個頁面添加Chunk配置
chunks: ['main']中的main對應的是webpack.base.conf.js中entry設置的入口文件
plugins:[
// <a rel="nofollow" href="https://github.com/ampedandwired/html-webpack-plugin" target="_blank">https://github.com/ampedandwired/html-webpack-plugin</a>
// 多頁:index.html → main.js
new HtmlWebpackPlugin({
filename: 'index.html',//生成的html
template: 'index.html',//來源html
inject: true,//是否開啟注入
chunks: ['main']//需要引入的Chunk,不配置就會引入所有頁面的資源
}),
// 多頁:index2.html → main2.js
new HtmlWebpackPlugin({
filename: 'index2.html',//生成的html
template: 'index2.html',//來源html
inject: true,//是否開啟注入
chunks: ['main2']//需要引入的Chunk,不配置就會引入所有頁面的資源
}),
// 多頁:index3.html → main3.js
new HtmlWebpackPlugin({
filename: 'index3.html',//生成的html
template: 'index3.html',//來源html
inject: true,//是否開啟注入
chunks: ['main3']//需要引入的Chunk,不配置就會引入所有頁面的資源
})
]
run build 編譯
修改 config/index.js
打開~\config\index.js,找到build下的index: path.resolve(__dirname, '../dist/index.html'),在其后添加多頁
build: {
index: path.resolve(__dirname, '../dist/index.html'),
index2: path.resolve(__dirname, '../dist/index2.html'),
index3: path.resolve(__dirname, '../dist/index3.html'),
},
修改 webpack.prod.conf.js
打開~\build\webpack.prod.conf.js,在plugins下找到new HtmlWebpackPlugin,在其后面添加對應的多頁,並為每個頁面添加Chunk配置
HtmlWebpackPlugin 中的 filename 引用的是 config/index.js 中對應的 build
plugins: [
// 多頁:index.html → main.js
new HtmlWebpackPlugin({
filename: config.build.index,
template: 'index.html',
inject: true,
minify: {
removeComments: true,
collapseWhitespace: true,
removeAttributeQuotes: true
// more options:
// <a rel="nofollow" href="https://github.com/kangax/html-minifier#options-quick-reference" target="_blank">https://github.com/kangax/html-minifier#options-quick-reference</a>
},
// necessary to consistently work with multiple chunks via CommonsChunkPlugin
chunksSortMode: 'dependency',
chunks: ['manifest','vendor','main']//需要引入的Chunk,不配置就會引入所有頁面的資源
}),
// 多頁:index2.html → main2.js
new HtmlWebpackPlugin({
filename: config.build.index2,
template: 'index2.html',
inject: true,
minify: {
removeComments: true,
collapseWhitespace: true,
removeAttributeQuotes: true
},
chunksSortMode: 'dependency',
chunks: ['manifest','vendor','main2']//需要引入的Chunk,不配置就會引入所有頁面的資源
}),
// 多頁:index3.html → main3.js
new HtmlWebpackPlugin({
filename: config.build.index3,
template: 'index3.html',
inject: true,
minify: {
removeComments: true,
collapseWhitespace: true,
removeAttributeQuotes: true
},
chunksSortMode: 'dependency',
chunks: ['manifest','vendor','main3']//需要引入的Chunk,不配置就會引入所有頁面的資源
}),
]
頁面較多時,可以用循環將 HtmlWebpackPlugin 添加到 plugins
// utils.js
exports.getEntry = function(globPath, pathDir) {
var files = glob.sync(globPath);
var entries = {},
entry, dirname, basename, pathname, extname;
for (var i = 0; i < files.length; i++) {
entry = files[i];
dirname = path.dirname(entry);
extname = path.extname(entry);
basename = path.basename(entry, extname);
pathname = path.join(dirname, basename);
pathname = pathDir ? pathname.replace(new RegExp(^_^ _^` + pathDir), '') : pathname;
entries[pathname] = ['./' + entry];
}
return entries;
}
// webpack.base.conf.js
var pages = Object.keys(utils.getEntry('../src/views/**/*.html', '../src/views/'));
pages.forEach(function (pathname) {
// <a rel="nofollow" href="https://github.com/ampedandwired/html-webpack-plugin" target="_blank">https://github.com/ampedandwired/html-webpack-plugin</a>
var conf = {
filename: '../views/' + pathname + '.html', //生成的html存放路徑,相對於path
template: '../src/views/' + pathname + '.html', //html模板路徑
inject: false, //js插入的位置,true/'head'/'body'/false
/*
* 壓縮這塊,調用了html-minify,會導致壓縮時候的很多html語法檢查問題,
* 如在html標簽屬性上使用{{...}}表達式,所以很多情況下並不需要在此配置壓縮項,
* 另外,UglifyJsPlugin會在壓縮代碼的時候連同html一起壓縮。
* 為避免壓縮html,需要在html-loader上配置'html?-minimize',見loaders中html-loader的配置。
*/
// minify: { //壓縮HTML文件
// removeComments: true, //移除HTML中的注釋
// collapseWhitespace: false //刪除空白符與換行符
// }
};
if (pathname in config.entry) {
conf.favicon = 'src/images/favicon.ico';
conf.inject = 'body';
conf.chunks = ['vendors', pathname];
conf.hash = true;
}
config.plugins.push(new HtmlWebpackPlugin(conf));
});
同樣入口 entry 也可以使用
// webpack.base.conf.js
entry: {
app: utils.getEntry('../src/scripts/**/*.js', '../src/scripts/')
},