简体   繁体   English

需求路径中的变量不适用于r.js

[英]Variable in require path not working with r.js

I am new to r.js optimization but a fan of requirejs 我是r.js优化的新手,但是requirejs的粉丝

build-config.js 建立-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 looks something like 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',
    },
});

When I run r.js -o build-config.js in my project root directory, I'm getting the following error : 当我在项目根目录中运行r.js -o build-config.js时,出现以下错误:

Try only using a config that is also valid JSON, or do not use mainConfigFile and instead copy the config values needed into a build file or command line arguments given to the optimizer. 尝试仅使用也是有效JSON的配置,或者不使用mainConfigFile,而是将所需的配置值复制到生成文件或提供给优化器的命令行参数中。

Source error from parsing: e:/mongrel_mpq/src/main/webapp/webresources/javascript/app.js: > ReferenceError: _JSHome is not defined 来自解析的源错误:e:/mongrel_mpq/src/main/webapp/webresources/javascript/app.js:> ReferenceError:_JSHome未定义

Duplicate but without solution - Require.js optimizer and variables in paths 重复但没有解决方案- 路径中的Require.js优化器和变量

The configuration you have in app.js is a computed configuration. app.js中的配置是计算的配置。 For instance, you set _JSHome to a value and then use it in the configuration. 例如,您将_JSHome设置为一个值,然后在配置中使用它。 At runtime, there is no problem with this. 在运行时,这没有问题。 However, r.js is not designed to execute what you pass to mainConfigFile . 但是, r.js并非旨在执行传递给mainConfigFile What it does is look for a JSON object that is passed to require.config (or requirejs.config ) and uses the JSON object. 它所做的是查找传递给require.config (或requirejs.config )并使用JSON对象的JSON对象。 It is as if r.js were to go into your app.js file and copy all the text inside require.config(...) and then paste it into its own execution context. 好像r.js进入您的app.js文件并复制require.config(...)内的所有文本,然后将其粘贴到其自己的执行上下文中。 When it tries to use what it captured from your app.js file, you get the error that _JSHome is not defined because in the context where r.js interprets your configuration , _JSHome is not defined. 当它尝试使用从app.js文件中捕获的内容时,会收到_JSHome的错误,因为r.js解释您的配置的上下文中_JSHome

The simple solution would be to have a configuration which is not computed. 简单的解决方案是拥有一个不计算的配置。 However, if you need to have a computed configuration, then to get r.js to work with it, it needs to be computed before r.js sees it. 但是,如果需要计算的配置,则r.js可以使用它,需要在r.js看到它之前r.js进行计算。 This could be done as part of a build system (with Grunt, Gulp, make , etc.). 这可以作为构建系统的一部分(使用Grunt,Gulp, make等)完成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM