webpack 打包trunk分析

webpack打包确实是很智能,它会分析你当前文件夹下所有文件,然后决定如何打包如何拆分才是最合理。

前提:webpack 默认有一个(或多个)entry入口。

举例子:入口文件:index.js---什么都不依赖

    页面其他组件:a.js--依赖jquery,vue

    这时候当你执行命令 webpack打包时,它会保持index最小化,把jquery打包到a.js的(trunk)中 实现按需加载

 

    情况二:

    入口文件:index.js---什么都不依赖

    页面其他组件:a.js--依赖jquery,vue

    页面其他组件:b.js-依赖jquery,react

 

    此时,打包,它会发现,页面有共同依赖jquery,所以它会把jquery打包到index.js这个trunk中。这样a和b组件在加载时候就不用重复加载jquery

 

 

当然这个公共抽取打包功能依赖与webpack的一个插件--在webpack.config.js中查看

var path = require('path')
var webpack = require('webpack')

module.exports = {
    entry: './src/index.js',
    output: {
        path: path.resolve(__dirname, './dist'),
        publicPath: '/dist/',
        chunkFilename: '[name].bundle.js',
        filename: 'build.js'
    },
    module: {
        rules: [
            {
                test: /\.html$/,
                loader: 'html-loader'
            },
            {
                test: /\.scss$/,
                loaders: ['style-loader', 'css-loader', 'sass-loader'],
                include: /src/

            },
            {
                test: /\.js$/,
                loader: 'babel-loader',
                exclude: /node_modules/,
                include: /src/
            },
        ]
    },
    plugins: [new webpack.optimize.CommonsChunkPlugin({
        name: 'main',
        // Move dependencies to our main file
        children: true,
        // Look for common dependencies in all children,
        minChunks: 2,
        // How many times a dependency must come up before being extracted
    }),]

}
View Code

 

转载于:https://www.cnblogs.com/WhiteHorseIsNotHorse/p/6493959.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值