Require.js优化器和路径中的变量 [英] Require.js optimizer and variables in paths

查看:237
本文介绍了Require.js优化器和路径中的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有以下问题:我们有2个域(例如foo.de和bar.de)和不同的环境。根据什么环境和运行的域,他们需要从源服务器加载不同的文件。我的初始解决方案是:

  // channelDomain和environmentPath在此脚本上面定义
require.config({
路径:{
'fooscript':channelDomain + environmentPath
}
}

在未优化的浏览器中测试此功能的工作原理与完全相同,但每晚都会抱怨:

 code> [错误:错误:无法使用mainConfigFile /absolute/path/app/public/js/main.js 
中的配置,因为在
优化程序中运行时无法正确评估它尝试只使用一个也是有效的JSON的配置,或者不要使用
mainConfigFile,而是将需要的配置值复制到构建文件或
给出优化器的命令行参数
源解析错误:/absolute/path/app/public/js/main.js:ReferenceError:
channelDomain未定义

我尝试了很多事情,但是我正在运行出来的想法我尝试在构建文件中执行空的:事情,但是也没有。如果有人可以指出我正确的方向,我会很高兴。

解决方案

在同一个文件中使用两个require.config。优化器只会读取第一个,就像James在这里所说的那样 https:// github.com/jrburke/r.js/issues/270#issuecomment-13112859 ,它将在优化后在浏览器中工作。



所以在结束你将在main.js中有这样的东西:

  require.config({
//只需要配置对于透析器的优化
});

require.config({
路径:{
'fooscript':channelDomain + environmentPath
}
});


I have a problem getting r.js to work the way we need it to.

I have the following problem: We have 2 domains (e.g. foo.de and bar.de) and different environments. Depending on what environment and what domain they are running on they need to load a different file from their origin servers. My initial solution was this:

// channelDomain and environmentPath get defined above this script
require.config({
  paths: {
    'fooscript': channelDomain+environmentPath
  }
}

Testing this in the browser unoptimized works exactly as it should but the nightly build complained with:

[Error: Error: The config in mainConfigFile /absolute/path/app/public/js/main.js 
cannot be used because it cannot be evaluated correctly while running in the 
optimizer. 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.
Source error from parsing: /absolute/path/app/public/js/main.js: ReferenceError:
channelDomain is not defined

I tried doing lots of things but I'm running out of ideas. I tried doing the empty: thing in the build file but that didn't work either. I'd be glad if someone could point me into the right direction.

解决方案

Use two require.config in the same file. The optimizer will only read the first one, as James says here https://github.com/jrburke/r.js/issues/270#issuecomment-13112859, and it will work in the browser after optimization.

So at the end you will have something like this in main.js:

require.config({
    //only configurations needed for the transpiler's optimization
});

require.config({
  paths: {
    'fooscript': channelDomain+environmentPath
  }
});

这篇关于Require.js优化器和路径中的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