0

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"
  },
JalilIrfan
  • 158
  • 1
  • 14

1 Answers1

0

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.

Lux
  • 17,835
  • 5
  • 43
  • 73
  • Thanks @lux for the answer but i am not allowed to use any add-on, it should be something inbuilt or just pure javascript. – JalilIrfan Jun 25 '20 at 11:10
  • this does not use any ember addons. Also this will work without `cross-env` on `*nix` systems with `env DEV_BUILD=1 ember build`. However not on windows because the syntax to define an environment variable is different. – Lux Jun 25 '20 at 13:10
  • or are you talking about my suggestion to write an addon? Basically with "write an addon" I mean you need to write your own addon. This can be an in-repo-addon and live in the same repo. But it can use the ember addon API to do build transforms. – Lux Jun 25 '20 at 13:12
  • i meant the `cross-env` , ya i do use windows system and my ember version is 2.8. As far as i saw in ember forums it only allows three variables by default "dev","test","production" and advised not to modify or include any more. – JalilIrfan Jun 25 '20 at 14:14
  • thats about `EMBER_ENV`. And yes, I would not change that. But you can use a custom environment variable as I suggested with `DEV_BUILD` and access this environment variable inside `config/environment` to modify futher stuff. – Lux Jun 25 '20 at 16:55
  • process.env.DEV_BUILD is not possible since DEV_BUILD is defined in package.json which triggers the ember build where the environment is mentioned as "production". so in ember there is no scope for "DEV_BUILD" so what happens if i will give `npm run-scripts devBuild` which will check the package.json and will trigger the ember relevant code `ember build --environment=production` so when we do process.env.devBuild i dont think we will get any value – JalilIrfan Jun 26 '20 at 07:12