Project files about webpack: https://gitee.com/codeFarmerPen/webpack
If you need to read (basic), please move: https://my.oschina.net/u/3797834/blog/1649157
webpack.config.js file
const path = require('path');
let htmlwebpackplugin = require('html-webpack-plugin');//引入html-webpack-plugin插件
let export_html= {
entry: {
main:__dirname+"/app/js/main.js",//入口文件
main1:__dirname+"/app/js/main1.js",//入口文件
},
output: {
path: __dirname+"/_build/",
filename: "js/[name].js",//产出文件,name根据entry的入口文件键名定
},
module: {
loaders: [
{
test: /(\.jsx|\.js)$/,
loader: 'babel-loader',
query: {
presets: ['es2015']
}
},
]
}
,
plugins: [
//new一个模板出来,这一个不使用chunks
new htmlwebpackplugin({
template: './app/home.html',//入口文件
filename: 'home1.html',//产出文件
}),
//new一个模板出来
new htmlwebpackplugin({
template: './app/home.html',//入口文件
filename: 'home2.html',//产出文件
chunks : ['main'],//可以设置chunks按需引入JS文件,不设置就会引入所有产出的js
chunksSortMode: 'manual',//将chunks按引入的顺序排序,不用这个的话,引入到html的JS可能是错乱排序的
})
]
};
module.exports=export_html;
see plugins here
//new一个模板出来,这一个不使用chunks
new htmlwebpackplugin({
template: './app/home.html',
filename: 'home1.html',// 会生成home1.html
}),
//new一个模板出来
new htmlwebpackplugin({
template: './app/home.html',
filename: 'home2.html',//会生成home2.html
chunks : ['main'],//注意:chunks里面的值是对应entry入口的键名来的
chunksSortMode: 'manual',
})
home.html file in the app directory
home1.html file in the _build directory
home2.html file in the _build directory
As you can see, home1.html introduces two js files, and main1.js comes before main.js,
And home2.html, only the specified main.js is introduced;
Add to the chunks of home2.html: main1
//new一个模板出来
new htmlwebpackplugin({
template: './app/home.html',//入口文件
filename: 'home2.html',//产出文件
chunks : ['main',"main1"],//可以设置chunks按需引入JS文件,不设置就会引入所有产出的js
chunksSortMode: 'manual',//将chunks按引入的顺序排序,不用这个的话,引入到html的JS可能是错乱排序的
})
Because in chunks, main is before main1, the imported files are also in this order;
The problem of order is mainly due to: this attribute
chunksSortMode: 'manual',//将chunks按引入的顺序排序,不用这个的话,引入到html的JS可能是错乱排序的
Going a step further:
It is very troublesome to do this new every time, so write a function to simplify the process
let get_html = function(name,chunk){//封装
return {
template: './app/ejs for html/'+ name + '.ejs',
filename: name+ '.html',
chunks : ['main',chunk||name],//可以设置chunks按需引入JS文件,不设置就会引入所有产出的js
chunksSortMode: 'manual',//将chunks按引入的顺序排序
inject : true,
hash : true,
xhtml : true
}
};
Then test a new one in the plugin;
At this point, webpack.config.js:
const path = require('path');
let htmlwebpackplugin = require('html-webpack-plugin');//引入html-webpack-plugin插件
let get_html = function(name,chunk){//封装
return {
template: './app/'+ name + '.html',
filename: name+ '.html',
chunks : ['main',chunk||null],//这里引入公共文件main.js,另外一个文件按需引入,当然也可以把这个的值设为数组,传入function的第二个值用数组就行
chunksSortMode: 'manual',//将chunks按引入的顺序排序
inject : true,//所有JavaScript资源插入到body元素的底部
hash : true,//避免缓存
xhtml : true //规范html书写
}
};
let export_html= {
entry: {
main:__dirname+"/app/js/main.js",//入口文件
main1:__dirname+"/app/js/main1.js",//入口文件
},
output: {
path: __dirname+"/_build/",
filename: "js/[name].js",//产出文件,name根据entry的入口文件键名定
},
module: {
loaders: [
{
test: /(\.jsx|\.js)$/,
loader: 'babel-loader',
query: {
presets: ['es2015']
}
},
]
}
,
plugins: [
//new一个模板出来测试一下
new htmlwebpackplugin(get_html("home","main1"))
]
};
module.exports=export_html;
result:
success!
Below are all my links about webpack config
webpack configuration (first step: configuration prerequisites): https://my.oschina.net/u/3797834/blog/1648528
Webpack configuration (Step 2: Getting Started): https://my.oschina.net/u/3797834/blog/1648578
webpack configuration (the third step: ES6): https://my.oschina.net/u/3797834/blog/1649033
webpack configuration (step 4: html article (basic article)): https://my.oschina.net/u/3797834/blog/1649157
webpack configuration (step 4: html article (advanced article)): https://my.oschina.net/u/3797834/blog/1649196
webpack configuration (step 4: html article (template article)): https://my.oschina.net/u/3797834/blog/1649246
webpack configuration (step 5: less/css articles (basic articles)): https://my.oschina.net/u/3797834/blog/1649270
webpack configuration (step 5: less/css articles (advanced articles)): https://my.oschina.net/u/3797834/blog/1649288
webpack configuration (step 5: less/css article (url picture article)): https://my.oschina.net/u/3797834/blog/1649302