Skip to content

Commit 97690ab

Browse files
authored
fix(core): restore core svg file-loader (#10820)
1 parent 3525952 commit 97690ab

File tree

8 files changed

+56
-15
lines changed

8 files changed

+56
-15
lines changed

packages/docusaurus-plugin-svgr/src/index.ts

+2-6
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8-
import {createLoader} from './svgrLoader';
8+
import {enhanceConfig} from './svgrLoader';
99
import type {LoadContext, Plugin} from '@docusaurus/types';
1010
import type {PluginOptions, Options} from './options';
1111

@@ -16,11 +16,7 @@ export default function pluginSVGR(
1616
return {
1717
name: 'docusaurus-plugin-svgr',
1818
configureWebpack: (config, isServer) => {
19-
return {
20-
module: {
21-
rules: [createLoader({isServer, svgrConfig: options.svgrConfig})],
22-
},
23-
};
19+
enhanceConfig(config, {isServer, svgrConfig: options.svgrConfig});
2420
},
2521
};
2622
}

packages/docusaurus-plugin-svgr/src/svgrLoader.ts

+28-8
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import {getFileLoaderUtils} from '@docusaurus/utils';
99

1010
import type {SVGRConfig, SVGOConfig} from './options';
11-
import type {RuleSetRule} from 'webpack';
11+
import type {Configuration, RuleSetRule} from 'webpack';
1212

1313
// TODO Docusaurus v4: change these defaults?
1414
// see https://github.com/facebook/docusaurus/issues/8297
@@ -37,7 +37,7 @@ const DefaultSVGRConfig: SVGRConfig = {
3737

3838
type Params = {isServer: boolean; svgrConfig: SVGRConfig};
3939

40-
function createSVGRLoader(params: Params): RuleSetRule {
40+
function createSVGRRule(params: Params): RuleSetRule {
4141
const options: SVGRConfig = {
4242
...DefaultSVGRConfig,
4343
...params.svgrConfig,
@@ -48,22 +48,42 @@ function createSVGRLoader(params: Params): RuleSetRule {
4848
};
4949
}
5050

51-
export function createLoader(params: Params): RuleSetRule {
51+
export function enhanceConfig(config: Configuration, params: Params): void {
5252
const utils = getFileLoaderUtils(params.isServer);
53-
return {
53+
54+
const rules = config?.module?.rules as RuleSetRule[];
55+
56+
const existingSvgRule: RuleSetRule = (() => {
57+
const rule = rules.find(
58+
(r) => String(r.test) === String(utils.rules.svgs().test),
59+
);
60+
if (!rule) {
61+
throw new Error(
62+
"Docusaurus built-in SVG rule couldn't be found. The SVGR plugin can't enhance it.",
63+
);
64+
}
65+
return rule;
66+
})();
67+
68+
const newSvgRule: RuleSetRule = {
5469
test: /\.svg$/i,
5570
oneOf: [
5671
{
57-
use: [createSVGRLoader(params)],
72+
use: [createSVGRRule(params)],
5873
// We don't want to use SVGR loader for non-React source code
5974
// ie we don't want to use SVGR for CSS files...
6075
issuer: {
6176
and: [/\.(?:tsx?|jsx?|mdx?)$/i],
6277
},
6378
},
64-
{
65-
use: [utils.loaders.url({folder: 'images'})],
66-
},
79+
existingSvgRule,
6780
],
6881
};
82+
83+
// This is annoying, but we have to "wrap" the existing SVG rule
84+
// Adding another extra SVG rule (first or last) will not "override"
85+
// the default: both rules will be applied (from last to bottom) leading to
86+
// conflicting behavior.
87+
const index = rules.indexOf(existingSvgRule);
88+
rules[index] = newSvgRule;
6989
}

packages/docusaurus-types/src/plugin.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ export type Plugin<Content = unknown> = {
140140
isServer: boolean,
141141
configureWebpackUtils: ConfigureWebpackUtils,
142142
content: Content,
143-
) => ConfigureWebpackResult;
143+
) => ConfigureWebpackResult | void;
144144
configurePostCss?: (options: PostCssOptions) => PostCssOptions;
145145
getThemePath?: () => string;
146146
getTypeScriptThemePath?: () => string;

packages/docusaurus-utils/src/webpackUtils.ts

+10
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ type FileLoaderUtils = {
4343
};
4444
rules: {
4545
images: () => RuleSetRule;
46+
svgs: () => RuleSetRule;
4647
fonts: () => RuleSetRule;
4748
media: () => RuleSetRule;
4849
otherAssets: () => RuleSetRule;
@@ -119,6 +120,15 @@ function createFileLoaderUtils({
119120
test: /\.(?:ico|jpe?g|png|gif|webp|avif)(?:\?.*)?$/i,
120121
}),
121122

123+
/**
124+
* The SVG rule is isolated on purpose: our SVGR plugin enhances it
125+
* See https://github.com/facebook/docusaurus/pull/10820
126+
*/
127+
svgs: () => ({
128+
use: [loaders.url({folder: 'images'})],
129+
test: /\.svg$/i,
130+
}),
131+
122132
fonts: () => ({
123133
use: [loaders.url({folder: 'fonts'})],
124134
test: /\.(?:woff2?|eot|ttf|otf)$/i,

packages/docusaurus/src/webpack/base.ts

+1
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,7 @@ export async function createBaseConfig({
251251
module: {
252252
rules: [
253253
fileLoaderUtils.rules.images(),
254+
fileLoaderUtils.rules.svgs(),
254255
fileLoaderUtils.rules.fonts(),
255256
fileLoaderUtils.rules.media(),
256257
fileLoaderUtils.rules.otherAssets(),

project-words.txt

+1
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,7 @@ Sucipto
323323
sunsetting
324324
supabase
325325
Supabase
326+
svgs
326327
swizzlable
327328
Tagkey
328329
Teik

website/_dogfooding/dogfooding.css

+9
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,15 @@ html {
1010
&.plugin-docs.plugin-id-docs-tests {
1111
.red > a {
1212
color: red;
13+
14+
&::after {
15+
background-image: url('./red.svg');
16+
background-size: contain;
17+
margin-left: 0.5rem;
18+
width: 1rem;
19+
height: 1rem;
20+
content: ' ';
21+
}
1322
}
1423

1524
.navbar {

website/_dogfooding/red.svg

+4
Loading

0 commit comments

Comments
 (0)