初始化
cd 项目名
webpack init
创建第二页面与其头文件
touch page2.html
touch src/page2.js
修改page2.jsconsole.log("success")
修改webpack.config.js
添加page2.html至plugins项,添加src/page2.js至entry项
最终模板:
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const WorkboxWebpackPlugin = require('workbox-webpack-plugin');
const isProduction = process.env.NODE_ENV == 'production';
const config = {
entry: {
main: './src/index.js',
page2: './src/page2.jz'
},
output: {
path: path.resolve(__dirname, 'dist'),
},
devServer: {
open: true,
host: 'localhost',
},
plugins: [
new HtmlWebpackPlugin({
template: 'index.html',
filename: 'index.html',
chunks: ['main']
}),
HtmlWebpackPlugin({
template: 'page2.html',
filename: 'page2.html',
chunks: ['page2']
})
],
module: {
rules: [
{
test: /\.(eot|svg|ttf|woff|woff2|png|jpg|gif)$/i,
type: 'asset',
},
],
},
};
module.exports = () => {
if (isProduction) {
config.mode = 'production';
config.plugins.push(new WorkboxWebpackPlugin.GenerateSW());
} else {
config.mode = 'development';
}
return config;
};
运行webpack serve,进入网址: localhost:3000/page2.html
(根据实际配置自行更改)
打开控制台有"success"输出
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。