Skip to content

Commit 1b311e9

Browse files
authored
refactor(plugin-svgr): use internal asset loader (#7482)
1 parent e84ceda commit 1b311e9

File tree

11 files changed

+77
-210
lines changed

11 files changed

+77
-210
lines changed

packages/plugin-svgr/package.json

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,11 @@
1717
},
1818
"types": "./dist/index.d.ts",
1919
"files": [
20-
"dist",
21-
"compiled"
20+
"dist"
2221
],
2322
"scripts": {
2423
"build": "rslib",
25-
"dev": "rslib -w",
26-
"prebundle": "prebundle"
24+
"dev": "rslib -w"
2725
},
2826
"dependencies": {
2927
"@rsbuild/plugin-react": "workspace:^2.0.0-rc.2",
@@ -39,11 +37,8 @@
3937
"@rslib/core": "0.21.0",
4038
"@scripts/test-helper": "workspace:*",
4139
"@types/node": "^24.12.2",
42-
"file-loader": "6.2.0",
43-
"prebundle": "1.6.4",
4440
"svgo": "^3.3.3",
45-
"typescript": "^6.0.2",
46-
"url-loader": "4.1.1"
41+
"typescript": "^6.0.2"
4742
},
4843
"peerDependencies": {
4944
"@rsbuild/core": "^2.0.0-0"

packages/plugin-svgr/prebundle.config.ts

Lines changed: 0 additions & 34 deletions
This file was deleted.

packages/plugin-svgr/rslib.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export default defineConfig({
88
{
99
source: {
1010
entry: {
11+
assetLoader: './src/assetLoader.ts',
1112
loader: './src/loader.ts',
1213
},
1314
},
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import path from 'node:path';
2+
import type { Rspack } from '@rsbuild/core';
3+
import { interpolateName } from 'loader-utils';
4+
5+
type FilenameTemplate =
6+
| string
7+
| ((resourcePath: string, resourceQuery?: string) => string);
8+
9+
export type SvgAssetLoaderOptions = {
10+
limit: number;
11+
name: FilenameTemplate;
12+
};
13+
14+
type RawLoaderDefinition<OptionsType = {}> = ((
15+
this: Rspack.LoaderContext<OptionsType>,
16+
content: Buffer,
17+
) => string | Buffer | void | Promise<string | Buffer | void>) & {
18+
raw: true;
19+
};
20+
21+
const SVG_MIME_TYPE = 'image/svg+xml';
22+
const HASH_TEMPLATE_REGEX = /\[(?:[^:\]]+:)?(?:hash|contenthash)(?::[^\]]+)?]/i;
23+
24+
const normalizePath = (value: string) => value.replace(/\\/g, '/');
25+
26+
const assetLoader: RawLoaderDefinition<SvgAssetLoaderOptions> = function (
27+
content,
28+
) {
29+
const { limit, name } = this.getOptions();
30+
31+
if (content.length <= limit) {
32+
const dataUrl = `data:${SVG_MIME_TYPE};base64,${content.toString('base64')}`;
33+
return `export default ${JSON.stringify(dataUrl)}`;
34+
}
35+
36+
const filename = interpolateName(this, name, {
37+
context: this.rootContext,
38+
content,
39+
});
40+
41+
const assetInfo: {
42+
immutable?: true;
43+
sourceFilename: string;
44+
} = {
45+
sourceFilename: normalizePath(
46+
path.relative(this.rootContext, this.resourcePath),
47+
),
48+
};
49+
50+
if (typeof name === 'string' && HASH_TEMPLATE_REGEX.test(name)) {
51+
assetInfo.immutable = true;
52+
}
53+
54+
this.emitFile(filename, content, undefined, assetInfo);
55+
56+
return `export default __webpack_public_path__ + ${JSON.stringify(filename)};`;
57+
};
58+
59+
assetLoader.raw = true;
60+
61+
export default assetLoader;

packages/plugin-svgr/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ export const pluginSvgr = (options: PluginSvgrOptions = {}): RsbuildPlugin => ({
256256
if (mixedImport && exportType === 'named') {
257257
svgRule
258258
.use(CHAIN_ID.USE.URL)
259-
.loader(path.join(__dirname, '../compiled', 'url-loader/index.js'))
259+
.loader(path.resolve(__dirname, './assetLoader.mjs'))
260260
.options({
261261
limit: maxSize,
262262
name: generatorOptions?.filename,
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
declare module 'loader-utils' {
2+
export function interpolateName(
3+
loaderContext: unknown,
4+
name?: string | ((resourcePath: string, resourceQuery?: string) => string),
5+
options?: {
6+
context?: string;
7+
content?: Buffer;
8+
},
9+
): string;
10+
}

packages/plugin-svgr/tests/__snapshots__/index.test.ts.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -769,7 +769,7 @@ exports[`svgr > exportType named / mixedImport true 1`] = `
769769
},
770770
},
771771
{
772-
"loader": "<ROOT>/packages/plugin-svgr/compiled/url-loader/index.js",
772+
"loader": "<ROOT>/packages/plugin-svgr/src/assetLoader.mjs",
773773
"options": {
774774
"limit": 4096,
775775
"name": "static/svg/[name].[contenthash:10].svg",

patches/file-loader@6.2.0.patch

Lines changed: 0 additions & 29 deletions
This file was deleted.

patches/url-loader@4.1.1.patch

Lines changed: 0 additions & 37 deletions
This file was deleted.

0 commit comments

Comments
 (0)