Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

webpack require.ensure 分割打包手动控制打包文件 #154

Open
mishe opened this issue Feb 23, 2017 · 1 comment
Open

webpack require.ensure 分割打包手动控制打包文件 #154

mishe opened this issue Feb 23, 2017 · 1 comment

Comments

@mishe
Copy link
Owner

mishe commented Feb 23, 2017

  • 要分割打包,首先要设置webpack的output,增加chunkFilename: "[name].js"的配置,比如:
{
   entry: {
        "index": "src/home.js"
    },
    output: {
        filename: "bundle.js",
        chunkFilename: "[name].js"
    }
}

require.ensure([]) 引用模块,其使用方法如下:

require.ensure(dependencies: String[], callback: function(require), chunkName: String)

  • require.ensure() API有三个参数,一般文档都只介绍2个入参,
  • 这样做,会有两个不是很符合意愿的结果:
    1. webpack会给每个require.ensure生产一个chunk file;
    1. [name] 是一个自动分配的、可读性很差的id。
  • 而第三个参数就是给模块命名的,并且可以把多个ensure合并到一个chunk中

采用require.ensure加载模块并给打包模块命名,代码如下:

require.ensure(['./mall/orders/orderInfo/orderInfo'],function(require){
            var OrderInfo = require('./mall/orders/orderInfo/orderInfo');
            new OrderInfo({el:fresh.$content,oid:oid});
        },'mall');
require.ensure(['./mall/orders/orderInfo/orderInfo'],function(require){
            var OrderInfo = require('./mall/orders/orderInfo/orderInfo');
            new OrderInfo({el:fresh.$content,oid:oid});
        },'mall');

这边把2个和mall相关的代码全部打包到mall.js 文件

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant