Nuxt.js lets you customize the webpack configuration for building your web application as you want.

analyze

Nuxt.js use webpack-bundle-analyzer to let you visualize your bundles and how to optimize them.

  • Type: Boolean or Object
  • Default: false

If an object, see available properties here.

nuxt.config.js

  1. export default {
  2. build: {
  3. analyze: true,
  4. // or
  5. analyze: {
  6. analyzerMode: 'static'
  7. }
  8. }
  9. }

build - 图1

Info: you can use the command yarn nuxt build --analyze or yarn nuxt build -a to build your application and launch the bundle analyzer on http://localhost:8888. If you are not using yarn you can run the command with npx.

corejs

As of Nuxt@2.14 Nuxt automatically detects the current version of core-js in your project, also you can specify which version you want to use.

  • Type: number | string (Valid values are 'auto', 2 and 3)
  • Default: 'auto'

babel

Customize Babel configuration for JavaScript and Vue files. .babelrc is ignored by default.

  • Type: Object

  • See babel-loader options and babel options

  • Default:

    1. {
    2. babelrc: false,
    3. cacheDirectory: undefined,
    4. presets: ['@nuxt/babel-preset-app']
    5. }

The default targets of @nuxt/babel-preset-app are ie: '9' in the client build, and node: 'current' in the server build.

presets

  • Type: Function
  • Argument:
    1. Object: { isServer: true | false }
    2. Array:
      • preset name @nuxt/babel-preset-app
      • options of @nuxt/babel-preset-app

Note: The presets configured in build.babel.presets will be applied to both, the client and the server build. The target will be set by Nuxt accordingly (client/server). If you want configure the preset differently for the client or the server build, please use presets as a function:

We highly recommend to use the default preset instead of below customization

  1. export default {
  2. build: {
  3. babel: {
  4. presets({ isServer }, [ preset, options ]) {
  5. // change options directly
  6. options.targets = isServer ? ... : ...
  7. options.corejs = ...
  8. // return nothing
  9. }
  10. }
  11. }
  12. }

Or override default value by returning whole presets list:

  1. export default {
  2. build: {
  3. babel: {
  4. presets({ isServer }, [preset, options]) {
  5. return [
  6. [
  7. preset,
  8. {
  9. targets: isServer ? ... : ...,
  10. ...options
  11. }
  12. ],
  13. [
  14. // Other presets
  15. ]
  16. ]
  17. }
  18. }
  19. }
  20. }

cache

  • Type: Boolean
  • Default: false
  • ⚠️ Experimental

Enable cache of terser-webpack-plugin and cache-loader

cssSourceMap

  • Type: boolean
  • Default: true for dev and false for production.

Enables CSS Source Map support

devMiddleware

  • Type: Object

See webpack-dev-middleware for available options.

devtools

  • Type: boolean
  • Default: false

Configure whether to allow vue-devtools inspection.

If you already activated through nuxt.config.js or otherwise, devtools enable regardless of the flag.

extend

Extend the webpack configuration manually for the client & server bundles.

  • Type: Function

The extend is called twice, one time for the server bundle, and one time for the client bundle. The arguments of the method are:

  1. The Webpack config object,
  2. An object with the following keys (all boolean except loaders): isDev, isClient, isServer, loaders.

build - 图2

Warning: The isClient and isServer keys provided in are separate from the keys available in context. They are not deprecated. Do not use process.client and process.server here as they are undefined at this point.

nuxt.config.js

  1. export default {
  2. build: {
  3. extend(config, { isClient }) {
  4. // Extend only webpack config for client-bundle
  5. if (isClient) {
  6. config.devtool = 'source-map'
  7. }
  8. }
  9. }
  10. }

If you want to see more about our default webpack configuration, take a look at our webpack directory.

loaders in extend

loaders has the same object structure as build.loaders, so you can change the options of loaders inside extend.

nuxt.config.js

  1. export default {
  2. build: {
  3. extend(config, { isClient, loaders: { vue } }) {
  4. // Extend only webpack config for client-bundle
  5. if (isClient) {
  6. vue.transformAssetUrls.video = ['src', 'poster']
  7. }
  8. }
  9. }
  10. }

extractCSS

Enables Common CSS Extraction using Vue Server Renderer guidelines.

  • Type: Boolean or Object
  • Default: false

Using extract-css-chunks-webpack-plugin under the hood, all your CSS will be extracted into separate files, usually one per component. This allows caching your CSS and JavaScript separately and is worth a try in case you have a lot of global or shared CSS.

Example (nuxt.config.js):

  1. export default {
  2. build: {
  3. extractCSS: true,
  4. // or
  5. extractCSS: {
  6. ignoreOrder: true
  7. }
  8. }
  9. }

