Passing environment-dependent variables in webpack

后端 未结 15 1919
醉酒成梦
醉酒成梦 2020-11-22 12:46

I\'m trying to convert an angular app from gulp to webpack. in gulp I use gulp-preprocess to replace some variables in the html page (e.g. database name) depending on the NO

15条回答
  •  感情败类
    2020-11-22 13:04

    Here is a way that has worked for me and has allowed me keep my environment variables DRY by reusing a json file.

    const webpack = require('webpack');
    let config = require('./settings.json');
    if (__PROD__) {
        config = require('./settings-prod.json');
    }
    
    const envVars = {};
    Object.keys(config).forEach((key) => {
        envVars[key] = JSON.stringify(config[key]);
    });
    
    new webpack.DefinePlugin({
        'process.env': envVars
    }),
    

提交回复
热议问题