13

我正在努力使用 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");

});
4

2 回答 2

24

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',
于 2013-01-24T05:55:04.210 回答
20

问题:r.js 没有解析 require.config()。该配置仅适用于运行时。对于 r.js,我们需要创建另一个文件来配置路径和其他内容。

创建一个文件(例如 app.build.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

于 2012-07-12T14:45:26.883 回答