build - 图3

Note: There was a bug prior to Vue 2.5.18 that removed critical CSS imports when using this options.

You may want to extract all your CSS to a single file. There is a workaround for this:

build - 图4

It is not recommended to extract everything into a single file. Extracting into multiple CSS files is better for caching and preload isolation. It can also improve page performance by downloading and resolving only those resources that are needed.

  1. export default {
  2. build: {
  3. extractCSS: true,
  4. optimization: {
  5. splitChunks: {
  6. cacheGroups: {
  7. styles: {
  8. name: 'styles',
  9. test: /\.(css|vue)$/,
  10. chunks: 'all',
  11. enforce: true
  12. }
  13. }
  14. }
  15. }
  16. }
  17. }

filenames

Customize bundle filenames.

  • Type: Object

  • Default:

    1. {
    2. app: ({ isDev, isModern }) => isDev ? `[name]${isModern ? '.modern' : ''}.js` : `[contenthash:7]${isModern ? '.modern' : ''}.js`,
    3. chunk: ({ isDev, isModern }) => isDev ? `[name]${isModern ? '.modern' : ''}.js` : `[contenthash:7]${isModern ? '.modern' : ''}.js`,
    4. css: ({ isDev }) => isDev ? '[name].css' : 'css/[contenthash:7].css',
    5. img: ({ isDev }) => isDev ? '[path][name].[ext]' : 'img/[name].[contenthash:7].[ext]',
    6. font: ({ isDev }) => isDev ? '[path][name].[ext]' : 'fonts/[name].[contenthash:7].[ext]',
    7. video: ({ isDev }) => isDev ? '[path][name].[ext]' : 'videos/[name].[contenthash:7].[ext]'
    8. }

This example changes fancy chunk names to numerical ids:

nuxt.config.js

  1. export default {
  2. build: {
  3. filenames: {
  4. chunk: ({ isDev }) => (isDev ? '[name].js' : '[id].[contenthash].js')
  5. }
  6. }
  7. }

To understand a bit more about the use of manifests, take a look at this webpack documentation.

build - 图5

Be careful when using non-hashed based filenames in production as most browsers will cache the asset and not detect the changes on first load.

friendlyErrors

  • Type: Boolean
  • Default: true (Overlay enabled)

Enables or disables the overlay provided by FriendlyErrorsWebpackPlugin

hardSource

  • Type: Boolean
  • Default: false
  • ⚠️ Experimental

Enables the HardSourceWebpackPlugin for improved caching

hotMiddleware

  • Type: Object

See webpack-hot-middleware for available options.

html.minify

  • Type: Object
  • Default:
  1. {
  2. collapseBooleanAttributes: true,
  3. decodeEntities: true,
  4. minifyCSS: true,
  5. minifyJS: true,
  6. processConditionalComments: true,
  7. removeEmptyAttributes: true,
  8. removeRedundantAttributes: true,
  9. trimCustomFragments: true,
  10. useShortDoctype: true
  11. }

Attention: If you make changes to html.minify, they won’t be merged with the defaults!

Configuration for the html-minifier plugin used to minify HTML files created during the build process (will be applied for all modes).

indicator

Display build indicator for hot module replacement in development (available in v2.8.0+)

  • Type: Boolean
  • Default: true

nuxt-build-indicator

loaders

Customize options of Nuxt.js integrated webpack loaders.

  • Type: Object
  • Default:
  1. {
  2. file: {},
  3. fontUrl: { limit: 1000 },
  4. imgUrl: { limit: 1000 },
  5. pugPlain: {},
  6. vue: {
  7. transformAssetUrls: {
  8. video: 'src',
  9. source: 'src',
  10. object: 'src',
  11. embed: 'src'
  12. }
  13. },
  14. css: {},
  15. cssModules: {
  16. localIdentName: '[local]_[hash:base64:5]'
  17. },
  18. less: {},
  19. sass: {
  20. indentedSyntax: true
  21. },
  22. scss: {},
  23. stylus: {},
  24. vueStyle: {}
  25. }

Note: In addition to specifying the configurations in nuxt.config.js, it can also be modified by build.extend

loaders.file

More details are in file-loader options.

loaders.fontUrl and loaders.imgUrl

More details are in url-loader options.

loaders.pugPlain

More details are in pug-plain-loader or Pug compiler options.

loaders.vue

More details are in vue-loader options.

loaders.css and loaders.cssModules

More details are in css-loader options. Note: cssModules is loader options for usage of CSS Modules

loaders.less

You can pass any Less specific options to the less-loader via loaders.less. See the Less documentation for all available options in dash-case.

loaders.sass and loaders.scss

