API: The build Property

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.

Example (nuxt.config.js):

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

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.

babel

Customize Babel configuration for JavaScript and Vue files. .babelrc is ignored by 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:
    • Object: { isServer: true | false }
    • 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. buildTarget: isServer ? 'server' : 'client',
  9. ...options
  10. }],
  11. [
  12. // Other presets
  13. ]
  14. ]
  15. }
  16. }
  17. }
  18. }

cache

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

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

crossorigin

  • Type: String

  • Default: undefined

Configure the crossorigin attribute on <link rel="stylesheet"> and <script> tags in generated HTML.

More Info: CORS settings attributes

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:

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

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.

Example (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.

Example (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
  • 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.

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:

⚠️ It is not recommended extracting 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 }) => isDev ? '[name].js' : '[chunkhash].js',
  3. chunk: ({ isDev }) => isDev ? '[name].js' : '[chunkhash].js',
  4. css: ({ isDev }) => isDev ? '[name].css' : '[contenthash].css',
  5. img: ({ isDev }) => isDev ? '[path][name].[ext]' : 'img/[hash:7].[ext]',
  6. font: ({ isDev }) => isDev ? '[path][name].[ext]' : 'fonts/[hash:7].[ext]',
  7. video: ({ isDev }) => isDev ? '[path][name].[ext]' : 'videos/[hash: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].[chunkhash].js'
  5. }
  6. }
  7. }

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

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 minifyHTML 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 Node 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: []

Example (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:
  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).

Example (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)

Example (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 & nuxt-tailwindcss

If you want to apply postcss plugin (eg. postcss-pxtorem) on the nuxt-tailwindcss configuration, you have to change order and load first tailwindcss.

This setup have no impact on the nuxt-purgecss.

Example (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: [
  10. '*',
  11. '!border*',
  12. ]
  13. }
  14. }
  15. }
  16. }
  17. }

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

Example (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à!

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:

  1. {
  2. layouts: false,
  3. pages: true,
  4. commons: true
  5. }

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.

styleResources

  • Type: Object
  • Default: {}

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 behaviour.

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

Example (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: { // Options are provided to template as `options` key
  8. live_chat: false
  9. }
  10. }
  11. ]
  12. }
  13. }

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

terser

  • Type: Object or Boolean
  • Default:
  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.

sourceMap will be enabled when webpack config.devtool matches 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 conditionnaly transpile, the function will receive a object ({ isDev, isServer, isClient, isModern, isLegacy }):

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

vueLoader

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

  • Type: Object

  • Default:

  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>
  1. export default {
  2. build: {
  3. watch: [
  4. '~/.nuxt/support.js'
  5. ]
  6. }
  7. }

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
  1. export default {
  2. build: {
  3. followSymlinks: false
  4. }
  5. }