-
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
- Loading branch information
Showing
14 changed files
with
1,497 additions
and
1,079 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
78 changes: 78 additions & 0 deletions
78
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,78 @@ | ||
import { Compiler } from 'webpack'; | ||
|
||
export class NxReactWebpackPlugin { | ||
constructor(private options: { svgr?: boolean } = {}) {} | ||
|
||
apply(compiler: Compiler): void { | ||
this.addHotReload(compiler); | ||
|
||
if (this.options.svgr !== false) { | ||
this.removeSvgLoaderIfPresent(compiler); | ||
|
||
compiler.options.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 | ||
compiler.options.node = { | ||
__dirname: true, | ||
__filename: true, | ||
}; | ||
} | ||
|
||
private addHotReload(compiler: Compiler) { | ||
const config = compiler.options; | ||
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 | ||
private removeSvgLoaderIfPresent(compiler: Compiler) { | ||
const svgLoaderIdx = compiler.options.module.rules.findIndex( | ||
(rule) => typeof rule === 'object' && rule.test.toString().includes('svg') | ||
); | ||
if (svgLoaderIdx === -1) return; | ||
compiler.options.module.rules.splice(svgLoaderIdx, 1); | ||
} | ||
} |
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
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.