webpack中的runtimeChunk
2021-02-06 本文已影响0人
Gino_Li
作用:打包时生成一个体积很小runtime.xxx.js文件,用作映射其他chunk文件,目的是更新后,以较小的代价利用缓存,提升页面加载速度。
结合script-ext-html-webpack-plugin
插件可将runtime文件内联进html文件,减少请求
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const ScriptExtHtmlWebpackPlugin= require('script-ext-html-webpack-plugin')
module.exports = {
mode: 'production',
entry: {
index: './src/index.js'
},
output: {
filename: '[name].[contenthash].js',
path: path.resolve(__dirname, 'dist'),
publicPath: '/'
},
devtool: 'inline-source-map',
plugins: [
new HtmlWebpackPlugin({
title: 'Caching',
}),
new CleanWebpackPlugin({cleanStaleWebpackAssets: false}),
new ScriptExtHtmlWebpackPlugin({ //结合`script-ext-html-webpack-plugin`插件可将runtime文件内联进html文件
inline: /runtime~.+\.js$/ //正则匹配runtime文件名
})
],
optimization: {
runtimeChunk: true,
splitChunks: {
chunks: 'all'
}
}
};