webpack dll优化记录
1、编写dll打包配置
// webpack.dll.js
const path = require('path');
const webpack = require('webpack');
console.log('webpack dll start...')
const dllPath = path.join(__dirname, 'dll');
module.exports = {
mode: 'development',
entry: {
dll: ['jquery']
},
output: {
path: dllPath,
filename: '[name].js',
library: '[name]',
libraryTarget: "global"
},
plugins: [
new webpack.DllPlugin({
name: "[name]",
path: path.join(dllPath, '[name]-manifest.json'),
}),
]
};
2、编写生产打包配置
// webpack.prod.js
const path = require('path');
const webpack = require('webpack');
console.log('webpack prod start...')
module.exports = {
mode: 'development',
entry: './test.ts',
output: {
filename: 'test.js',
path: path.resolve(__dirname, 'dist'),
library: 'test',
libraryTarget: "window"
},
module: {
rules: [{
test: /\.ts$/,
use: "ts-loader"
}]
},
resolve: {
extensions: [
'.ts','js'
]
},
plugins: [
new webpack.DllReferencePlugin({
name:'dll',
context: __dirname,
manifest: require('./dll/dll-manifest.json')
})
]
};
3、分别运行脚本
"build:dll": "webpack --config ./webpack.dll.js -p",
"build": "webpack --config ./webpack.prod.js",
4、使用打包后的文件
dll/dll.js
dist/test.js
<body>
123
<script src="../dll/dll.js"> </script>
<script src="../dist/test.js"> </script>
</body>
<全文结束>