如何在 webpack 配置中包含和使用 DefinePlugin?

Tin*_*Zea 8 javascript npm webpack

嗨,我正在尝试使用定义插件,因此我可以更新版本号以确保我的 JS 在发布新版本后刷新。不过,我似乎无法让 DefinePlugin 正常工作。我在文件夹 webpack 中看到它,我正在尝试按照文档进行操作,但是我收到找不到它的错误。这是我的配置:

const path = require('path'),
settings = require('./settings');

const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
const webpack = require('webpack');


module.exports = {
  entry: {
    'scrollerbundled': [settings.themeLocation + "js/scroller.js"],
    'mapbundled': [settings.themeLocation + "js/shopmap.js"],
    'sculptor': [settings.themeLocation + "js/sculptor.js"]
  },
  output: {
    path: path.resolve(__dirname, settings.themeLocation + "js-dist"),
    filename: "[name].js"
  },
  module: {
    rules: [
      {
        test: /\.js$/,
        exclude: /node_modules/,
        use: {
          loader: 'babel-loader',
          options: {
            presets: ['@babel/preset-env']
          }
        }
      }
    ],
    plugins: [new webpack.DefinePlugin({
    PRODUCTION: JSON.stringify(true),
    VERSION: JSON.stringify('5fa3b9'),
  })]
  },
  optimization: {
    minimizer: [new UglifyJsPlugin({ 
            uglifyOptions: {
                mangle: true,
                output: {
                    comments: false
                }
            }
       })]
  },
  mode: 'production'
}
Run Code Online (Sandbox Code Playgroud)

小智 2

{
 "parser": "babel-eslint",
 "extends": [
    "airbnb",
    "plugin:react/recommended",
    "prettier",
    "prettier/react"
 ],
 "plugins": ["react", "import", "prettier"],
 "env": {
   "browser": true
 },
 "settings": {
 "import/resolver": {
  "webpack": {
    "config": "webpack.dev.js"
   }
  }
 }
}
Run Code Online (Sandbox Code Playgroud)

这是我的 eslintrc。这是为了使用在 webpack 配置中使用模块别名创建的绝对导入。您需要安装eslint-import-resolver-webpack