Webpacker: How to use angular enableProdMode() for RAILS_ENV=production?

Created on 28 Sep 2017  路  5Comments  路  Source: rails/webpacker

Hello, guys!

Can anybody explain, how do u use enableProdMode() in angular that depends on current environment?

Thanks!

Most helpful comment

@DmytroStepaniuk Just do it inside the angular pack like so:

import { enableProdMode } from '@angular/core'

if (process.env.ENV === 'production') {
  enableProdMode()
}

All 5 comments

@DmytroStepaniuk Just do it inside the angular pack like so:

import { enableProdMode } from '@angular/core'

if (process.env.ENV === 'production') {
  enableProdMode()
}

Sorry to ask again.

How can you get the environment variable through webpacker?
For me, I cannot find the variable process has imported to the script before.

@adamaiken89 If you put the code @gauravtiwari suggested in the /javascript/packs/app_name.js file, after the require("../app_name"); statement, it should work.

If you still can't get it to work, you can always try this hack in the index.ts file of your Angular application,

if (window.location.host !== 'localhost:3000') {
  enableProdMode();
}

@gauravtiwari I've tried adding this code to the angular pack file (app/javascript/packs/angular.js) it doesn't work, get the following error:

compiler.js:2426 Uncaught Error: Can't resolve all parameters for ApplicationModule: (?).
    at syntaxError (compiler.js:2426)
    at CompileMetadataResolver._getDependenciesMetadata (compiler.js:18979)
    at CompileMetadataResolver._getTypeMetadata (compiler.js:18872)
    at CompileMetadataResolver.getNgModuleMetadata (compiler.js:18740)
    at CompileMetadataResolver.getNgModuleSummary (compiler.js:18550)
    at compiler.js:18664
    at Array.forEach (<anonymous>)
    at CompileMetadataResolver.getNgModuleMetadata (compiler.js:18652)
    at CompileMetadataResolver.getNgModuleSummary (compiler.js:18550)
    at compiler.js:18637

Should I have put it somewhere else?

I was able to get this working with one change to the code snippet above.

process.env.ENV => process.env.NODE_ENV

if (process.env.NODE_ENV === 'production') {
  enableProdMode();
}

This should be placed where the Angular app is bootstrapped in the packs folder ie.app/javascript/packs/application.js.

Was this page helpful?
0 / 5 - 0 ratings