3

I have gone through several solutions including the ones listed here:

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.

Community
  • 1
  • 1
Explosion Pills
  • 188,624
  • 52
  • 326
  • 405
  • Have you tried `EnvironmentPlugin` mentioned in the second question you listed? – Franklin Yu Jul 04 '17 at 14:41
  • testing with console.log at runtime won't work with DefinePlugin. It appears use string matching to replace the code that gets served. Fornew webpack.DefinePlugin({MY_CONSTANT: JSON.stringify("something")} will literally replace the text MY_CONSTANT with "something" in all your TypeScript files. – Stephen Rudolph Aug 23 '17 at 04:19

0 Answers0