使用 r.js 对不同 AMD 依赖树的共享依赖进行分组

Grouping shared dependencies of different AMD dependency trees with r.js

我有一个AMD项目,基本上有以下结构:

samples/sample1/main
________________
* sharedpackage1/subfolder1/module1
* sharedpackage1/subfolder2/module1
* ./localmodule1
* ./localmodule2
* ./subfolder1/localmodule1



samples/sample2/main
________________
* sharedpackage1/subfolder2/module2
* sharedpackage2/subfolder1/subfolder1/module1
* ./localmodule1
* ./localmodule2
* ./localmodule3
* ./subfolder1/localmodule1
* ./subfolder1/localmodule2
* ./subfolder2/localmodule1



samples/sample3/main
________________
* sharedpackage2/subfolder2/subfolder1/module1
* sharedpackage2/subfolder3/module1
* ./localmodule1
* ./localmodule2
* ./localmodule3
* ./localmodule4
* ./localmodule5

...

我想使用 r.js 打包我的项目并最终得到...

请注意...

现在,只要每个新样本都有一个 main.js 文件作为 "root" 模块,让我的构建脚本自动获取每个新样本应该不会太难附加配置。所以我会处理的。

从我找到的任何文档中我似乎无法弄清楚的是,如何创建一个仅包含来自 sharedpackage1 和 [=] 的模块的 shared.js 文件15=] 在示例中使用,但没有定义 shared 模块,我在其中手动列出每个示例的每个 main.js 的所有独立依赖项(这不适合我的用例)。

是否可以用 r.js 做到这一点?如果是,我该如何实现?

我在 Github 收到 jrburke 的以下答复:

example-multipage project sounds closest to what you describe. However, it may not be as smart as you are wanting. The "common" layer still needs the build config to list some top level modules to include. It can trace the modules by those top level modules in the include array, so it does not need to be an exhaustive graph listing.

It also sounds like you may want bundles config, if you don't want to do the two-staged loading that example-multipage does. But bundles config can be very wordy.

If you want really fine grained tracing and grouping, amodro-trace will just trace the modules, and you can use your own build scripts to combine the traced modules into the bundles you want. It still may take some effort to turn turn those built files into a runtime requirejs config that can be used to efficiently load the bundles though.

There are no solid easy answers for complex bisecting of module graphs and separating the bundles. r.js is not very smart as far as not doing any fancy source modifications to, for example, introduce intermediate require([]) calls to load bundles.