forked from nrwl/nx
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs(webpack): customize webpack and refactor docs (nrwl#14702)
- Loading branch information
Showing
9 changed files
with
307 additions
and
173 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 113 additions & 0 deletions
113
docs/generated/packages/webpack/documents/customize-webpack.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
# Customize your Webpack Config | ||
|
||
{% callout type="note" title="Webpack Config Setup" %} | ||
This guide helps you customize your webpack config - add extra functionality and support for other frameworks or features. To see how you can set up a basic webpack config for Nx, read the [Webpack Config Setup guide](/packages/webpack/documents/webpack-config-setup). | ||
{% /callout %} | ||
|
||
For most apps, the default configuration of webpack is sufficient, but sometimes you need to tweak a setting in your webpack config. This guide explains how to make a small change without taking on the maintenance burden of the entire webpack config. | ||
|
||
## React projects | ||
|
||
React projects use the `@nrwl/react` package to build their apps. This package provides a `withReact` plugin that adds the necessary configuration for React to work with webpack. You can use this plugin to add the necessary configuration to your webpack config. | ||
|
||
```js {% fileName="apps/my-app/webpack.config.js" %} | ||
const { composePlugins, withNx } = require('@nrwl/webpack'); | ||
const { withReact } = require('@nrwl/react'); | ||
|
||
// Nx plugins for webpack. | ||
module.exports = composePlugins( | ||
withNx(), | ||
withReact(), | ||
(config, { options, context }) => { | ||
// Update the webpack config as needed here. | ||
// e.g. config.plugins.push(new MyPlugin()) | ||
return config; | ||
} | ||
); | ||
``` | ||
|
||
## Add a Loader | ||
|
||
To add the `css-loader` to your config, install it and add the rule. | ||
|
||
{% tabs %} | ||
{% tab label="yarn" %} | ||
|
||
```shell | ||
yarn add -D css-loader | ||
``` | ||
|
||
{% /tab %} | ||
{% tab label="npm" %} | ||
|
||
```shell | ||
npm install -D css-loader | ||
``` | ||
|
||
{% /tab %} | ||
{% /tabs %} | ||
|
||
```js {% fileName="apps/my-app/webpack.config.js" %} | ||
const { composePlugins, withNx } = require('@nrwl/webpack'); | ||
const { merge } = require('webpack-merge'); | ||
|
||
module.exports = composePlugins(withNx(), (config, { options, context }) => { | ||
return merge(config, { | ||
module: { | ||
rules: [ | ||
{ | ||
test: /\.css$/i, | ||
use: ['style-loader', 'css-loader'], | ||
}, | ||
], | ||
}, | ||
}); | ||
}); | ||
``` | ||
|
||
## Module Federation | ||
|
||
If you use the [Module Federation](/recipes/module-federation/faster-builds) support from `@nrwl/angular` or `@nrwl/react` then | ||
you can customize your webpack configuration as follows. | ||
|
||
```js {% fileName="apps/my-app/webpack.config.js" %} | ||
const { composePlugins, withNx } = require('@nrwl/webpack'); | ||
const { merge } = require('webpack-merge'); | ||
const withModuleFederation = require('@nrwl/react/module-federation'); | ||
// or `const withModuleFederation = require('@nrwl/angular/module-federation');` | ||
|
||
module.exports = composePlugins(withNx(), (config, { options, context }) => { | ||
const federatedModules = await withModuleFederation({ | ||
// your options here | ||
}); | ||
|
||
return merge(federatedModules(config, context), { | ||
// overwrite values here | ||
}); | ||
}); | ||
``` | ||
|
||
Reference the [webpack documentation](https://webpack.js.org/configuration/) for details on the structure of the webpack | ||
config object. | ||
|
||
## Next.js Applications | ||
|
||
Next.js supports webpack customization in the `next.config.js` file. | ||
|
||
```js {% fileName="next.config.js" %} | ||
const { withNx } = require('@nrwl/next/plugins/with-nx'); | ||
|
||
const nextConfig = { | ||
webpack: ( | ||
config, | ||
{ buildId, dev, isServer, defaultLoaders, nextRuntime, webpack } | ||
) => { | ||
// Important: return the modified config | ||
return config; | ||
}, | ||
}; | ||
|
||
return withNx(nextConfig); | ||
``` | ||
|
||
Read the [official documentation](https://nextjs.org/docs/api-reference/next.config.js/custom-webpack-config) for more details. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.