上一節的目錄結構以下:
首先在根目錄下新建配置文件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