2020-01-03 19:16:51 +01:00
---
description: Extend the default webpack config added by Next.js.
---
2019-12-23 16:07:38 +01:00
# Custom Webpack Config
2020-07-27 21:24:29 +02:00
Before continuing to add custom webpack configuration to your application make sure Next.js doesn't already support your use-case:
2020-07-27 11:46:17 +02:00
- [CSS imports ](/docs/basic-features/built-in-css-support#adding-a-global-stylesheet )
- [CSS modules ](/docs/basic-features/built-in-css-support#adding-component-level-css )
- [Sass/SCSS imports ](/docs/basic-features/built-in-css-support#sass-support )
- [Sass/SCSS modules ](/docs/basic-features/built-in-css-support#sass-support )
- [preact ](https://github.com/vercel/next.js/tree/canary/examples/using-preact )
- [Customizing babel configuration ](/docs/advanced-features/customizing-babel-config )
2019-12-23 16:07:38 +01:00
Some commonly asked for features are available as plugins:
- [@zeit/next-less ](https://github.com/zeit/next-plugins/tree/master/packages/next-less )
2020-05-27 23:51:11 +02:00
- [@next/mdx ](https://github.com/vercel/next.js/tree/canary/packages/next-mdx )
- [@next/bundle-analyzer ](https://github.com/vercel/next.js/tree/canary/packages/next-bundle-analyzer )
2019-12-23 16:07:38 +01:00
In order to extend our usage of `webpack` , you can define a function that extends its config inside `next.config.js` , like so:
```js
module.exports = {
webpack: (config, { buildId, dev, isServer, defaultLoaders, webpack }) => {
// Note: we provide webpack above so you should not `require` it
// Perform customizations to webpack config
config.plugins.push(new webpack.IgnorePlugin(/\/__tests__\//))
2020-07-27 11:46:17 +02:00
2019-12-23 16:07:38 +01:00
// Important: return the modified config
return config
},
}
```
> The `webpack` function is executed twice, once for the server and once for the client. This allows you to distinguish between client and server configuration using the `isServer` property.
The second argument to the `webpack` function is an object with the following properties:
- `buildId` : `String` - The build id, used as a unique identifier between builds
- `dev` : `Boolean` - Indicates if the compilation will be done in development
- `isServer` : `Boolean` - It's `true` for server-side compilation, and `false` for client-side compilation
- `defaultLoaders` : `Object` - Default loaders used internally by Next.js:
- `babel` : `Object` - Default `babel-loader` configuration
Example usage of `defaultLoaders.babel` :
```js
// Example config for adding a loader that depends on babel-loader
// This source was taken from the @next/mdx plugin source:
2020-05-27 23:51:11 +02:00
// https://github.com/vercel/next.js/tree/canary/packages/next-mdx
2019-12-23 16:07:38 +01:00
module.exports = {
webpack: (config, options) => {
config.module.rules.push({
test: /\.mdx/,
use: [
options.defaultLoaders.babel,
{
loader: '@mdx-js/loader',
options: pluginOptions.options,
},
],
})
return config
},
}
```
2020-01-20 21:40:20 +01:00
## Related
< div class = "card" >
< a href = "/docs/api-reference/next.config.js/introduction.md" >
< b > Introduction to next.config.js:< / b >
< small > Learn more about the configuration file used by Next.js.< / small >
< / a >
< / div >