dan*_*ast 6 ember.js ember-cli
当您这样做时:
ember build --environment="production"
Run Code Online (Sandbox Code Playgroud)
该environment参数可用于config/environment.js:
module.exports = function(environment) {
...
};
Run Code Online (Sandbox Code Playgroud)
我的应用有一些要求取决于环境的要求prepend(在中ember-cli-build.js):
var STATIC_URL = "TODO"; // This depends on the deploy "environment" parameter
module.exports = function(defaults) {
var app = new EmberApp(defaults, {
fingerprint: {
enabled: true,
prepend: STATIC_URL,
},
});
return app.toTree();
};
Run Code Online (Sandbox Code Playgroud)
如何从中访问环境参数ember-cli-build.js?
我的版本:
» ember --version
version: 1.13.13
node: 0.12.2
npm: 2.14.10
os: linux x64
Run Code Online (Sandbox Code Playgroud)
在我们的Brocfile.js中(我想您的名字叫ember-cli-build.js?),我们正在做这样的事情:
var EmberApp = require('ember-cli/broccoli/ember-app');
var environment = process.env.EMBER_ENV || 'development';
var config = require('./config/environment')(environment);
var app = new EmberApp(/* configuration for the app... */ );
module.exports = app.toTree();
Run Code Online (Sandbox Code Playgroud)
我们分配给环境变量的那一行是如何获取您所处的环境。我们使用EMBER_ENV命令行变量,但可以使用其他变量。基本上在我们所有的代码中,我们都是这样运行ember的:
EMBER_ENV=production ember-cli start
EMBER_ENV=test ember-cli test
# the next lines use the same 'development' environment
EMBER_ENV=development ember-cli start
ember-cli start
Run Code Online (Sandbox Code Playgroud)
我正在使用Ember 2.5。要从ember-cli-build.js访问环境参数,请使用process.env.EMBER_ENV。这是我的ember-cli-build.js:
let EmberApp = require('ember-cli/lib/broccoli/ember-app');
module.exports = function(defaults) {
let app = getApp(defaults, process.env.EMBER_ENV);
// Use `app.import` to add additional libraries to the generated
// output files.
return app.toTree();
};
function getApp(defaults, environment) {
switch(environment) {
case "production":
return new EmberApp(defaults, {
fingerprint: {
enabled: true
},
});
default:
return new EmberApp(defaults, {
fingerprint: {
enabled: false
},
});
}
}
Run Code Online (Sandbox Code Playgroud)
编辑:
我刚刚意识到您需要 ember-cli-build.js 中已有的环境,而不仅仅是 app.js 中的环境,因此这个答案可能不起作用。无论如何,我会把它贴出来,也许会有帮助!
我的配置有点不同,但环境的包含是相同的:
// app.js - I stripped some unrelated stuff
import Ember from 'ember';
import Resolver from 'ember/resolver';
import ENV from 'my-appname/config/environment';
var App;
App = Ember.Application.extend({
fingerprint: {
enabled: true,
prepend: ENV.STATIC_URL,
},
modulePrefix: config.modulePrefix,
podModulePrefix: config.podModulePrefix,
Resolver: Resolver,
});
export default App;
Run Code Online (Sandbox Code Playgroud)
现在,您可以根据您传递到的环境更改 enviroment.js 中的 STATIC_URL:
// config/enviroment.js
module.exports = function(environment) {
var ENV;
if(environment==='production') {
ENV.STATIC_URL='foo';
}
return ENV;
}
Run Code Online (Sandbox Code Playgroud)
请注意,配置/环境位于您的破折号应用程序名称下。