I have gone through several solutions including the ones listed here:
- Environment Variables in an isomorphic JS app: Webpack find & replace?
- Passing environment-dependent variables in webpack
I am used to using something like gulp-replace-task
to find and update a config
file for the app to replace things like @@SERVER_URL
with something set from the environment.
That way I can do export SERVER_URL=something
or run the script with SERVER_URL=something gulp build
to set the configuration.
I've tried all of the following:
Using the transform-loader
plus envify
This is a suggestion from the first question, but it does not work for me because of:
Module build failed: Error: Parse Error: Line 1: Illegal import declaration
at throwError (ngapp/node_modules/esprima-fb/esprima.js:2823:21)
Seems like esprima-fb
is using an import
declaration that Webpack can't use for some reason or another. The project is no longer maintained either, so this may be the wrong road to go down.
Using DefinePlugin
I've added:
module: {plugins: [new webpack.DefinePlugin({"process.env.SERVER_URL": "something"})]}
This seems to be ignored, or at least process.env.SERVER_URL
does not get interpolated in my typescript files. When I console.log(process.env)
, it emits an empty object.
Setting using --define
for webpack
I updated my npm
script:
"start": "webpack-dev-server --define process.env.SERVER_URL=${SERVER_URL}"
However this just ends up replacing process.env.SERVER_URL
in my code with a literal "${SERVER_URL}"
rather than being interpolated in the npm script.
Is there a simple / convenient (or at this point really any) way to use environment variables in TypeScript apps built with Webpack?
My Webpack setup is essentially what is listed in the Angular docs.