Environment Variables in an isomorphic JS app: Webpack find & replace?

后端 未结 4 931
没有蜡笔的小新
没有蜡笔的小新 2020-12-29 08:32

I\'m using webpack to bundle an isomorphic JS app (based on this example) so that the browser runs the same code as the server. Everything is running smoothly except I have

相关标签:
4条回答
  • 2020-12-29 09:04

    Note that using the DefinePlugin as suggested in the accepted answer is potentially a dangerous action as it completely exposes process.env. As Tobias commented above there's actually a plugin EnvironmentPlugin that does exactly this with an added whitelisting ability, using DefinePlugin internally.

    In your webpack.config.js:

    {
      plugins: [
        new webpack.EnvironmentPlugin([
          'NODE_ENV',
          'WHITELISTED_ENVIRONMENT_VARIABLE'
        ])
      ]
    }
    
    0 讨论(0)
  • 2020-12-29 09:09

    Yeah; looks like envify-loader was the easy solution.

    I just added the following to my webpack loaders:

    {
      test: /config\.js$/, loader: "envify-loader"
    }
    

    And the config.js (and only that file) is modified to include any referenced environment variables statically :)

    0 讨论(0)
  • 2020-12-29 09:12

    I needed a way to use the env variables set on the machine that is running the code, no the env variables of the machine building the app.

    I do not see a solution for this yet. This is what I did.

    In publicEnv.js:

    // List of the env variables you want to use on the client. Careful on what you put here!
    const publicEnv = [
      'API_URL',
      'FACEBOOK_APP_ID',
      'GA_ID'
    ];
    
    const isBrowser = typeof window !== 'undefined';
    const base = (isBrowser ? window.__ENV__ : process.env) || {};
    
    const env = {};
    for (const v of publicEnv) {
      env[v] = base[v];
    }
    export default env;
    

    In the HTML template file of the page I have:

    import publicEnv from 'publicEnv.js';
    
    ...
    
    <script>
      window.__ENV__ = ${stringify(publicEnv)};
    
      // Other things you need here...
      window.__INITIAL_STATE__ = ${stringify(initialState)};
    </script>
    

    So now I can get the value of the env variable on both frontend and backend with:

    import publicEnv from 'publicEnv.js';
    
    ...
    
    console.log("Google Analytic code is", publicEnv.GA_ID);
    

    I hope it can help.

    0 讨论(0)
  • 2020-12-29 09:25

    In your webpack.config.js, use the following preLoaders (or postLoaders),

      module: {
        preLoaders: [
          { test: /\.js$/, loader: "transform?envify" },
        ]
      }
    

    Another way using the webpack.DefinePlugin:

    plugins: [
        new DefinePlugin({
          'process.env': Object.keys(process.env).reduce(function(o, k) {
            o[k] = JSON.stringify(process.env[k]);
            return o;
          }, {})
        })
    ]
    

    NOTE: The old method using envify-loader was deprecated:

    DEPRECATED: use transform-loader + envify instead.

    0 讨论(0)
提交回复
热议问题