@rsbuild/plugin-svgr 2.0.4 → 2.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/assetLoader.mjs +1 -1
- package/dist/index.d.ts +8 -5
- package/dist/index.js +14 -16
- package/dist/loader.mjs +22 -18
- package/package.json +23 -23
package/dist/assetLoader.mjs
CHANGED
|
@@ -9,7 +9,7 @@ const assetLoader = function(content) {
|
|
|
9
9
|
const buffer = Buffer.isBuffer(content) ? content : Buffer.from(content, 'utf8');
|
|
10
10
|
if (buffer.length <= limit) {
|
|
11
11
|
const dataUrl = `data:${SVG_MIME_TYPE};base64,${buffer.toString('base64')}`;
|
|
12
|
-
return `export default ${
|
|
12
|
+
return `export default "${dataUrl}"`;
|
|
13
13
|
}
|
|
14
14
|
const filename = interpolateName(this, name, {
|
|
15
15
|
context: this.rootContext,
|
package/dist/index.d.ts
CHANGED
|
@@ -21,11 +21,14 @@ export type PluginSvgrOptions = {
|
|
|
21
21
|
* @default /react/
|
|
22
22
|
*/ query?: RegExp;
|
|
23
23
|
/**
|
|
24
|
-
* Whether to
|
|
25
|
-
* threads. When enabled,
|
|
26
|
-
*
|
|
27
|
-
*
|
|
28
|
-
*
|
|
24
|
+
* Whether to transform SVG modules into React components in parallel using worker
|
|
25
|
+
* threads. When enabled, SVG modules are processed across multiple worker threads,
|
|
26
|
+
* reducing pressure on the main thread and improving overall build performance
|
|
27
|
+
* when compiling large numbers of SVG modules.
|
|
28
|
+
*
|
|
29
|
+
* Options transferred to worker threads must comply with the HTML structured clone
|
|
30
|
+
* algorithm. For example, functions cannot be passed as options.
|
|
31
|
+
* @see https://nodejs.org/api/worker_threads.html#portpostmessagevalue-transferlist
|
|
29
32
|
* @default false
|
|
30
33
|
*/ parallel?: boolean;
|
|
31
34
|
/**
|
package/dist/index.js
CHANGED
|
@@ -3,7 +3,7 @@ import { PLUGIN_REACT_NAME } from "@rsbuild/plugin-react";
|
|
|
3
3
|
import deepmerge from "deepmerge";
|
|
4
4
|
let SVG_REGEX = /\.svg$/, PLUGIN_SVGR_NAME = 'rsbuild:svgr';
|
|
5
5
|
function assertCoreVersion(version) {
|
|
6
|
-
if ('1' === version.
|
|
6
|
+
if ('1' === version || version.startsWith('1.')) throw Error('"@rsbuild/plugin-svgr" v2 requires "@rsbuild/core" >= 2.0. Please upgrade "@rsbuild/core" or use "@rsbuild/plugin-svgr" v1.');
|
|
7
7
|
}
|
|
8
8
|
let pluginSvgr = (options = {})=>({
|
|
9
9
|
name: PLUGIN_SVGR_NAME,
|
|
@@ -36,20 +36,17 @@ let pluginSvgr = (options = {})=>({
|
|
|
36
36
|
if (!config.plugins) return config;
|
|
37
37
|
let mergedPlugins = [];
|
|
38
38
|
for (let plugin of config.plugins){
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
39
|
+
let name = 'string' == typeof plugin ? plugin : plugin.name, index = mergedPlugins.findIndex((item)=>('string' == typeof item ? item : item.name) === name);
|
|
40
|
+
if (-1 === index) mergedPlugins.push(plugin);
|
|
41
|
+
else if ('string' != typeof plugin) {
|
|
42
|
+
let existing = mergedPlugins[index];
|
|
43
|
+
mergedPlugins[index] = 'string' == typeof existing ? plugin : deepmerge(existing, plugin);
|
|
42
44
|
}
|
|
43
|
-
let strIndex = mergedPlugins.findIndex((item)=>'string' == typeof item && item === plugin.name);
|
|
44
|
-
if (-1 !== strIndex) {
|
|
45
|
-
mergedPlugins[strIndex] = plugin;
|
|
46
|
-
continue;
|
|
47
|
-
}
|
|
48
|
-
let isMerged = !1;
|
|
49
|
-
mergedPlugins = mergedPlugins.map((item)=>'object' == typeof item && item.name === plugin.name ? (isMerged = !0, deepmerge(item, plugin)) : item), isMerged || mergedPlugins.push(plugin);
|
|
50
45
|
}
|
|
51
46
|
return config.plugins = mergedPlugins, config;
|
|
52
|
-
})(svgrOptions.svgoConfig), rule.oneOf(CHAIN_ID.ONE_OF.SVG_URL).type('asset/resource').resourceQuery(/^\?url$/).set('generator', generatorOptions), rule.oneOf(CHAIN_ID.ONE_OF.SVG_INLINE).type('asset/inline').resourceQuery(/^\?inline$/), CHAIN_ID.ONE_OF.
|
|
47
|
+
})(svgrOptions.svgoConfig), rule.oneOf(CHAIN_ID.ONE_OF.SVG_URL).type('asset/resource').resourceQuery(/^\?url$/).set('generator', generatorOptions), rule.oneOf(CHAIN_ID.ONE_OF.SVG_INLINE).type('asset/inline').resourceQuery(/^\?inline$/), CHAIN_ID.ONE_OF.SVG_TEXT && rule.oneOf(CHAIN_ID.ONE_OF.SVG_TEXT).type('asset/source').with({
|
|
48
|
+
type: 'text'
|
|
49
|
+
}), CHAIN_ID.ONE_OF.SVG_RAW && rule.oneOf(CHAIN_ID.ONE_OF.SVG_RAW).type('asset/source').resourceQuery(/^\?raw$/);
|
|
53
50
|
let svgReactUse = rule.oneOf(CHAIN_ID.ONE_OF.SVG_REACT).type("javascript/auto").resourceQuery(options.query || /react/).use(CHAIN_ID.USE.SVGR).loader(node_path.join(import.meta.dirname, 'loader.mjs')).options({
|
|
54
51
|
...svgrOptions,
|
|
55
52
|
exportType: 'default'
|
|
@@ -88,15 +85,16 @@ let pluginSvgr = (options = {})=>({
|
|
|
88
85
|
CHAIN_ID.USE.SWC,
|
|
89
86
|
CHAIN_ID.USE.BABEL
|
|
90
87
|
].some((jsUseId)=>{
|
|
91
|
-
|
|
92
|
-
|
|
88
|
+
if (!jsMainRule.uses.has(jsUseId)) return !1;
|
|
89
|
+
let use = jsMainRule.use(jsUseId), loaderPath = use.get('loader');
|
|
90
|
+
if (!loaderPath) return !1;
|
|
93
91
|
for (let oneOfId of [
|
|
94
92
|
CHAIN_ID.ONE_OF.SVG,
|
|
95
93
|
CHAIN_ID.ONE_OF.SVG_REACT
|
|
96
94
|
]){
|
|
97
95
|
if (!rule.oneOfs.has(oneOfId)) continue;
|
|
98
96
|
let loaderOptions = use.get('options');
|
|
99
|
-
jsUseId === CHAIN_ID.USE.SWC && (loaderOptions = deepmerge(loaderOptions, {
|
|
97
|
+
jsUseId === CHAIN_ID.USE.SWC && (loaderOptions = deepmerge(loaderOptions ?? {}, {
|
|
100
98
|
jsc: {
|
|
101
99
|
transform: {
|
|
102
100
|
react: {
|
|
@@ -104,7 +102,7 @@ let pluginSvgr = (options = {})=>({
|
|
|
104
102
|
}
|
|
105
103
|
}
|
|
106
104
|
}
|
|
107
|
-
})), rule.oneOf(oneOfId).use(jsUseId).before(CHAIN_ID.USE.SVGR).loader(
|
|
105
|
+
})), rule.oneOf(oneOfId).use(jsUseId).before(CHAIN_ID.USE.SVGR).loader(loaderPath).options(loaderOptions);
|
|
108
106
|
}
|
|
109
107
|
return !0;
|
|
110
108
|
});
|
package/dist/loader.mjs
CHANGED
|
@@ -1,37 +1,41 @@
|
|
|
1
1
|
import { normalize } from "node:path";
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
2
|
+
import { promisify } from "node:util";
|
|
3
|
+
const cachedImport = (importer)=>{
|
|
4
|
+
let promise;
|
|
5
|
+
return ()=>promise ??= importer().catch((error)=>{
|
|
6
|
+
promise = void 0;
|
|
7
|
+
throw error;
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
const getSvgr = cachedImport(()=>Promise.all([
|
|
11
|
+
import("@svgr/core"),
|
|
12
|
+
import("@svgr/plugin-jsx"),
|
|
13
|
+
import("@svgr/plugin-svgo")
|
|
14
|
+
]));
|
|
15
|
+
const svgrLoader = async function(contents) {
|
|
8
16
|
this?.cacheable();
|
|
9
|
-
const callback = this.async();
|
|
10
17
|
const options = this.getOptions();
|
|
11
18
|
const previousExport = (()=>{
|
|
12
19
|
if (contents.startsWith('export ')) return contents;
|
|
13
20
|
const exportMatches = contents.match(/^module.exports\s*=\s*(.*)/);
|
|
14
21
|
return exportMatches ? `export default ${exportMatches[1]}` : null;
|
|
15
22
|
})();
|
|
16
|
-
|
|
23
|
+
if (previousExport) {
|
|
24
|
+
const fs = this.fs;
|
|
25
|
+
contents = String(await promisify(fs.readFile).call(fs, this.resourcePath));
|
|
26
|
+
}
|
|
27
|
+
const [{ transform }, { default: jsx }, { default: svgo }] = await getSvgr();
|
|
28
|
+
return transform(contents, options, {
|
|
17
29
|
caller: {
|
|
18
30
|
name: '@rsbuild/plugin-svgr',
|
|
19
31
|
previousExport,
|
|
20
32
|
defaultPlugins: [
|
|
21
|
-
|
|
22
|
-
|
|
33
|
+
svgo,
|
|
34
|
+
jsx
|
|
23
35
|
]
|
|
24
36
|
},
|
|
25
37
|
filePath: normalize(this.resourcePath)
|
|
26
|
-
};
|
|
27
|
-
if (previousExport) this.fs.readFile(this.resourcePath, (err, result)=>{
|
|
28
|
-
if (err) return void callback(err);
|
|
29
|
-
transformSvg(String(result), options, state, (err, content)=>{
|
|
30
|
-
if (err) return void callback(err);
|
|
31
|
-
callback(null, content);
|
|
32
|
-
});
|
|
33
38
|
});
|
|
34
|
-
else transformSvg(contents, options, state, callback);
|
|
35
39
|
};
|
|
36
40
|
const loader = svgrLoader;
|
|
37
41
|
export default loader;
|
package/package.json
CHANGED
|
@@ -1,28 +1,25 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rsbuild/plugin-svgr",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.6",
|
|
4
4
|
"description": "SVGR plugin for Rsbuild",
|
|
5
|
-
"
|
|
5
|
+
"homepage": "https://rsbuild.rs",
|
|
6
|
+
"bugs": {
|
|
7
|
+
"url": "https://github.com/web-infra-dev/rsbuild/issues"
|
|
8
|
+
},
|
|
6
9
|
"repository": {
|
|
7
10
|
"type": "git",
|
|
8
11
|
"url": "https://github.com/web-infra-dev/rsbuild",
|
|
9
12
|
"directory": "packages/plugin-svgr"
|
|
10
13
|
},
|
|
11
|
-
"
|
|
12
|
-
"dist"
|
|
13
|
-
],
|
|
14
|
+
"license": "MIT",
|
|
14
15
|
"type": "module",
|
|
15
|
-
"types": "./dist/index.d.ts",
|
|
16
16
|
"exports": {
|
|
17
|
-
".":
|
|
18
|
-
"types": "./dist/index.d.ts",
|
|
19
|
-
"default": "./dist/index.js"
|
|
20
|
-
}
|
|
21
|
-
},
|
|
22
|
-
"publishConfig": {
|
|
23
|
-
"access": "public",
|
|
24
|
-
"registry": "https://registry.npmjs.org/"
|
|
17
|
+
".": "./dist/index.js"
|
|
25
18
|
},
|
|
19
|
+
"types": "./dist/index.d.ts",
|
|
20
|
+
"files": [
|
|
21
|
+
"dist"
|
|
22
|
+
],
|
|
26
23
|
"dependencies": {
|
|
27
24
|
"@rsbuild/plugin-react": "^2.0.0",
|
|
28
25
|
"@svgr/core": "8.1.0",
|
|
@@ -32,13 +29,11 @@
|
|
|
32
29
|
"loader-utils": "^3.3.1"
|
|
33
30
|
},
|
|
34
31
|
"devDependencies": {
|
|
35
|
-
"@rsbuild/core
|
|
36
|
-
"@
|
|
37
|
-
"@types/node": "^
|
|
38
|
-
"svgo": "^3.3.
|
|
39
|
-
"typescript": "^6.0.3"
|
|
40
|
-
"@rsbuild/core": "2.0.15",
|
|
41
|
-
"@scripts/test-helper": "1.0.0"
|
|
32
|
+
"@rsbuild/core": "2.3.7",
|
|
33
|
+
"@scripts/test-helper": "1.0.0",
|
|
34
|
+
"@types/node": "^26.6.11",
|
|
35
|
+
"svgo": "^3.3.5",
|
|
36
|
+
"typescript": "^6.0.3"
|
|
42
37
|
},
|
|
43
38
|
"peerDependencies": {
|
|
44
39
|
"@rsbuild/core": "^2.0.0"
|
|
@@ -48,8 +43,13 @@
|
|
|
48
43
|
"optional": true
|
|
49
44
|
}
|
|
50
45
|
},
|
|
46
|
+
"publishConfig": {
|
|
47
|
+
"access": "public",
|
|
48
|
+
"registry": "https://registry.npmjs.org/"
|
|
49
|
+
},
|
|
51
50
|
"scripts": {
|
|
52
|
-
"build": "
|
|
53
|
-
"dev": "
|
|
51
|
+
"build": "rs lib",
|
|
52
|
+
"dev": "rs lib -w",
|
|
53
|
+
"test": "rs test"
|
|
54
54
|
}
|
|
55
55
|
}
|