feat(webpack): add NxWebpackPlugin that works with normal Webpack configuration #19984
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR adds three Webpack plugins that can be used in standard
webpack.config.js
files.NxTsconfigPathsWebpackPlugin
- Add support for using workspace libs (via tsconfig paths) but leaves out everything elseNxWebpackPlugin
- Add support for using TS/TSX, assets, package.json/index.html generation, etc. (basically everything thatwithNx()
andwithWeb()
does in current setup)NxReactWebpackPlugin
- Add support for SVGR (can be opted out), and HMR via Fast RefreshNote: Existing usages of
composePlugins()
,withNx()
,withWeb()
,withReact()
will continue to work.This means that that existing projects with a build such as this in
package.json
:Can adopt Nx simply with this in their
webpack.config.js
file:If user only wants tsconfig paths feature, and want to handle TS/TSX, etc. on their own, then they can use the bare minimal
NxTsconfigPathsWebpackPlugin
.If user uses React, they can add the
NxReactWebpackPlugin
to bring in Fast Refresh support.Notes
I previously made
withNx
,withWeb
, andwithReact
push the new plugins into the config. However, this approach breaks the existing behavior of allowing users to update the config like so:Since the
.apply()
method of webpack plugins happen during build time, the configuration phase will no longer contain new rules, etc. Thus, instead of using the new plugins with thewith*
plugin functions, we just extract a shared function that can be used in the new and old plugins.TODO
.apply()
to be called prior to returning fromwithNx()
,withReact()
, etc.Current Behavior
webpack.config.js
files have to export a function rather than config object. This format is only understood by Nx, and makes it awkward to add Nx to existing webpack projects.Expected Behavior
User can adopt Nx in existing projects by adding either
NxWebpackPlugin
orNxTsconfigPathsWebpackPlugin
.Related Issue(s)
Fixes #