上一节的目录结构以下:
首先在根目录下新建配置文件webpack.config.js, 并添加以下内容:webpack
webpack.config.js:web
module.exports = { mode: 'production', entry: './src/index.js', output: { filename: 'main.js' } };
在配置文件中,指定了最基础的三个选项,也是webpack默认的几个配置项,即:npm
此时在根目录下执行:npm run build
将会获得与上节同样的结果。segmentfault
在webpack4中若是不指定mode选项,webpack会默认将mode设为 'production',并在打包时发出警告。
mode可选值有三个:'development', 'production', 'none'.ide
不一样模式下,webpack会选择不一样的默认打包配置,好比在production模式下,打包会自动对代码进行压缩和各类优化。
三种模式的默认配置以下:优化
mode: 'development'ui
// webpack.development.config.js module.exports = { + mode: 'development' - devtool: 'eval', - cache: true, - performance: { - hints: false - }, - output: { - pathinfo: true - }, - optimization: { - namedModules: true, - namedChunks: true, - nodeEnv: 'development', - flagIncludedChunks: false, - occurrenceOrder: false, - sideEffects: false, - usedExports: false, - concatenateModules: false, - splitChunks: { - hidePathInfo: false, - minSize: 10000, - maxAsyncRequests: Infinity, - maxInitialRequests: Infinity, - }, - noEmitOnErrors: false, - checkWasmTypes: false, - minimize: false, - }, - plugins: [ - new webpack.NamedModulesPlugin(), - new webpack.NamedChunksPlugin(), - new webpack.DefinePlugin({ "process.env.NODE_ENV": JSON.stringify("development") }), - ] }
mode: 'production'spa
// webpack.production.config.js module.exports = { + mode: 'production', - performance: { - hints: 'warning' - }, - output: { - pathinfo: false - }, - optimization: { - namedModules: false, - namedChunks: false, - nodeEnv: 'production', - flagIncludedChunks: true, - occurrenceOrder: true, - sideEffects: true, - usedExports: true, - concatenateModules: true, - splitChunks: { - hidePathInfo: true, - minSize: 30000, - maxAsyncRequests: 5, - maxInitialRequests: 3, - }, - noEmitOnErrors: true, - checkWasmTypes: true, - minimize: true, - }, - plugins: [ - new TerserPlugin(/* ... */), - new webpack.DefinePlugin({ "process.env.NODE_ENV": JSON.stringify("production") }), - new webpack.optimize.ModuleConcatenationPlugin(), - new webpack.NoEmitOnErrorsPlugin() - ] }
mode: 'none'code
// webpack.custom.config.js module.exports = { + mode: 'none', - performance: { - hints: false - }, - optimization: { - flagIncludedChunks: false, - occurrenceOrder: false, - sideEffects: false, - usedExports: false, - concatenateModules: false, - splitChunks: { - hidePathInfo: false, - minSize: 10000, - maxAsyncRequests: Infinity, - maxInitialRequests: Infinity, - }, - noEmitOnErrors: false, - checkWasmTypes: false, - minimize: false, - }, - plugins: [] }
因为涉及的选项较多,在后续教程中均会涉及到,如今只需暂时了解便可
参考官网: https://webpack.js.org/config...
下一节:entry与output