简体   繁体   中英

How to include and use DefinePlugin in webpack config?

Hi there i am trying to use the define plugin so i can update the version number to make sure my JS refreshes after releasing a new build. I can't seem to get DefinePlugin to work properly though. I see it in the folder webpack and i'm trying to follow the documentation but i get errors that it isn't found. Here is my config:

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'
}

I Have "webpack": "^4.28.4" and define in webpack config

new webpack.DefinePlugin({
 PRODUCTION: JSON.stringify(true),
});

if you console that variables, you don't find it. I use in conditional

if (PRODUCTION) {
 //do stuff
}

Another case is to set globals variables in a object and share with webpack.

here is an example

        new webpack.ProvidePlugin({
        CONFIG: path.resolve(__dirname, './CONSTS.js')
        }),
        // the path is src/CONST.JS

In the eslintrc file you can add that variables to avoid import errors.

    "settings": {
     "import/resolver": {
      "webpack": {
       "config": "webpack.dev.js"
      }
     }
    }

then in any file you can use import {value} from 'CONFIG'

{
 "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"
   }
  }
 }
}

That's my eslintrc. This is for use absolute imports created in your webpack config with the modules alias. You need to install eslint-import-resolver-webpack

If you are using laravel mix, you can place that new webpack.DefinePlugin code into the plugins array of your .webpackConfig block:

webpack.mix.js:

mix
.webpackConfig({
    devtool: 'source-map',
    resolve: {
        alias: {
            'sass': path.resolve('resources/sass'),
        }
    },
    plugins: [
        new webpack.ProvidePlugin({
        'window.Quill': 'quill',     // <--------------------- this right here
        __VERSION__: JSON.stringify('12345')
        })
    ]
})
.js('resources/js/app.js', 'public/js')
.sass('resources/sass/app.scss', 'public/css')
.copy([
    'resources/fonts/*',
], 'public/fonts');

By extrapolation, that means you can also add this code to the similar block in your regular (not laravel mix) webpack config.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM