How to extend webpack config?

You can extend nuxt’s webpack configuration via the extend option in your nuxt.config.js. The extend option of the build property is a method that accepts two arguments. The first argument is the webpack config object exported from nuxt’s webpack config. The second parameter is a context object with the following boolean properties: { isDev, isClient, isServer, loaders }.

  1. export default {
  2. build: {
  3. extend(config, { isDev, isClient }) {
  4. // ..
  5. config.module.rules.push({
  6. test: /\.(ttf|eot|svg|woff(2)?)(\?[a-z0-9=&.]+)?$/,
  7. loader: 'file-loader'
  8. })
  9. // Sets webpack's mode to development if `isDev` is true.
  10. if (isDev) {
  11. config.mode = 'development'
  12. }
  13. }
  14. }
  15. }

The extend method gets called twice - Once for the client bundle and the other for the server bundle.

Examples

Customize chunks configuration

You may want to tweak a bit optimization configuration, avoiding to rewrite default object.

  1. export default {
  2. build: {
  3. extend(config, { isClient }) {
  4. if (isClient) {
  5. config.optimization.splitChunks.maxSize = 200000
  6. }
  7. }
  8. }
  9. }

Execute ESLint on every webpack build in dev environment

In order to be aware of code style errors, you may want to run ESLint on every build in dev environment.

  1. export default {
  2. build: {
  3. extend(config, { isDev, isClient }) {
  4. if (isDev && isClient) {
  5. config.module.rules.push({
  6. enforce: 'pre',
  7. test: /\.(js|vue)$/,
  8. loader: 'eslint-loader',
  9. exclude: /(node_modules)/
  10. })
  11. }
  12. }
  13. }
  14. }