我正在努力使用requirejs优化器。如果我只是在浏览器中加载它而没有优化,那么此代码将起作用。如果我运行优化器,我得
: ENOENT, no such file or directory 'C:\Users\dev\checkout\src\main\webapp\resources\scripts\
json2.js'
In module tree: main
这是代码
requirejs.config({
paths : {
jquery : "lib/jquery",
bootstrap : "lib/bootstrap",
modals : "lib/modals",
tablesort : "lib/tablesort",
json2 : "lib/json2"
},
shim : {
"bootstrap" : [ "jquery" ],
"modals" : [ "jquery" ],
"tablesort" : [ "jquery" ],
"json2" : [ "jquery" ]
}
});
require([ "jquery", "json2","bootstrap", "modals", "tablesort", "registermodule", "personsmodule" ], function($) {
要使优化器工作,需要做些什么?我尝试将lib / json2放在require中。然后我得到jQuery问题,因为它是非AMD模块。
编辑:仍然在努力解决这个问题。试过最简单的例子。在浏览器中工作正常,但优化器抱怨没有找到文件。 lib / jquery.js和lib / modal.js。
requirejs.config({
paths : {
jquery : "lib/jquery",
modals : "lib/modals"
},
shim : {
"bootstrap" : [ "jquery" ],
"modals" : [ "jquery" ]
}
});
require([ "jquery", "modals" ], function($) {
console.log($("#leverandor_span").text());
$("#register_modal").modal("show");
});
答案 0 :(得分:25)
见mainConfigFile
。这已经到位,因此您无需将需求配置复制到运行时/优化时文件中,从而保持构建干燥:
//By default all the configuration for optimization happens from the command
//line or by properties in the config file, and configuration that was
//passed to requirejs as part of the app's runtime "main" JS file is *not*
//considered. However, if you prefer the "main" JS file configuration
//to be read for the build so that you do not have to duplicate the values
//in a separate configuration, set this property to the location of that
//main JS file. The first requirejs({}), require({}), requirejs.config({}),
//or require.config({}) call found in that file will be used.
mainConfigFile: '../some/path/to/main.js',
答案 1 :(得分:20)
问题:r.js没有解析require.config()。该配置仅适用于运行时。对于r.js,我们需要创建另一个文件来配置路径和其他东西。
({
appDir: "../",
baseUrl: "./",
dir: "dist",
modules: [
{
name: "bootloader"
}
],
paths: {
// libraries path
"json": "lib/json2",
"jquery": "lib/jquery",
"underscore": "lib/underscore",
"bootstrap": "lib/bootstrap",
"backbone": "lib/backbone",
"hogan": "lib/hogan",
// require plugins
"css": "lib/css",
"hgn": "lib/hgn",
"text": "lib/text"
}
})
$ r.js -o app.build.js
此处有更多详情: http://japhr.blogspot.it/2011/12/optimizing-requirejs-part-2.html
构建文件选项: http://requirejs.org/docs/optimization.html#wholeproject