-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(webpack): add NxWebpackPlugin that works with normal Webpack con…
…figuration (#19984)
- Loading branch information
Showing
23 changed files
with
1,640 additions
and
1,170 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
80 changes: 80 additions & 0 deletions
80
packages/react/plugins/nx-react-webpack-plugin/lib/apply-react-config.ts
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,80 @@ | ||
import { Compiler, Configuration, WebpackOptionsNormalized } from 'webpack'; | ||
|
||
export function applyReactConfig( | ||
options: { svgr?: boolean }, | ||
config: Partial<WebpackOptionsNormalized | Configuration> = {} | ||
): void { | ||
addHotReload(config); | ||
|
||
if (options.svgr !== false) { | ||
removeSvgLoaderIfPresent(config); | ||
|
||
config.module.rules.push({ | ||
test: /\.svg$/, | ||
issuer: /\.(js|ts|md)x?$/, | ||
use: [ | ||
{ | ||
loader: require.resolve('@svgr/webpack'), | ||
options: { | ||
svgo: false, | ||
titleProp: true, | ||
ref: true, | ||
}, | ||
}, | ||
{ | ||
loader: require.resolve('file-loader'), | ||
options: { | ||
name: '[name].[hash].[ext]', | ||
}, | ||
}, | ||
], | ||
}); | ||
} | ||
|
||
// enable webpack node api | ||
config.node = { | ||
__dirname: true, | ||
__filename: true, | ||
}; | ||
} | ||
|
||
function addHotReload( | ||
config: Partial<WebpackOptionsNormalized | Configuration> | ||
) { | ||
if (config.mode === 'development' && config['devServer']?.hot) { | ||
// add `react-refresh/babel` to babel loader plugin | ||
const babelLoader = config.module.rules.find( | ||
(rule) => | ||
rule && | ||
typeof rule !== 'string' && | ||
rule.loader?.toString().includes('babel-loader') | ||
); | ||
|
||
if (babelLoader && typeof babelLoader !== 'string') { | ||
babelLoader.options['plugins'] = [ | ||
...(babelLoader.options['plugins'] || []), | ||
[ | ||
require.resolve('react-refresh/babel'), | ||
{ | ||
skipEnvCheck: true, | ||
}, | ||
], | ||
]; | ||
} | ||
|
||
const ReactRefreshPlugin = require('@pmmmwh/react-refresh-webpack-plugin'); | ||
config.plugins.push(new ReactRefreshPlugin()); | ||
} | ||
} | ||
|
||
// We remove potentially conflicting rules that target SVGs because we use @svgr/webpack loader | ||
// See https://github.com/nrwl/nx/issues/14383 | ||
function removeSvgLoaderIfPresent( | ||
config: Partial<WebpackOptionsNormalized | Configuration> | ||
) { | ||
const svgLoaderIdx = config.module.rules.findIndex( | ||
(rule) => typeof rule === 'object' && rule.test.toString().includes('svg') | ||
); | ||
if (svgLoaderIdx === -1) return; | ||
config.module.rules.splice(svgLoaderIdx, 1); | ||
} |
10 changes: 10 additions & 0 deletions
10
packages/react/plugins/nx-react-webpack-plugin/nx-react-webpack-plugin.ts
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,10 @@ | ||
import { Compiler } from 'webpack'; | ||
import { applyReactConfig } from './lib/apply-react-config'; | ||
|
||
export class NxReactWebpackPlugin { | ||
constructor(private options: { svgr?: boolean } = {}) {} | ||
|
||
apply(compiler: Compiler): void { | ||
applyReactConfig(this.options, compiler.options); | ||
} | ||
} |
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
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.