margherita pizza
margherita pizza

Reputation: 7145

VueJS Webpack where to add plugins

I want to add web pack provided Plugins to use JQuery globally. Some articles says

You need to put it in webpack.config.js under plugins:[]

But when I look at my webpack.config.js there is no such plugins object. Then I create plugins object manually under the module array.

After that when I try to run the project using npm run dev

it doesn't run. I want to add this plugin to the webpack.config.js

new webpack.ProvidePlugin({
  $: 'jquery',
  jQuery: 'jquery'
})

How do I do this? This is my webpack.config.js file

var path = require('path')
var webpack = require('webpack')

module.exports = {
  entry: './src/main.js',
  output: {
    path: path.resolve(__dirname, './dist'),
    publicPath: '/dist/',
    filename: 'build.js'
  },
  module: {
    rules: [
      {
        test: /\.css$/,
        use: [
          'vue-style-loader',
          'css-loader'
        ],
      },      {
        test: /\.vue$/,
        loader: 'vue-loader',
        options: {
          loaders: {
          }
          // other vue-loader options go here
        }
      },
      {
        test: /\.js$/,
        loader: 'babel-loader',
        exclude: /node_modules/
      },
      {
        test: /\.(png|jpg|gif|svg)$/,
        loader: 'file-loader',
        options: {
          name: '[name].[ext]?[hash]'
        }
      }
    ]
  },
  plugins[
    new webpack.ProvidePlugin({
      $: 'jquery',
      jQuery: 'jquery'
    }),
    new webpack.ProvidePlugin({
  Vue: ['vue/dist/vue.esm.js', 'default']
})


],
  resolve: {
    alias: {
      'vue$': 'vue/dist/vue.esm.js'
    },
    extensions: ['*', '.js', '.vue', '.json']
  },
  devServer: {
    historyApiFallback: true,
    noInfo: true,
    overlay: true
  },
  performance: {
    hints: false
  },
  devtool: '#eval-source-map'
}

if (process.env.NODE_ENV === 'production') {
  module.exports.devtool = '#source-map'
  // http://vue-loader.vuejs.org/en/workflow/production.html
  module.exports.plugins = (module.exports.plugins || []).concat([
    new webpack.DefinePlugin({
      'process.env': {
        NODE_ENV: '"production"'
      }
    }),
    new webpack.optimize.UglifyJsPlugin({
      sourceMap: true,
      compress: {
        warnings: false
      }
    }),
    new webpack.LoaderOptionsPlugin({
      minimize: true
    })
  ])
}

Upvotes: 1

Views: 237

Answers (2)

Linus Borg
Linus Borg

Reputation: 23968

If you take a quick look at the webpack documentation for plugins here, you would see where the plugins object belongs.

The good news is: your webpack config already looks correct - you put the plugins array as a direct child of the config, after module, instead of inside it. You only miss a : colon after plugins, which likely is a typo.

So I don't see a reason what should not work.

Upvotes: 1

Yakalent
Yakalent

Reputation: 1152

Try this in build/webpack.dev.conf.js and build/webpack.prod.conf.js:

plugins: [

  // ...

  new webpack.ProvidePlugin({
    $: 'jquery',
    jquery: 'jquery',
    'window.jQuery': 'jquery',
    jQuery: 'jquery'
  })
]

jQuery becomes globally available to all your modules:

Upvotes: 0

Related Questions