@rsbuild/plugin-svgr 2.0.5 → 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.
@@ -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 ${JSON.stringify(dataUrl)}`;
12
+ return `export default "${dataUrl}"`;
13
13
  }
14
14
  const filename = interpolateName(this, name, {
15
15
  context: this.rootContext,
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.split('.')[0]) throw Error('"@rsbuild/plugin-svgr" v2 requires "@rsbuild/core" >= 2.0. Please upgrade "@rsbuild/core" or use "@rsbuild/plugin-svgr" v1.');
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,17 +36,12 @@ let pluginSvgr = (options = {})=>({
36
36
  if (!config.plugins) return config;
37
37
  let mergedPlugins = [];
38
38
  for (let plugin of config.plugins){
39
- if ('string' == typeof plugin) {
40
- mergedPlugins.find((item)=>item === plugin || 'object' == typeof item && item.name === plugin) || mergedPlugins.push(plugin);
41
- continue;
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
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({
@@ -90,15 +85,16 @@ let pluginSvgr = (options = {})=>({
90
85
  CHAIN_ID.USE.SWC,
91
86
  CHAIN_ID.USE.BABEL
92
87
  ].some((jsUseId)=>{
93
- let use = jsMainRule.uses.get(jsUseId);
94
- if (!use) return !1;
88
+ if (!jsMainRule.uses.has(jsUseId)) return !1;
89
+ let use = jsMainRule.use(jsUseId), loaderPath = use.get('loader');
90
+ if (!loaderPath) return !1;
95
91
  for (let oneOfId of [
96
92
  CHAIN_ID.ONE_OF.SVG,
97
93
  CHAIN_ID.ONE_OF.SVG_REACT
98
94
  ]){
99
95
  if (!rule.oneOfs.has(oneOfId)) continue;
100
96
  let loaderOptions = use.get('options');
101
- jsUseId === CHAIN_ID.USE.SWC && (loaderOptions = deepmerge(loaderOptions, {
97
+ jsUseId === CHAIN_ID.USE.SWC && (loaderOptions = deepmerge(loaderOptions ?? {}, {
102
98
  jsc: {
103
99
  transform: {
104
100
  react: {
@@ -106,7 +102,7 @@ let pluginSvgr = (options = {})=>({
106
102
  }
107
103
  }
108
104
  }
109
- })), rule.oneOf(oneOfId).use(jsUseId).before(CHAIN_ID.USE.SVGR).loader(use.get('loader')).options(loaderOptions);
105
+ })), rule.oneOf(oneOfId).use(jsUseId).before(CHAIN_ID.USE.SVGR).loader(loaderPath).options(loaderOptions);
110
106
  }
111
107
  return !0;
112
108
  });
package/dist/loader.mjs CHANGED
@@ -1,37 +1,41 @@
1
1
  import { normalize } from "node:path";
2
- import { callbackify } from "node:util";
3
- import { transform } from "@svgr/core";
4
- import plugin_jsx from "@svgr/plugin-jsx";
5
- import plugin_svgo from "@svgr/plugin-svgo";
6
- const transformSvg = callbackify(async (contents, config, state)=>transform(contents, config, state));
7
- const svgrLoader = function(contents) {
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
- const state = {
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
- plugin_svgo,
22
- plugin_jsx
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,32 +1,25 @@
1
1
  {
2
2
  "name": "@rsbuild/plugin-svgr",
3
- "version": "2.0.5",
3
+ "version": "2.0.6",
4
4
  "description": "SVGR plugin for Rsbuild",
5
5
  "homepage": "https://rsbuild.rs",
6
6
  "bugs": {
7
7
  "url": "https://github.com/web-infra-dev/rsbuild/issues"
8
8
  },
9
- "license": "MIT",
10
9
  "repository": {
11
10
  "type": "git",
12
11
  "url": "https://github.com/web-infra-dev/rsbuild",
13
12
  "directory": "packages/plugin-svgr"
14
13
  },
15
- "files": [
16
- "dist"
17
- ],
14
+ "license": "MIT",
18
15
  "type": "module",
19
- "types": "./dist/index.d.ts",
20
16
  "exports": {
21
- ".": {
22
- "types": "./dist/index.d.ts",
23
- "default": "./dist/index.js"
24
- }
25
- },
26
- "publishConfig": {
27
- "access": "public",
28
- "registry": "https://registry.npmjs.org/"
17
+ ".": "./dist/index.js"
29
18
  },
19
+ "types": "./dist/index.d.ts",
20
+ "files": [
21
+ "dist"
22
+ ],
30
23
  "dependencies": {
31
24
  "@rsbuild/plugin-react": "^2.0.0",
32
25
  "@svgr/core": "8.1.0",
@@ -36,13 +29,11 @@
36
29
  "loader-utils": "^3.3.1"
37
30
  },
38
31
  "devDependencies": {
39
- "@rsbuild/core-v1": "npm:@rsbuild/core@^1.7.5",
40
- "@rslib/core": "0.23.1",
41
- "@types/node": "^24.13.2",
42
- "svgo": "^3.3.3",
43
- "typescript": "^6.0.3",
44
- "@rsbuild/core": "2.1.2",
45
- "@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"
46
37
  },
47
38
  "peerDependencies": {
48
39
  "@rsbuild/core": "^2.0.0"
@@ -52,8 +43,13 @@
52
43
  "optional": true
53
44
  }
54
45
  },
46
+ "publishConfig": {
47
+ "access": "public",
48
+ "registry": "https://registry.npmjs.org/"
49
+ },
55
50
  "scripts": {
56
- "build": "rslib",
57
- "dev": "rslib -w"
51
+ "build": "rs lib",
52
+ "dev": "rs lib -w",
53
+ "test": "rs test"
58
54
  }
59
55
  }