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

后端 未结 4 963
没有蜡笔的小新
没有蜡笔的小新 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: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.

提交回复
热议问题