Is it possible to somehow "mark" a piece of code so it will be included in "dev" mode but will be omitted in "prod".
I know angular has:
if (isDevMode()) {...}
But can I avoid the code checking whether it is dev mode as well?
Is it possible to somehow "mark" a piece of code so it will be included in "dev" mode but will be omitted in "prod".
I know angular has:
if (isDevMode()) {...}
But can I avoid the code checking whether it is dev mode as well?
The only thing that comes to mind is using the environment.ts for that.
In your environment.dev.ts:
export const environment = {
doSomething() {
// Do your dev stuff here
}
}
In your environment.prod.ts:
export const environment = {
doSomething() {
// Leave empty
}
}
And in your component/service/wherever call it from environment. Since environment will be replaced, the dev code will only be included in your dev bundle.
You could use a build tool like webpack and have it include files based on an alias combined with the NODE_ENV
. NODE_ENV
is development
in dev mode and production
in prod.
The easiest way would be in your webpack.dev.config have the alias load the dev file, and in the webpack.prod.config load the production file.
// dev
module.exports = {
//...
resolve: {
alias: {
Something: path.resolve(__dirname, 'dev/foo/')
}
}
};
// prod
module.exports = {
//...
resolve: {
alias: {
Something: path.resolve(__dirname, 'prod/foo/')
}
}
};
If you wanted to use one config file you could just use a ternary in the config.
Just import your environement file in your component:
import { environment } from './environments/environment';
and in your script do :
if (!environment.production) {
.....
}