Search code examples
angularwebpackservice-workerangular-compiler

Parameter passed in angular import opts ignored


Since I want to load my @angular/service-worker only in production mode, I have added the following to my webpack config:

new webpack.DefinePlugin({
   'process.env': {
        'NODE_ENV': JSON.stringify(isDevBuild ? 'dev' : 'prod')
    }
})

However, if I try to use it as

imports: [
    ServiceWorkerModule.register('/ngsw-worker.js', { enabled: (process.env.NODE_ENV === 'prod') })
]

then it works as expected in dev mode (i.e. if I make the equality to 'dev' then it is used and not otherwise), but in prod mode, the service worker never loads and there is no compile-time or runtime error. Moreover, if I log it to console then the value is true as expected and the service worker loads if I set enabled:true.

I'm using this in my webpack config

new AngularCompilerPlugin({
    tsConfigPath: './tsconfig.json',
    entryModule: path.join(__dirname, 'ClientApp/app/app.module#AppModule')
})

Solution

  • Managed to work around this by using

    new webpack.DefinePlugin({
        'process.env':{
            'NODE_ENV': JSON.stringify(isDevBuild ? 'dev' : 'prod'),
            'ISDEV': isDevBuild ? true : false,
         }
    }),
    

    and using the ISDEVparameter instead. For some reason, the AOT compiler seems to work improperly with such comparisons. I would love to know the reason!