0

我是r.js优化新手,但喜欢requirejs

构建-config.js

({
    appDir: "./src/main/webapp/webresources/javascript/",
    baseUrl: "./",
    dir: "./target/webresources/js",
    
    optimizeCss: "none",
    mainConfigFile: "./src/main/webapp/webresources/javascrip/app.js",
    
    inlineText: true,
    removeCombined: true,
    
    modules: [
    {
        name: "app",
    }
    ]
})

app.js看起来像

if (typeof _JSHome=== "undefined") {
    _JSHome = "/webresources/javascript/edulastic";
}

if (typeof EditorValue === "undefined") {
    EditorValue = "";
}

require.config({

    baseUrl: '/webresources/javascript',
    waitSeconds: 180,
    paths: {
        tpl                    : _JSHome+'/js/tpl',
        underscore             : 'underscore-min',
        backbone               : 'backbone-min',
        text                   : 'text/text',
        jquery                  : 'jquery',
        jqueryuitouchpunch     : "jquery.ui.touch-punch",
        modernizr              : 'modernizr',
        hammer                 : 'hammer.min',
        detectizr              : 'detectizr',
        bootstrap              : _edulasticJSHome+'/bootstrap/js/bootstrap.min',
        fastClick              : "mobileutils/fastclick/fastclick.min",
        asMetaData             : _JSHome+'/js/app/metaData',
        highCharts             : '/webresources/javascript/highcharts/highcharts-min',
    },
});

当我 r.js -o build-config.js在项目根目录中运行时,出现以下错误:

尝试仅使用也是有效 JSON 的配置,或者不使用 mainConfigFile,而是将所需的配置值复制到构建文件或提供给优化器的命令行参数中。

解析的源错误:e:/mongrel_mpq/src/main/webapp/webresources/javascript/app.js: > ReferenceError: _JSHome 未定义

重复但没有解决方案 - Require.js 优化器和路径中的变量

4

1 回答 1

1

您拥有的配置app.js计算配置。例如,您设置_JSHome一个值,然后在配置中使用它。在运行时,这没有问题。但是,r.js它并非旨在执行您传递给mainConfigFile. 它所做的是寻找传递给require.config(或requirejs.config)并使用 JSON 对象的 JSON 对象。就好像r.js要进入您的app.js文件并复制其中的所有文本require.config(...),然后将其粘贴到它自己的执行上下文中。当它尝试使用从您的文件中捕获的内容时,您会收到未定义app.js的错误,因为解释您的配置的上下文中,_JSHomer.js_JSHome没有定义。

简单的解决方案是使用不计算的配置。但是,如果您需要计算配置,那么要r.js使用它,就需要在r.js看到它之前对其进行计算。这可以作为构建系统的一部分来完成(使用 Grunt、Gulpmake等)。

于 2014-12-02T17:57:45.907 回答