如何在Webpack编译期间禁止替换process.env变量? [英] How to forbid replacing process.env variables during compilation in webpack?

查看:395
本文介绍了如何在Webpack编译期间禁止替换process.env变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

故事

我正在开发AWS Lambda函数,并使用webpack编译代码.

I'm developing the AWS Lambda functions and compile the code using webpack.

我已经阅读了一些文章,并且似乎在编译过程中自动替换了process.env变量.虽然很酷,但我还是想禁止这种行为.

I've read some of the articles and it seems that the process.env variables are auto replaced during compilation. Although it's cool I want to forbid this behaviour.

为什么?

因为我正在使用AWS Lambda仪表板传递环境变量.

Because I'm passing environment variables using AWS Lambda dashboard.

Webpack配置

const nodeExternals = require('webpack-node-externals')
const webpack = require('webpack')
const path = require('path')

module.exports = {
  target: 'node',
  entry: path.resolve(__dirname, 'index.ts'),
  externals: [nodeExternals()],
  devtool: 'inline-source-map',
  mode: 'production',
  module: {
    rules: [{
      test: /\.tsx?$/,
      use: [{
        loader: 'ts-loader',
        options: {
          experimentalWatchApi: true,
        },
      }],
    }]
  },
  plugins: [],
  resolve: {
    extensions: ['.tsx', '.ts', '.js']
  },
  output: {
    filename: 'index.js',
    libraryTarget: 'commonjs',
    path: path.resolve(__dirname, 'dist')
  }
}

问题

webpack编译期间是否可以禁止替换process.env变量的行为? 如果是,请帮助我达到此效果.

Is it possible to forbid the behaviour of replacing the process.env variables during webpack compilation? If yes please help me to achieve this effect.

推荐答案

mode选项>可以替换process.env.NODE_ENV:

发展

将DefinePlugin上的process.env.NODE_ENV设置为值开发. 启用NamedChunksPlugin和NamedModulesPlugin.

Sets process.env.NODE_ENV on DefinePlugin to value development. Enables NamedChunksPlugin and NamedModulesPlugin.

生产

在DefinePlugin上将process.env.NODE_ENV设置为值生产.启用 FlagDependencyUsagePlugin,FlagIncludedChunksPlugin, ModuleConcatenationPlugin,NoEmitOnErrorsPlugin, OccurrenceOrderPlugin,SideEffectsFlagPlugin和TerserPlugin.

Sets process.env.NODE_ENV on DefinePlugin to value production. Enables FlagDependencyUsagePlugin, FlagIncludedChunksPlugin, ModuleConcatenationPlugin, NoEmitOnErrorsPlugin, OccurrenceOrderPlugin, SideEffectsFlagPlugin and TerserPlugin.

退出任何默认优化选项

webpack -p CLI选项也是如此.

So does webpack -p CLI option.

如果DefinePluginprocess.env.NODE_ENV的影响不是所希望的,则列出的插件应不带DefinePlugin来应用,如文档所示 development 模式.

In case, the effect of DefinePlugin on process.env.NODE_ENV is not desirable, listed plugins should be applied without DefinePlugin, as the documentation shows for production and development modes.

这篇关于如何在Webpack编译期间禁止替换process.env变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