2019-5-22 公司项目 代码优化1-webpack DllPlugin/DllReferencePlugin 抽出不常编译的模块单独打包

博客主要围绕Webpack展开,提及webpack.dll.conf.js和webpack.dev.conf.js文件。指出使用DllReferencePlugin和DllPlugin时context需保持一致,否则抽出失败。还展示了抽出前后文件大小变化,抽出前h5.js约1.2M,抽出vendor_xxx.dll.js后变为1.0M。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.webpack

webpack.DllPlugin/DllReferencePlugin 抽出常用的插件,单独打包成js然后,塞到index.html中

webpack.dll.conf.js

const path = require('path');
const webpack = require('webpack');

const webpackConfig = {
    entry: {
        vendor: ['vue','vue-router','axios','vuex','fastclick','vue-lazyload','nprogress'],
    },
    devtool: '#source-map',
    output: {
        path: path.join(__dirname, '../dist'),
        filename: '[name]_[hash].dll.js',
        library: '[name]_[hash]',
    },
    plugins: [
        new webpack.DllPlugin({
            context: __dirname,
            name: '[name]_[hash]',
            path: path.join(__dirname, '../dist/manifest.json'),
        })
    ],
};

module.exports = webpackConfig

 

webpack.dev.conf.js

    new webpack.DllReferencePlugin({
        //  context: path.join(__dirname),
        context:  __dirname,
        manifest: require('../dist/manifest.json'),
    }),
    new HtmlWebpackPlugin({
        filename: 'index.html',
        template: 'index.html',
        inject: true,
        hash:true
    }),
    new AddAssetHtmlPlugin([{
        filepath: path.resolve(__dirname, '../dist/*.dll.js'),
    }]),
    new webpack.optimize.CommonsChunkPlugin({
        name: 'app',
        async: 'vendor-async',
        children: true,
        minChunks: 3
    })

坑:

DllReferencePlugin和DllPlugin的context:一定要保持一致,否则,会找不到路径,然后抽出失败;

 

抽出前,h5.js大小在1.2M左右,抽出vendor_xxx.dll.js后,变成1.0M;

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值