See the Sass documentation for all available Sass options. Note: loaders.sass is for Sass Indented Syntax

loaders.vueStyle

More details are in vue-style-loader options.

optimization

  • Type: Object

  • Default:

    1. {
    2. minimize: true,
    3. minimizer: [
    4. // terser-webpack-plugin
    5. // optimize-css-assets-webpack-plugin
    6. ],
    7. splitChunks: {
    8. chunks: 'all',
    9. automaticNameDelimiter: '.',
    10. name: undefined,
    11. cacheGroups: {}
    12. }
    13. }

The default value of splitChunks.name is true in dev or analyze mode.

You can set minimizer to a customized Array of plugins or set minimize to false to disable all minimizers. (minimize is being disabled for development by default)

See Webpack Optimization.

optimizeCSS

  • Type: Object or Boolean
  • Default:
    • false
    • {} when extractCSS is enabled

OptimizeCSSAssets plugin options.

See NMFR/optimize-css-assets-webpack-plugin.

parallel

  • Type: Boolean
  • Default: false
  • ⚠️ Experimental

Enable thread-loader in webpack building

plugins

Add webpack plugins

  • Type: Array
  • Default: []

nuxt.config.js

  1. import webpack from 'webpack'
  2. import { version } from './package.json'
  3. export default {
  4. build: {
  5. plugins: [
  6. new webpack.DefinePlugin({
  7. 'process.VERSION': version
  8. })
  9. ]
  10. }
  11. }

postcss

Customize PostCSS Loader plugins.

  • Type: Array (legacy, will override defaults), Object (recommended), Function or Boolean

    Note: Nuxt.js has applied PostCSS Preset Env. By default it enables Stage 2 features and Autoprefixer, you can use build.postcss.preset to configure it.

  • Default:

    nuxt.config.js

    1. {
    2. plugins: {
    3. 'postcss-import': {},
    4. 'postcss-url': {},
    5. 'postcss-preset-env': this.preset,
    6. 'cssnano': { preset: 'default' } // disabled in dev mode
    7. },
    8. order: 'presetEnvAndCssnanoLast',
    9. preset: {
    10. stage: 2
    11. }
    12. }

Your custom plugin settings will be merged with the default plugins (unless you are using an Array instead of an Object).

nuxt.config.js

  1. export default {
  2. build: {
  3. postcss: {
  4. plugins: {
  5. // Disable `postcss-url`
  6. 'postcss-url': false,
  7. // Add some plugins
  8. 'postcss-nested': {},
  9. 'postcss-responsive-type': {},
  10. 'postcss-hexrgba': {}
  11. },
  12. preset: {
  13. autoprefixer: {
  14. grid: true
  15. }
  16. }
  17. }
  18. }
  19. }

If the postcss configuration is an Object, order can be used for defining the plugin order:

  • Type: Array (ordered plugin names), String (order preset name), Function
  • Default: cssnanoLast (put cssnano in last)

nuxt.config.js

  1. export default {
  2. build: {
  3. postcss: {
  4. // preset name
  5. order: 'cssnanoLast',
  6. // ordered plugin names
  7. order: ['postcss-import', 'postcss-preset-env', 'cssnano']
  8. // Function to calculate plugin order
  9. order: (names, presets) => presets.cssnanoLast(names)
  10. }
  11. }
  12. }

postcss plugins & @nuxtjs/tailwindcss

If you want to apply a postcss plugin (e.g. postcss-pxtorem) on the @nuxtjs/tailwindcss configuration, you have to change order and load tailwindcss first.

This setup has no impact on nuxt-purgecss.

nuxt.config.js

  1. import { join } from 'path'
  2. export default {
  3. // ...
  4. build: {
  5. postcss: {
  6. plugins: {
  7. tailwindcss: join(__dirname, 'tailwind.config.js'),
  8. 'postcss-pxtorem': {
  9. propList: ['*', '!border*']
  10. }
  11. }
  12. }
  13. }
  14. }

profile

  • Type: Boolean
  • Default: enabled by command line argument --profile

Enable the profiler in WebpackBar

publicPath

Nuxt.js lets you upload your dist files to your CDN for maximum performances, simply set the publicPath to your CDN.

  • Type: String
  • Default: '/_nuxt/'

nuxt.config.js

  1. export default {
  2. build: {
  3. publicPath: 'https://cdn.nuxtjs.org'
  4. }
  5. }

Then, when launching nuxt build, upload the content of .nuxt/dist/client directory to your CDN and voilà!

In Nuxt 2.15+, changing the value of this property at runtime will override the configuration of an app that has already been built.

quiet

