Tintinabulator Zea
Tintinabulator Zea

Reputation: 2807

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

Upvotes: 14

Views: 37673

Answers (4)

Max S.
Max S.

Reputation: 1461

Install devtools globally

npm install -g @vue/devtools

... and try again.

If уоu have any issues try following the official instructions.

Upvotes: 0

Abraham Brookes
Abraham Brookes

Reputation: 1998

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.

Upvotes: 2

Fernando Colom
Fernando Colom

Reputation: 377

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

Upvotes: 2

Fernando Colom
Fernando Colom

Reputation: 377

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'

Upvotes: 4

Related Questions