系列:
webpack入门——了解及使用
webpack进阶——缓存与独立打包
文件目录:
-- /node_modules:依赖的各种模块
-- /src
– index.js
– print.js
– style.css
– package.json:配置npm相关设置
– webpack.config.js:配置webpack
package.json脚本(输入npm run xxx,xxx为脚本中对应命令,将会在命令行中执行相应操作):
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build": "webpack", // webpack打包
"watch": "webpack --watch", // webpack监听模式
"start": "webpack-dev-server --open" // 自动刷新
}
webpack.config.js:
(这是一个我们已经配置好的webpack配置js,本文中的讲解都将基于该文件,会在本文件上多多少少做一些改变以供测试。)
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const CleanWebpackPlugin = require('clean-webpack-plugin');
const webpack = require('webpack');
module.exports = {
entry: {
app: './src/index.js',
print: './src/print.js'
},
output: {
filename: '[name].js',
path: path.resolve(__dirname, 'dist')
},
module: {
rules: [
{
test: /.css$/,
use: ['style-loader', 'css-loader']
}
]
},
devtool: 'inline-source-map',
devServer: {
hot: true,
contentBase: path.resolve(__dirname, 'dist'),
publicPath: '/'
},
plugins: [
new webpack.optimize.CommonsChunkPlugin({
name: "common",
minChunks: Infinity,
}),
new webpack.HotModuleReplacementPlugin(),
new CleanWebpackPlugin(['dist']),
new HtmlWebpackPlugin({
title: 'Output Management'
})
]
HtmlWebpackPlugin
由webpack.config.js可知,webpack打包后生成的是若干个js文件,但是最终这些js文件都是应用在html中的。
假如打包后的文件只有app.js,那么完全可以手动创建一个html,并加入