问题
Our Ember application is developed in 2.8 version and we follow POD structure.
We are generating two different builds as below
1) For our dev server : No change in code,just do build
2) For our test server: Delete a section in HBS and also remove a route from router.js then do build
And we take build using "npm run-scripts build" which is configured in our package.json as below
"scripts": {
"build": "ember build",
"start": "ember server",
"test" : "ember test"
},
I would like to know in ember 2.8v can i have a condition written to remove the section based on build.
Like if i give npm run-scripts buildDev it will do the regular build
and if i give npm run-scripts buildTest it will do sections removals and give a build
but in package.json both will be configured like
"scripts": {
"build" : "ember build",
"buildDev" : "ember build --environment=production",
"buildTest": "ember build --environment=production",
"start": "ember server",
"test" : "ember test"
},
回答1:
Do you only want to disable a section or do you need to remove it from the build?
Its pretty easy if you want to only disable something:
First you can use environment variables in your config/environment.js. So something like this:
if(process.env.DEV_BUILD) {
ENV.disableSomething = true;
}
In your package.json you could do this:
"build": "cross-env DEV_BUILD=1 ember build",
This uses cross-env to set the env variable in windows and *nix systems.
You can then import the config and use it:
import ENV from 'hss/config/environment';
...
if(!ENV.disableSomething) {
this.route('foo');
}
...
However if you want to actually remove some code you could write your own addon for this to replace some variable with true or false. then the minifier will remove the code.
来源:https://stackoverflow.com/questions/62423370/how-to-hide-a-section-in-hbs-and-js-while-taking-an-ember-production-build