停止WebPack替换AWS Lambda的所有process.env.VARNAME示例

nwlls2ji  于 2023-11-19  发布在  Webpack
关注(0)|答案(1)|浏览(130)

我有一个JavaScript AWS Lambda函数,我在部署前进行了WebPacking。我看到的问题是,我的应用程序中的所有process.env.VARNAME都被替换为静态值,而不是在运行时使用的Lambda环境变量的剩余变量。
根据我的研究,我认为这与DefinePlugin的默认行为有关,但我不确定如何禁用DefinePlugin。有人知道如何解决这个问题吗?
这是我的webpack.config.js:

  1. const NodePolyfillPlugin = require("node-polyfill-webpack-plugin");
  2. const TerserPlugin = require('terser-webpack-plugin');
  3. const webpack = require('webpack');
  4. module.exports = {
  5. entry: {
  6. entry: './src/lib/index.js',
  7. },
  8. target: 'node',
  9. module: {
  10. rules: [
  11. {
  12. test: /\.ts?$/,
  13. use: 'ts-loader',
  14. exclude: /node_modules/,
  15. }
  16. ]
  17. },
  18. resolve: {
  19. extensions: ['.tsx', '.ts', '.js'],
  20. fallback: {
  21. "fs": false,
  22. "tls": false,
  23. "net": false,
  24. "dns": false,
  25. "child_process": false,
  26. "dtrace-provider": false
  27. }
  28. },
  29. optimization: {
  30. minimize: true,
  31. minimizer: [
  32. new TerserPlugin({
  33. terserOptions: {
  34. keep_classnames: true,
  35. keep_fnames: true
  36. }
  37. })
  38. ]
  39. },
  40. output: {
  41. library: {
  42. type: 'commonjs-static'
  43. },
  44. filename: "lib.js",
  45. path: path.resolve(__dirname, 'build'),
  46. chunkFormat: 'commonjs'
  47. },
  48. plugins: [
  49. new NodePolyfillPlugin()
  50. ]
  51. };

字符串

7tofc5zh

7tofc5zh1#

我发现了这一点。下面是我的webpack.js.js。具体来说,webpack.DefinePlugin({ 'process. env':'process. env'})的插件条目是我需要避免的。

  1. const path = require('path')
  2. const webpack = require('webpack');
  3. module.exports = {
  4. entry: './src/index.js',
  5. target: 'node',
  6. module: {
  7. rules: [
  8. {
  9. test: /\.tsx?$/,
  10. use: 'ts-loader',
  11. exclude: /node_modules/,
  12. },
  13. ],
  14. },
  15. resolve: {
  16. extensions: ['.tsx', '.ts', '.js'],
  17. },
  18. optimization: {
  19. minimize: false,
  20. },
  21. mode: 'production',
  22. output: {
  23. filename: 'index.js',
  24. path: path.resolve(__dirname, 'build'),
  25. libraryTarget: 'commonjs2'
  26. },
  27. plugins: [
  28. new webpack.DefinePlugin({
  29. 'process.env': 'process.env',
  30. }),
  31. ]
  32. }

字符串

展开查看全部

相关问题