【发布时间】:2016-04-30 01:55:55
【问题描述】:
我无法让 bundles 在优化构建中工作,我正在尝试加载一些外部 pre-built 捆绑包(不包括在需要构建过程输出中)。
在requirejs.config:
paths: {
'mymodules': '../lib/test-bundle/test-bundle'
},
bundles: {
'mymodules': ['mymodule1', 'mymodule2']
}
test-bundle 内容为:
console.log("defining modules...");
define('mymodule1', ['jquery'], function($) {
console.log('within mymodule1', $.fn.jquery);
return {
test: 'module1'
};
});
define('mymodule2', ['jquery'], function($) {
console.log('within mymodule2', $.fn.jquery);
return {
test: 'module2'
};
});
在构建配置中paths for mymodules、mymodule1 和mymodule2 设置为empty:(或构建过程失败),我没有使用@ build config 中的 987654333@ 选项以生成捆绑包。
如果我按原样使用源,一切正常,正如预期的那样。
在内置版本中(但未优化)test-bundle 被加载并打印"defining modules",然后超时加载mymodule2:
Error: Failed to load root module (viewmodels/shell). Details: Load timeout for modules: mymodule2(…)
Uncaught Error: Failed to load root module (viewmodels/shell). Details: Load timeout for modules: mymodule2
http://requirejs.org/docs/errors.html#timeout
在构建和优化的版本中还有一个错误:
Uncaught ReferenceError: define is not defined
如果test-bundle 在requirejs 执行define() 之前加载。
我错过了什么或做错了什么?
编辑
我已经使用上面的测试创建了一个分支来安装和构建(nodejs npm 可能在系统上需要grunt-cli)
git clone https://github.com/xenogenesi/HTMLStarterKitPro
cd HTMLStarterKitPro
git checkout test-bundle
# nodejs npm required on the system (maybe grunt-cli)
npm install # required only once to install node modules
grunt build-only # create a build/ directory and the content
php -S localhost:8888 # to publish the sources as they are
# browse to http://localhost:8888
php -S localhost:7777 -t build # to publish the built versions
# browse to http://localhost:7777 for built but not optimized
# browse to http://localhost:7777/index2.html for built optimized
(请参阅 this commit 以了解所有已修改以添加测试包的文件)
【问题讨论】:
-
minimal reproducible example 会很有帮助。另外,您使用的是哪个版本的 RequireJS?新版本修复了旧版本中存在的错误。
-
@Louis,提供一个最小的例子需要时间,环境也不一样,所以,如果你知道如何安装 node、npm、grunt-cli,请查看更新应该快速简单。 RequireJS 版本是 2.2.0
标签: javascript requirejs requirejs-optimizer