Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): restore core svg file-loader #10820

Merged
merged 6 commits into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions packages/docusaurus-plugin-svgr/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* LICENSE file in the root directory of this source tree.
*/

import {createLoader} from './svgrLoader';
import {enhanceConfig} from './svgrLoader';
import type {LoadContext, Plugin} from '@docusaurus/types';
import type {PluginOptions, Options} from './options';

Expand All @@ -16,11 +16,7 @@ export default function pluginSVGR(
return {
name: 'docusaurus-plugin-svgr',
configureWebpack: (config, isServer) => {
return {
module: {
rules: [createLoader({isServer, svgrConfig: options.svgrConfig})],
},
};
enhanceConfig(config, {isServer, svgrConfig: options.svgrConfig});
},
};
}
Expand Down
36 changes: 28 additions & 8 deletions packages/docusaurus-plugin-svgr/src/svgrLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import {getFileLoaderUtils} from '@docusaurus/utils';

import type {SVGRConfig, SVGOConfig} from './options';
import type {RuleSetRule} from 'webpack';
import type {Configuration, RuleSetRule} from 'webpack';

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

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

function createSVGRLoader(params: Params): RuleSetRule {
function createSVGRRule(params: Params): RuleSetRule {
const options: SVGRConfig = {
...DefaultSVGRConfig,
...params.svgrConfig,
Expand All @@ -48,22 +48,42 @@ function createSVGRLoader(params: Params): RuleSetRule {
};
}

export function createLoader(params: Params): RuleSetRule {
export function enhanceConfig(config: Configuration, params: Params): void {
const utils = getFileLoaderUtils(params.isServer);
return {

const rules = config?.module?.rules as RuleSetRule[];

const existingSvgRule: RuleSetRule = (() => {
const rule = rules.find(
(r) => String(r.test) === String(utils.rules.svgs().test),
);
if (!rule) {
throw new Error(
"Docusaurus built-in SVG rule couldn't be found. The SVGR plugin can't enhance it.",
);
}
return rule;
})();

const newSvgRule: RuleSetRule = {
test: /\.svg$/i,
oneOf: [
{
use: [createSVGRLoader(params)],
use: [createSVGRRule(params)],
// We don't want to use SVGR loader for non-React source code
// ie we don't want to use SVGR for CSS files...
issuer: {
and: [/\.(?:tsx?|jsx?|mdx?)$/i],
},
},
{
use: [utils.loaders.url({folder: 'images'})],
},
existingSvgRule,
],
};

// This is annoying, but we have to "wrap" the existing SVG rule
// Adding another extra SVG rule (first or last) will not "override"
// the default: both rules will be applied (from last to bottom) leading to
// conflicting behavior.
const index = rules.indexOf(existingSvgRule);
rules[index] = newSvgRule;
}
2 changes: 1 addition & 1 deletion packages/docusaurus-types/src/plugin.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ export type Plugin<Content = unknown> = {
isServer: boolean,
configureWebpackUtils: ConfigureWebpackUtils,
content: Content,
) => ConfigureWebpackResult;
) => ConfigureWebpackResult | void;
configurePostCss?: (options: PostCssOptions) => PostCssOptions;
getThemePath?: () => string;
getTypeScriptThemePath?: () => string;
Expand Down
10 changes: 10 additions & 0 deletions packages/docusaurus-utils/src/webpackUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ type FileLoaderUtils = {
};
rules: {
images: () => RuleSetRule;
svgs: () => RuleSetRule;
fonts: () => RuleSetRule;
media: () => RuleSetRule;
otherAssets: () => RuleSetRule;
Expand Down Expand Up @@ -119,6 +120,15 @@ function createFileLoaderUtils({
test: /\.(?:ico|jpe?g|png|gif|webp|avif)(?:\?.*)?$/i,
}),

/**
* The SVG rule is isolated on purpose: our SVGR plugin enhances it
* See https://github.com/facebook/docusaurus/pull/10820
*/
svgs: () => ({
use: [loaders.url({folder: 'images'})],
test: /\.svg$/i,
}),

fonts: () => ({
use: [loaders.url({folder: 'fonts'})],
test: /\.(?:woff2?|eot|ttf|otf)$/i,
Expand Down
1 change: 1 addition & 0 deletions packages/docusaurus/src/webpack/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ export async function createBaseConfig({
module: {
rules: [
fileLoaderUtils.rules.images(),
fileLoaderUtils.rules.svgs(),
fileLoaderUtils.rules.fonts(),
fileLoaderUtils.rules.media(),
fileLoaderUtils.rules.otherAssets(),
Expand Down
1 change: 1 addition & 0 deletions project-words.txt
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,7 @@ Sucipto
sunsetting
supabase
Supabase
svgs
swizzlable
Tagkey
Teik
Expand Down
9 changes: 9 additions & 0 deletions website/_dogfooding/dogfooding.css
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@ html {
&.plugin-docs.plugin-id-docs-tests {
.red > a {
color: red;

&::after {
background-image: url('./red.svg');
background-size: contain;
margin-left: 0.5rem;
width: 1rem;
height: 1rem;
content: ' ';
}
}

.navbar {
Expand Down
4 changes: 4 additions & 0 deletions website/_dogfooding/red.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading