definePlugin在create-react-app样板中的react webpack.config.js中不起作用

时间:2019-12-02 13:47:26

标签: reactjs webpack

我正在尝试根据环境设置前端的服务URL。

到目前为止我一直在尝试。

    const currentPath =  path.join(__dirname);
    const basePath =  currentPath +   ".env";
    const envPath = basePath +"."+ process.env.NODE_ENV;
    const finalPath = fs.existsSync(envPath) ? envPath : basePath;

    const fileEnv = dotenv.config({ path: finalPath }).parsed;
    let envKeys = Object.keys(fileEnv).reduce((prev, next) => {
        prev[`process.env.${next}`] = JSON.stringify(fileEnv[next]);
        return prev;
        }, {});


//in the plugins array

new webpack.DefinePlugin(envKeys)

1 个答案:

答案 0 :(得分:0)

键不应该字符串化

    let envKeys = Object.keys(fileEnv).reduce((prev, next) => {
        prev.process.env[next] = JSON.stringify(fileEnv[next]);
        return prev;
    }, {process: {env: {}}});