How to add webpack plugins?

In your nuxt.config.js file, under the build option, you can pass webpack plugins, the same way you would doit in a webpack.config.js file.

In this example we add the webpack built-in ProvidePluginfor automatically loading JavaScript modules (lodash and jQuery) instead of having to import or requirethem everywhere.

  1. import webpack from 'webpack'
  2. export default {
  3. build: {
  4. plugins: [
  5. new webpack.ProvidePlugin({
  6. // global modules
  7. '$': 'jquery',
  8. '_': 'lodash'
  9. })
  10. ]
  11. }
  12. }

Note: You might not need jQuery in a Vue-based app.

With Nuxt, you can also control plugins execution context: if they are meant to be run on the client or in the server builds (or differentiating dev and prod builds) within build.extend, where you can manually pass webpack plugins too.