使用 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
打包我的项目并最终得到...
- 所有示例的单个
shared.js
文件包含 sharedpackage1
和 sharedpackage2
中的所有模块 - 及其依赖项 - 至少在一个示例中使用
- 每个样本一个
main.js
文件,其中包含特定于该样本的所有模块("local" 模块)
请注意...
sharedpackage1
和 sharedpackage2
是可以包含数百个不同的相互依赖模块的库
sharedpackage1
和 sharedpackage2
都没有 "root"(或 "main")模块将包中的所有其他模块作为依赖项
sharedpackage1
中的模块可以将 sharedpackage2
中的模块作为依赖项
sharedpackage2
中的模块只有 sharedpackage2
中的其他模块作为依赖项
sharedpackage1
或 sharedpackage2
中未在任何示例中使用的模块不应包含在 shared.js
中
- 特定于示例的配置应尽可能少且通用,因此可以在构建时使用 Node 脚本轻松生成
现在,只要每个新样本都有一个 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.
我有一个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
打包我的项目并最终得到...
- 所有示例的单个
shared.js
文件包含sharedpackage1
和sharedpackage2
中的所有模块 - 及其依赖项 - 至少在一个示例中使用 - 每个样本一个
main.js
文件,其中包含特定于该样本的所有模块("local" 模块)
请注意...
sharedpackage1
和sharedpackage2
是可以包含数百个不同的相互依赖模块的库sharedpackage1
和sharedpackage2
都没有 "root"(或 "main")模块将包中的所有其他模块作为依赖项sharedpackage1
中的模块可以将sharedpackage2
中的模块作为依赖项sharedpackage2
中的模块只有sharedpackage2
中的其他模块作为依赖项sharedpackage1
或sharedpackage2
中未在任何示例中使用的模块不应包含在shared.js
中
- 特定于示例的配置应尽可能少且通用,因此可以在构建时使用 Node 脚本轻松生成
现在,只要每个新样本都有一个 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 theinclude
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.