webpack-demo
|- package.json
|- webpack.config.js
|- /dist
|- bundle.js
|- index.html
|- /src
|- data.xml
|- icon.png
|- style.css
|- my-font.woff
|- my-font.woff2
+ |- print.js
|- index.js
|- /node_modules
输出设置output,输出文件根据entry的key输出js
npm install --save-dev html-webpack-plugin
npm install --save-dev clean-webpack-plugin
// webpack.config.js
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin'); // 自动生成index.html
// 自动清理,清理dist旧文件
// const CleanWebpackPlugin = require('clean-webpack-plugin');
// 升级版本后,自动清理,清理dist旧文件
const {CleanWebpackPlugin} = require('clean-webpack-plugin');
module.exports = {
entry: {
index: './src/index.js',
print: './src/print.js'
},
plugins: [
// new CleanWebpackPlugin(['dist']),
new CleanWebpackPlugin(), // 自动删除webpack output 对应目录
new HtmlWebpackPlugin({
title: 'VISION-CANVAS-L', // 设置输出文件标题名
filename: 'index.html', // 设置输出文件名
template: 'view/index.html', // 设置模板
})
],
output: {
filename: '[name].bundle.js', // 输出文件根据entry的key来命名
path: path.resolve(__dirname, 'dist')
},
module: {
rules: [
{
test: /\.css$/,
use: [
'style-loader',
'css-loader'
]
},
{
test: /\.(png|svg|jpg|gif)$/,
use: [
'file-loader'
]
},
{
test: /\.(woff|woff2|eot|ttf|otf)$/,
use: [
'file-loader'
]
},
{
test: /\.(csv|tsv)$/,
use: [
'csv-loader'
]
},
{
test: /\.xml$/,
use: [
'xml-loader'
]
}
],
}
};
<!-- /dist/index.html -->
<!doctype html>
<html>
<head>
<title><%= htmlWebpackPlugin.options.title %></title>
</head>
<body>
</body>
</html>