简体   繁体   English

使用ember serve上的in-repo addon config()方法更新Ember.js环境变量不会生效

[英]Updating Ember.js environment variables do not take effect using in-repo addon config() method on ember serve

My goal here is to create an auto-increment build number that updates both on ember build and ember serve. 我的目标是创建一个自动增量构建号,在ember构建和ember服务上更新。 In the end, if I can only use this on build, that's totally ok. 最后,如果我只能在构建时使用它,那就完全可以了。

I originally asked this question: In-repo addon writing public files on build causes endless build loop on serve In that I was attempting to solve this problem by writing out JSON files. 我最初问过这个问题: In-repo插件在构建时编写公共文件会导致无法在服务上构建循环因为我试图通过写出JSON文件来解决这个问题。 The problem was mostly solved, but not using ember serve. 问题基本上解决了,但没有使用ember服务。

Instead of doing that, I'm now trying to update the local environment. 我现在正在尝试更新本地环境,而不是这样做。 But this is having a similar problem with ember serve. 但这与ember服务有类似的问题。 I've got the build number incrementing fine. 我的内部版本号增加了很多。 I can use the config() method to set custom/dynamic variables in the environment. 我可以使用config()方法在环境中设置自定义/动态变量。 The problem I'm having is that the even though I can log the change in terminal when config() is called, and I can see it run on serve when files change, I don't see the changes in browser when I output Ember's ENV using ember serve. 我遇到的问题是即使我可以在调用config()时记录终端的更改,我可以看到它在文件更改时在服务器上运行,当我输出Ember时我看不到浏览器中的更改ENV使用ember服务。 Here's my addon's methods so far. 到目前为止,这是我的插件方法。

Note: the appNumberSetup() function is just reading a local json file in the project root and updating the build number. 注意:appNumberSetup()函数只是读取项目根目录中的本地json文件并更新内部版本号。 That's working fine. 这工作正常。 Anything about pubSettingsFile can be ignored, I won't be using that moving forward. 有关pubSettingsFile的任何内容都可以忽略,我不会继续使用它。

init(parent, project) {
    this._super.init && this._super.init.apply(this, arguments);
    // we need to setup env in init() so config() and prebuild()
    // will see update immediately
    this.settingsFile = path.resolve(this.appDir,  this.settingsFileName);
    this.addonPubDataPath = path.resolve(this.appDir, 'lib', this.name, 'inc', 'public', 'build-data-output');
    this.pubSettingsFile = path.resolve(this.addonPubDataPath,  this.pubSettingsFileName);
    // this only checks for .env variables and sets defaults
    this.dotEnvSetup();
    // must set this so prebuild skips processing a build number on build
    // else we get build number incremented twice on first run
    // then appNumberSetup() disables so subsequent serve preBuild() will run.
    this.skipPreBuild = true;
    this.appNumberSetup();

},
// this sends our created settings data to ENV.localBuildSettings in app
config(environment, appConfig){
    // this 'buildme' is just an experiment
    let x = `buildme${this.buildNumber}`;
    let r = {
        localBuildSettings: this.settings
    };
    r[`buildme${this.buildNumber}`] = this.buildNumber;
    this.dlog("Config ran...");
    this.dlog(JSON.stringify(r, null, 4));
    return r;
},
preBuild: function(result){
    // init() disables preBuild() here, but subsequent builds with serve still
    // run appNumberSetup() to update this.settings for env and JSON
    if(this.skipPreBuild === true){
        this.skipPreBuild = false;
    }
    else {
        // only run here after init runs
        this.appNumberSetup();
    }
    // don't do this... write file makes endless loop on serve
    // this.saveSettingsFile(this.pubSettingsFile, this.settings);

},

this.settings is a local variable in addon and it updated on build/serve, the JSON looks like this: this.settings是addon中的局部变量,它在build / serve上更新,JSON如下所示:

{
"appVersion": 911,
"appBuildNumber": 7117
}

Is there a way to update Ember's ENV with dynamic data? 有没有办法用动态数据更新Ember的ENV? (like a new build number) (比如新的内部版本号)

The addon config() appears to run on each change in ember serve, and it shows the build number in terminal output. addon config()似乎在ember serve中的每次更改时运行,它显示终端输出中的内部版本号。 But it looks like that runs after postBuild(). 但看起来它在postBuild()之后运行。 Maybe that's why I don't see the changes. 也许这就是我没有看到变化的原因。 Is there a way to update that environment during preBuild()? 有没有办法在preBuild()期间更新该环境?

I'm not sure of the specifics but ember-cli-new-version does this. 我不确定具体细节,但是ember-cli-new-version做到了这一点。 During the build stage they create a VERSION.txt file, might even do what you need already without needing to write it yourself. 在构建阶段,他们创建了一个VERSION.txt文件,甚至可以做你需要的,而不需要自己编写。

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

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