Skip to content

Commit 120e987

Browse files
authored
fix(react): fix issue with postcss and images in webpack config (#8802)
Fixes #8300
1 parent f2b207e commit 120e987

File tree

4 files changed

+51
-81
lines changed

4 files changed

+51
-81
lines changed

packages/react/plugins/webpack.ts

Lines changed: 36 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -3,55 +3,45 @@ import ReactRefreshPlugin = require('@pmmmwh/react-refresh-webpack-plugin');
33

44
// Add React-specific configuration
55
function getWebpackConfig(config: Configuration) {
6-
config.module.rules.push(
7-
{
8-
test: /\.(png|jpe?g|gif|webp)$/,
9-
loader: require.resolve('url-loader'),
10-
options: {
11-
limit: 10000, // 10kB
12-
name: '[name].[hash:7].[ext]',
13-
},
14-
},
15-
{
16-
test: /\.svg$/,
17-
oneOf: [
18-
// If coming from JS/TS or MDX file, then transform into React component using SVGR.
19-
{
20-
issuer: /\.(js|ts|md)x?$/,
21-
use: [
22-
{
23-
loader: require.resolve('@svgr/webpack'),
24-
options: {
25-
svgo: false,
26-
titleProp: true,
27-
ref: true,
28-
},
6+
config.module.rules.push({
7+
test: /\.svg$/,
8+
oneOf: [
9+
// If coming from JS/TS or MDX file, then transform into React component using SVGR.
10+
{
11+
issuer: /\.(js|ts|md)x?$/,
12+
use: [
13+
{
14+
loader: require.resolve('@svgr/webpack'),
15+
options: {
16+
svgo: false,
17+
titleProp: true,
18+
ref: true,
2919
},
30-
{
31-
loader: require.resolve('url-loader'),
32-
options: {
33-
limit: 10000, // 10kB
34-
name: '[name].[hash:7].[ext]',
35-
esModule: false,
36-
},
20+
},
21+
{
22+
loader: require.resolve('url-loader'),
23+
options: {
24+
limit: 10000, // 10kB
25+
name: '[name].[hash:7].[ext]',
26+
esModule: false,
3727
},
38-
],
39-
},
40-
// Fallback to plain URL loader.
41-
{
42-
use: [
43-
{
44-
loader: require.resolve('url-loader'),
45-
options: {
46-
limit: 10000, // 10kB
47-
name: '[name].[hash:7].[ext]',
48-
},
28+
},
29+
],
30+
},
31+
// Fallback to plain URL loader.
32+
{
33+
use: [
34+
{
35+
loader: require.resolve('url-loader'),
36+
options: {
37+
limit: 10000, // 10kB
38+
name: '[name].[hash:7].[ext]',
4939
},
50-
],
51-
},
52-
],
53-
}
54-
);
40+
},
41+
],
42+
},
43+
],
44+
});
5545

5646
if (config.mode === 'development' && config['devServer']?.hot) {
5747
// add `react-refresh/babel` to babel loader plugin

packages/web/src/utils/config.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ import * as webpack from 'webpack';
33
import { Configuration, WebpackPluginInstance } from 'webpack';
44
import { LicenseWebpackPlugin } from 'license-webpack-plugin';
55
import * as CopyWebpackPlugin from 'copy-webpack-plugin';
6-
import TerserPlugin = require('terser-webpack-plugin');
76
import { AssetGlobPattern, BuildBuilderOptions } from './shared-models';
87
import { getOutputHashFormat } from './hash-format';
98
import { TsconfigPathsPlugin } from 'tsconfig-paths-webpack-plugin';
9+
import TerserPlugin = require('terser-webpack-plugin');
1010
import ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin');
1111

1212
const IGNORED_WEBPACK_WARNINGS = [
@@ -58,6 +58,15 @@ export function getBaseWebpackPartial(
5858
// Enabled for performance
5959
unsafeCache: true,
6060
rules: [
61+
{
62+
test: /\.(bmp|png|jpe?g|gif|webp|avif)$/,
63+
type: 'asset',
64+
parser: {
65+
dataUrlCondition: {
66+
maxSize: 10_000, // 10 kB
67+
},
68+
},
69+
},
6170
{
6271
// There's an issue resolving paths without fully specified extensions
6372
// See: https://github.com/graphql/graphql-js/issues/2721

packages/web/src/utils/web.config.ts

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import { Configuration } from 'webpack';
77

88
import { WebWebpackExecutorOptions } from '../executors/webpack/webpack.impl';
99
import { convertBuildOptions } from './normalize';
10+
11+
// TODO(jack): These should be inlined in a single function so it is easier to understand
1012
import { getBaseWebpackPartial } from './config';
1113
import { getBrowserConfig } from './webpack/partials/browser';
1214
import { getCommonConfig } from './webpack/partials/common';
@@ -51,6 +53,7 @@ export function getWebConfig(
5153
tsConfig,
5254
tsConfigPath: options.tsConfig,
5355
};
56+
// TODO(jack): Replace merge behavior with an inlined config so it is easier to understand.
5457
return webpackMerge.merge([
5558
_getBaseWebpackPartial(
5659
options,
@@ -67,14 +70,10 @@ export function getWebConfig(
6770
),
6871
getStylesPartial(wco.root, wco.projectRoot, wco.buildOptions, true),
6972
getCommonPartial(wco),
70-
getBrowserPartial(wco, options),
73+
getBrowserConfig(wco),
7174
]);
7275
}
7376

74-
function getBrowserPartial(wco: any, options: WebWebpackExecutorOptions) {
75-
return getBrowserConfig(wco);
76-
}
77-
7877
function _getBaseWebpackPartial(
7978
options: WebWebpackExecutorOptions,
8079
esm: boolean,
@@ -144,25 +143,11 @@ export function getStylesPartial(
144143
},
145144
importLoaders: 1,
146145
};
147-
const postcssOptions: PostcssOptions = (loader) => ({
146+
const postcssOptions: PostcssOptions = () => ({
148147
plugins: [
149148
postcssImports({
150149
addModulesDirectories: includePaths,
151150
resolve: (url: string) => (url.startsWith('~') ? url.substr(1) : url),
152-
load: (filename: string) => {
153-
return new Promise<string>((resolve, reject) => {
154-
loader.fs.readFile(filename, (err: Error, data: Buffer) => {
155-
if (err) {
156-
reject(err);
157-
158-
return;
159-
}
160-
161-
const content = data.toString();
162-
resolve(content);
163-
});
164-
});
165-
},
166151
}),
167152
],
168153
});

packages/web/src/utils/webpack/partials/styles.ts

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -40,20 +40,6 @@ export function getStylesConfig(
4040
postcssImports({
4141
addModulesDirectories: includePaths,
4242
resolve: (url: string) => (url.startsWith('~') ? url.substr(1) : url),
43-
load: (filename: string) => {
44-
return new Promise<string>((resolve, reject) => {
45-
loader.fs.readFile(filename, (err: Error, data: Buffer) => {
46-
if (err) {
47-
reject(err);
48-
49-
return;
50-
}
51-
52-
const content = data.toString();
53-
resolve(content);
54-
});
55-
});
56-
},
5743
}),
5844
PostcssCliResources({
5945
baseHref: buildOptions.baseHref,

0 commit comments

Comments
 (0)