Suppresses most of the build output log

  • Type: Boolean
  • Default: Enabled when a CI or test environment is detected by std-env

splitChunks

  • Type: Object

  • Default:

    nuxt.config.js

    1. export default {
    2. build: {
    3. splitChunks: {
    4. layouts: false,
    5. pages: true,
    6. commons: true
    7. }
    8. }
    9. }

If split codes for layout, pages and commons (common libs: vue|vue-loader|vue-router|vuex…).

ssr

Creates special webpack bundle for SSR renderer.

  • Type: Boolean
  • Default: true for universal mode and false for spa mode

This option is automatically set based on mode value if not provided.

standalone

Inline server bundle dependencies (advanced)

  • Type: Boolean
  • Default: false

This mode bundles node_modules that are normally preserved as externals in the server build (more information).

build - 图7

* Warning : Runtime dependencies (modules, nuxt.config , server middleware and static directory) are not bundled. This feature only disables use of webpack-externals for server-bundle.

build - 图8

Info: you can use the command yarn nuxt build --standalone to enable this mode on the command line. (If you are not using yarn you can run the command with npx .)

styleResources

  • Type: Object
  • Default: {}

build - 图9

Warning: This property is deprecated. Please use the style-resources-module instead for improved performance and better DX!

This is useful when you need to inject some variables and mixins in your pages without having to import them every time.

Nuxt.js uses https://github.com/yenshih/style-resources-loader to achieve this behavior.

You need to specify the patterns/path you want to include for the given pre-processors: less, sass, scss or stylus

You cannot use path aliases here (~ and @), you need to use relative or absolute paths.

nuxt.config.js

  1. {
  2. build: {
  3. styleResources: {
  4. scss: './assets/variables.scss',
  5. less: './assets/*.less',
  6. // sass: ...,
  7. // scss: ...
  8. options: {
  9. // See https://github.com/yenshih/style-resources-loader#options
  10. // Except `patterns` property
  11. }
  12. }
  13. }
  14. }

templates

Nuxt.js allows you provide your own templates which will be rendered based on Nuxt configuration. This feature is specially useful for using with modules.

  • Type: Array

nuxt.config.js

  1. export default {
  2. build: {
  3. templates: [
  4. {
  5. src: '~/modules/support/plugin.js', // `src` can be absolute or relative
  6. dst: 'support.js', // `dst` is relative to project `.nuxt` dir
  7. options: {
  8. // Options are provided to template as `options` key
  9. live_chat: false
  10. }
  11. }
  12. ]
  13. }
  14. }

Templates are rendered using lodash.template you can learn more about using them here.

terser

  • Type: Object or Boolean
  • Default:

nuxt.config.js

  1. {
  2. parallel: true,
  3. cache: false,
  4. sourceMap: false,
  5. extractComments: {
  6. filename: 'LICENSES'
  7. },
  8. terserOptions: {
  9. output: {
  10. comments: /^\**!|@preserve|@license|@cc_on/
  11. }
  12. }
  13. }

Terser plugin options. Set to false to disable this plugin.

Enabling sourceMap will leave //# sourceMappingURL linking comment at the end of each output file if webpack config.devtool is set to source-map.

See webpack-contrib/terser-webpack-plugin.

transpile

  • Type: Array<String | RegExp | Function>
  • Default: []

If you want to transpile specific dependencies with Babel, you can add them in build.transpile. Each item in transpile can be a package name, a string or regex object matching the dependency’s file name.

Starting with v2.9.0, you can also use a function to conditionally transpile, the function will receive a object ({ isDev, isServer, isClient, isModern, isLegacy }):

nuxt.config.js

  1. {
  2. build: {
  3. transpile: [({ isLegacy }) => isLegacy && 'ky']
  4. }
  5. }

vueLoader

Note: This config has been removed since Nuxt 2.0, please use build.loaders.vueinstead.

  • Type: Object

  • Default:

    nuxt.config.js

    1. {
    2. productionMode: !this.options.dev,
    3. transformAssetUrls: {
    4. video: 'src',
    5. source: 'src',
    6. object: 'src',
    7. embed: 'src'
    8. }
    9. }

Specify the Vue Loader Options.

watch

You can provide your custom files to watch and regenerate after changes. This feature is specially useful for using with modules.

  • Type: Array<String>

nuxt.config.js

  1. export default {
  2. build: {
  3. watch: ['~/.nuxt/support.js']
  4. }
  5. }

followSymlinks

By default, the build process does not scan files inside symlinks. This boolean includes them, thus allowing usage of symlinks inside folders such as the “pages” folder, for example.

  • Type: Boolean

nuxt.config.js

  1. export default {
  2. build: {
  3. followSymlinks: true
  4. }
  5. }