-1

我无法bundles在优化的构建中工作,我正在尝试加载一些外部预构建包(不包含在需要构建过程输出中)。

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'
    };
});

构建配置 pathsmymodulesmymodule1mymodule2设置为empty:(或构建过程失败),我没有使用构建配置modules中的选项来生成捆绑包。

如果我按原样使用源,那么一切正常,正如预期的那样。

在构建版本(但未优化)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

就像 if在implementtest-bundle之前加载。requirejsdefine()

我错过了什么或做错了什么?

编辑

我已经使用上面的测试创建了一个分支来安装和构建(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

(请参阅此提交以获取所有修改以添加测试包的文件)

4

2 回答 2

0

每个文件只能使用一个定义。如果您仍然想使用,有一个捆绑配置选项,您可以说一个模块在您的已经捆绑的文件中。

正如它所说:

 bundle config is for pointing multiple module IDs to a bundle's module ID.
于 2016-04-29T02:33:35.483 回答
-1

因此,很明显,即使是require.js它自己,曾经包含在优化版本中的加载器也会或至少以不同的方式运行。

使其工作的一种简单方法是不包含任何加载程序并继续使用 require 来启动:

Gruntfile.js

- name: 'requireLib',
+ name: false,

index2.html

- <script src="app/main-built.js"></script>
+ <script src="lib/require/require.js" data-main="app/main-built"></script>

尽管如此,如果有人知道如何包含加载器并让预先构建的外部包(一个文件中的多个模块)工作,我想知道并将接受作为答案。

于 2016-04-29T17:55:55.180 回答