我正在努力使用requirejs优化器和非AMD模块

pet*_*hel 13 javascript jquery require requirejs

我正在努力使用requirejs优化器.如果我只是在浏览器中加载它而没有优化,那么此代码将起作用.如果我运行优化器,我会得到

: ENOENT, no such file or directory 'C:\Users\dev\checkout\src\main\webapp\resources\scripts\
json2.js'

In module tree: main
Run Code Online (Sandbox Code Playgroud)

这是代码

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($) {
Run Code Online (Sandbox Code Playgroud)

要使优化器工作,需要做些什么?我尝试将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");

});
Run Code Online (Sandbox Code Playgroud)

Pro*_*ega 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',
Run Code Online (Sandbox Code Playgroud)


Mar*_*Zen 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"
   }
})
Run Code Online (Sandbox Code Playgroud)

运行优化器:

$ r.js -o app.build.js

更多细节:http: //japhr.blogspot.it/2011/12/optimizing-requirejs-part-2.html

构建文件选项:http: //requirejs.org/docs/optimization.html#wholeproject