2

我有一个大型客户端项目,我想将其捆绑成 2 个捆绑包而不是 1 个捆绑包。

这是我的依赖树:

模块的依赖树

所需的输出将是拥有这些捆绑包:

  1. main那包含着b
  2. x仅包括a(因为b已包含在第一个捆绑包中,我不希望用户多次下载一段代码)。

这是我的优化器配置:

({
  appDir: 'www',
  baseUrl: 'js/',
  mainConfigFile: 'www/js/require.config.js',
  dir: 'www-release',
  modules: [
    {
      name: 'main',
      exclude: ['x']
    },
    {
      name: 'x',
      exclude: ['main']
    }
  ],
  optimize: 'none',
  removeCombined: true
})

我确实想从main的整个依赖树中排除x,但仍然包含我明确需要的模块,例如a.

我知道:

  1. include— 显式包含不需要的模块及其整个依赖树。
  2. exclude— 排除一个模块实际上是排除了它的整个依赖树,include在发生冲突时会覆盖。
  3. excludeShallow— 包括模块的依赖树,包括模块本身。

有了这个,我看不到一个明确的方法来完成我想要的,你能帮忙吗?

4

1 回答 1

1

您需要像这样使用 requier.js 捆绑功能。

在你的 require.config.js 文件中

需要写入捆绑设置

bundles: {
    x: ['files for x bundle'],
    b: ['files for b bundle']
}

在此之后需要更改构建文件。

优化器配置文件

({
     appDir: 'www',
     baseUrl: 'js/',
    // mainConfigFile: 'www/js/require.config.js',// not need here
     dir: 'www-release',
     modules: [
        {
            name: 'main',
            create: true, // this will create www-release/main.js file(main bundle)
            include: ['main'],
            exclude: ['x']
        },
        {
            name: 'b',
            create: true,
            include: ['files for b bundle'],
            exclude: ['main']
        },
        {
            name: 'x',
            create: true,
            include: ['files for x bundle']
            exclude: ['main']
        }
   ],
   optimize: 'none',
   removeCombined: true
   paths: {
       //put paths object from requierjs.confige file 
   }
});
于 2016-10-31T09:00:24.177 回答