我正在尝试根据环境设置前端的服务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)
答案 0 :(得分:0)
键不应该字符串化
let envKeys = Object.keys(fileEnv).reduce((prev, next) => {
prev.process.env[next] = JSON.stringify(fileEnv[next]);
return prev;
}, {process: {env: {}}});