fix(webpack): use webpack-merge for plugins to preserve values, fix React SVG loader (#14695)
This commit is contained in:
parent
a10dc40f18
commit
e92097c5d0
@ -289,14 +289,17 @@ describe('CLI - Environment Variables', () => {
|
|||||||
|
|
||||||
updateFile(main2, `${newCode2}\n${content2}`);
|
updateFile(main2, `${newCode2}\n${content2}`);
|
||||||
|
|
||||||
runCLI(`run-many --target build --all --outputHashing=none`, {
|
runCLI(
|
||||||
env: {
|
`run-many --target build --all --outputHashing=none --optimization=false`,
|
||||||
...process.env,
|
{
|
||||||
NODE_ENV: 'test',
|
env: {
|
||||||
NX_BUILD: '52',
|
...process.env,
|
||||||
NX_API: 'QA',
|
NODE_ENV: 'test',
|
||||||
},
|
NX_BUILD: '52',
|
||||||
});
|
NX_API: 'QA',
|
||||||
|
},
|
||||||
|
}
|
||||||
|
);
|
||||||
expect(readFile(`dist/apps/${appName}/main.js`)).toContain(
|
expect(readFile(`dist/apps/${appName}/main.js`)).toContain(
|
||||||
'const envVars = ["test", "52", "QA", "ws-base", "ws-env-local", "ws-local-env", "app-base", "app-env-local", "app-local-env", "shared-in-app-env-local"];'
|
'const envVars = ["test", "52", "QA", "ws-base", "ws-env-local", "ws-local-env", "app-base", "app-env-local", "app-local-env", "shared-in-app-env-local"];'
|
||||||
);
|
);
|
||||||
|
|||||||
@ -1,18 +1,63 @@
|
|||||||
import type { Configuration } from 'webpack';
|
import type { Configuration } from 'webpack';
|
||||||
import type { WithWebOptions } from '@nrwl/webpack';
|
import type { WithWebOptions } from '@nrwl/webpack';
|
||||||
|
import type { NxWebpackExecutionContext } from '@nrwl/webpack';
|
||||||
|
|
||||||
const processed = new Set();
|
const processed = new Set();
|
||||||
|
|
||||||
interface WithReactOptions extends WithWebOptions {}
|
interface WithReactOptions extends WithWebOptions {}
|
||||||
|
|
||||||
|
function addHotReload(config: Configuration) {
|
||||||
|
if (config.mode === 'development' && config['devServer']?.hot) {
|
||||||
|
// add `react-refresh/babel` to babel loader plugin
|
||||||
|
const babelLoader = config.module.rules.find(
|
||||||
|
(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: 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);
|
||||||
|
}
|
||||||
|
|
||||||
export function withReact(pluginOptions: WithReactOptions = {}) {
|
export function withReact(pluginOptions: WithReactOptions = {}) {
|
||||||
return function configure(config: Configuration, _ctx?: any): Configuration {
|
return function configure(
|
||||||
|
config: Configuration,
|
||||||
|
context: NxWebpackExecutionContext
|
||||||
|
): Configuration {
|
||||||
const { withWeb } = require('@nrwl/webpack');
|
const { withWeb } = require('@nrwl/webpack');
|
||||||
|
|
||||||
if (processed.has(config)) return config;
|
if (processed.has(config)) return config;
|
||||||
|
|
||||||
// Apply web config for CSS, JSX, index.html handling, etc.
|
// Apply web config for CSS, JSX, index.html handling, etc.
|
||||||
config = withWeb(pluginOptions)(config, _ctx);
|
config = withWeb(pluginOptions)(config, context);
|
||||||
|
|
||||||
|
addHotReload(config);
|
||||||
|
removeSvgLoaderIfPresent(config);
|
||||||
|
|
||||||
config.module.rules.push({
|
config.module.rules.push({
|
||||||
test: /\.svg$/,
|
test: /\.svg$/,
|
||||||
@ -35,28 +80,6 @@ export function withReact(pluginOptions: WithReactOptions = {}) {
|
|||||||
],
|
],
|
||||||
});
|
});
|
||||||
|
|
||||||
if (config.mode === 'development' && config['devServer']?.hot) {
|
|
||||||
// add `react-refresh/babel` to babel loader plugin
|
|
||||||
const babelLoader = config.module.rules.find(
|
|
||||||
(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());
|
|
||||||
}
|
|
||||||
|
|
||||||
// enable webpack node api
|
// enable webpack node api
|
||||||
config.node = {
|
config.node = {
|
||||||
__dirname: true,
|
__dirname: true,
|
||||||
|
|||||||
@ -25,6 +25,7 @@ import CssMinimizerPlugin = require('css-minimizer-webpack-plugin');
|
|||||||
import MiniCssExtractPlugin = require('mini-css-extract-plugin');
|
import MiniCssExtractPlugin = require('mini-css-extract-plugin');
|
||||||
import autoprefixer = require('autoprefixer');
|
import autoprefixer = require('autoprefixer');
|
||||||
import postcssImports = require('postcss-import');
|
import postcssImports = require('postcss-import');
|
||||||
|
import { NxWebpackExecutionContext } from '@nrwl/webpack/src/utils/config';
|
||||||
|
|
||||||
interface PostcssOptions {
|
interface PostcssOptions {
|
||||||
(loader: any): any;
|
(loader: any): any;
|
||||||
@ -58,10 +59,7 @@ export type MergedOptions = Omit<
|
|||||||
export function withWeb(pluginOptions: WithWebOptions = {}) {
|
export function withWeb(pluginOptions: WithWebOptions = {}) {
|
||||||
return function configure(
|
return function configure(
|
||||||
config: Configuration,
|
config: Configuration,
|
||||||
{
|
{ options: executorOptions, context }: NxWebpackExecutionContext
|
||||||
options: executorOptions,
|
|
||||||
context,
|
|
||||||
}: { options: NormalizedWebpackExecutorOptions; context: ExecutorContext }
|
|
||||||
): Configuration {
|
): Configuration {
|
||||||
if (processed.has(config)) return config;
|
if (processed.has(config)) return config;
|
||||||
|
|
||||||
@ -367,7 +365,7 @@ export function withWeb(pluginOptions: WithWebOptions = {}) {
|
|||||||
|
|
||||||
config.optimization = {
|
config.optimization = {
|
||||||
...config.optimization,
|
...config.optimization,
|
||||||
minimizer,
|
minimizer: [...config.optimization.minimizer, ...minimizer],
|
||||||
emitOnErrors: false,
|
emitOnErrors: false,
|
||||||
moduleIds: 'deterministic' as const,
|
moduleIds: 'deterministic' as const,
|
||||||
runtimeChunk: mergedOptions.runtimeChunk ? ('single' as const) : false,
|
runtimeChunk: mergedOptions.runtimeChunk ? ('single' as const) : false,
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user