@rsbuild/plugin-svgr 0.4.14 → 0.5.0

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/index.d.ts CHANGED
@@ -9,10 +9,10 @@ type PluginSvgrOptions = {
9
9
  */
10
10
  svgrOptions?: Config;
11
11
  /**
12
- * Configure the default export type of SVG files.
13
- * @default 'url'
12
+ * Whether to allow the use of default import and named import at the same time.
13
+ * @default false
14
14
  */
15
- svgDefaultExport?: SvgDefaultExport;
15
+ mixedImport?: boolean;
16
16
  };
17
17
  declare const pluginSvgr: (options?: PluginSvgrOptions) => RsbuildPlugin;
18
18
 
package/dist/index.js CHANGED
@@ -59,7 +59,6 @@ var pluginSvgr = (options = {}) => ({
59
59
  setup(api) {
60
60
  api.modifyBundlerChain(async (chain, { isProd, CHAIN_ID }) => {
61
61
  const config = api.getNormalizedConfig();
62
- const { svgDefaultExport = "url" } = options;
63
62
  const distDir = (0, import_shared.getDistPath)(config, "svg");
64
63
  const filename = (0, import_shared.getFilename)(config, "svg", isProd);
65
64
  const outputName = import_node_path.default.posix.join(distDir, filename);
@@ -82,39 +81,44 @@ var pluginSvgr = (options = {}) => ({
82
81
  ...svgrOptions,
83
82
  exportType: "default"
84
83
  }).end();
84
+ const { mixedImport = false } = options;
85
+ if (mixedImport || svgrOptions.exportType) {
86
+ const { exportType = mixedImport ? "named" : void 0 } = svgrOptions;
87
+ const svgRule = rule.oneOf(CHAIN_ID.ONE_OF.SVG).type("javascript/auto").set("issuer", [import_shared.SCRIPT_REGEX, /\.mdx$/]).use(CHAIN_ID.USE.SVGR).loader(import_node_path.default.resolve(__dirname, "./loader")).options({
88
+ ...svgrOptions,
89
+ exportType
90
+ }).end();
91
+ if (mixedImport && exportType === "named") {
92
+ svgRule.use(CHAIN_ID.USE.URL).loader(import_node_path.default.join(__dirname, "../compiled", "url-loader")).options({
93
+ limit: maxSize,
94
+ name: outputName
95
+ });
96
+ }
97
+ }
85
98
  rule.oneOf(CHAIN_ID.ONE_OF.SVG_ASSET).type("asset").parser({
99
+ // Inline SVG if size < maxSize
86
100
  dataUrlCondition: {
87
101
  maxSize
88
102
  }
89
103
  }).set("generator", {
90
104
  filename: outputName
91
- }).set("issuer", {
92
- // The issuer option ensures that SVGR will only apply if the SVG is imported from a JS file.
93
- not: [import_shared.SCRIPT_REGEX]
94
105
  });
95
- const exportType = svgDefaultExport === "url" ? "named" : "default";
96
- rule.oneOf(CHAIN_ID.ONE_OF.SVG).type("javascript/auto").use(CHAIN_ID.USE.SVGR).loader(import_node_path.default.resolve(__dirname, "./loader")).options({
97
- ...svgrOptions,
98
- exportType
99
- }).end().when(
100
- exportType === "named",
101
- (c) => c.use(CHAIN_ID.USE.URL).loader(import_node_path.default.join(__dirname, "../compiled", "url-loader")).options({
102
- limit: maxSize,
103
- name: outputName
104
- })
105
- );
106
106
  const jsRule = chain.module.rules.get(CHAIN_ID.RULE.JS);
107
107
  [CHAIN_ID.USE.SWC, CHAIN_ID.USE.BABEL].some((jsUseId) => {
108
108
  const use = jsRule.uses.get(jsUseId);
109
- if (use) {
110
- [CHAIN_ID.ONE_OF.SVG, CHAIN_ID.ONE_OF.SVG_REACT].forEach(
111
- (oneOfId) => {
112
- rule.oneOf(oneOfId).use(jsUseId).before(CHAIN_ID.USE.SVGR).loader(use.get("loader")).options(use.get("options"));
113
- }
114
- );
115
- return true;
109
+ if (!use) {
110
+ return false;
111
+ }
112
+ for (const oneOfId of [
113
+ CHAIN_ID.ONE_OF.SVG,
114
+ CHAIN_ID.ONE_OF.SVG_REACT
115
+ ]) {
116
+ if (!rule.oneOfs.has(oneOfId)) {
117
+ continue;
118
+ }
119
+ rule.oneOf(oneOfId).use(jsUseId).before(CHAIN_ID.USE.SVGR).loader(use.get("loader")).options(use.get("options"));
116
120
  }
117
- return false;
121
+ return true;
118
122
  });
119
123
  });
120
124
  }
package/dist/index.mjs CHANGED
@@ -2,7 +2,7 @@ import { createRequire } from 'module';
2
2
  var require = createRequire(import.meta['url']);
3
3
 
4
4
 
5
- // ../../node_modules/.pnpm/@modern-js+module-tools@2.48.0/node_modules/@modern-js/module-tools/shims/esm.js
5
+ // ../../node_modules/.pnpm/@modern-js+module-tools@2.48.2/node_modules/@modern-js/module-tools/shims/esm.js
6
6
  import { fileURLToPath } from "url";
7
7
  import path from "path";
8
8
  var getFilename = () => fileURLToPath(import.meta.url);
@@ -42,7 +42,6 @@ var pluginSvgr = (options = {}) => ({
42
42
  setup(api) {
43
43
  api.modifyBundlerChain(async (chain, { isProd, CHAIN_ID }) => {
44
44
  const config = api.getNormalizedConfig();
45
- const { svgDefaultExport = "url" } = options;
46
45
  const distDir = getDistPath(config, "svg");
47
46
  const filename = getFilename2(config, "svg", isProd);
48
47
  const outputName = path2.posix.join(distDir, filename);
@@ -65,39 +64,44 @@ var pluginSvgr = (options = {}) => ({
65
64
  ...svgrOptions,
66
65
  exportType: "default"
67
66
  }).end();
67
+ const { mixedImport = false } = options;
68
+ if (mixedImport || svgrOptions.exportType) {
69
+ const { exportType = mixedImport ? "named" : void 0 } = svgrOptions;
70
+ const svgRule = rule.oneOf(CHAIN_ID.ONE_OF.SVG).type("javascript/auto").set("issuer", [SCRIPT_REGEX, /\.mdx$/]).use(CHAIN_ID.USE.SVGR).loader(path2.resolve(__dirname, "./loader")).options({
71
+ ...svgrOptions,
72
+ exportType
73
+ }).end();
74
+ if (mixedImport && exportType === "named") {
75
+ svgRule.use(CHAIN_ID.USE.URL).loader(path2.join(__dirname, "../compiled", "url-loader")).options({
76
+ limit: maxSize,
77
+ name: outputName
78
+ });
79
+ }
80
+ }
68
81
  rule.oneOf(CHAIN_ID.ONE_OF.SVG_ASSET).type("asset").parser({
82
+ // Inline SVG if size < maxSize
69
83
  dataUrlCondition: {
70
84
  maxSize
71
85
  }
72
86
  }).set("generator", {
73
87
  filename: outputName
74
- }).set("issuer", {
75
- // The issuer option ensures that SVGR will only apply if the SVG is imported from a JS file.
76
- not: [SCRIPT_REGEX]
77
88
  });
78
- const exportType = svgDefaultExport === "url" ? "named" : "default";
79
- rule.oneOf(CHAIN_ID.ONE_OF.SVG).type("javascript/auto").use(CHAIN_ID.USE.SVGR).loader(path2.resolve(__dirname, "./loader")).options({
80
- ...svgrOptions,
81
- exportType
82
- }).end().when(
83
- exportType === "named",
84
- (c) => c.use(CHAIN_ID.USE.URL).loader(path2.join(__dirname, "../compiled", "url-loader")).options({
85
- limit: maxSize,
86
- name: outputName
87
- })
88
- );
89
89
  const jsRule = chain.module.rules.get(CHAIN_ID.RULE.JS);
90
90
  [CHAIN_ID.USE.SWC, CHAIN_ID.USE.BABEL].some((jsUseId) => {
91
91
  const use = jsRule.uses.get(jsUseId);
92
- if (use) {
93
- [CHAIN_ID.ONE_OF.SVG, CHAIN_ID.ONE_OF.SVG_REACT].forEach(
94
- (oneOfId) => {
95
- rule.oneOf(oneOfId).use(jsUseId).before(CHAIN_ID.USE.SVGR).loader(use.get("loader")).options(use.get("options"));
96
- }
97
- );
98
- return true;
92
+ if (!use) {
93
+ return false;
94
+ }
95
+ for (const oneOfId of [
96
+ CHAIN_ID.ONE_OF.SVG,
97
+ CHAIN_ID.ONE_OF.SVG_REACT
98
+ ]) {
99
+ if (!rule.oneOfs.has(oneOfId)) {
100
+ continue;
101
+ }
102
+ rule.oneOf(oneOfId).use(jsUseId).before(CHAIN_ID.USE.SVGR).loader(use.get("loader")).options(use.get("options"));
99
103
  }
100
- return false;
104
+ return true;
101
105
  });
102
106
  });
103
107
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rsbuild/plugin-svgr",
3
- "version": "0.4.14",
3
+ "version": "0.5.0",
4
4
  "description": "svgr plugin for Rsbuild",
5
5
  "repository": {
6
6
  "type": "git",
@@ -26,17 +26,17 @@
26
26
  "@svgr/core": "8.1.0",
27
27
  "@svgr/plugin-jsx": "8.1.0",
28
28
  "@svgr/plugin-svgo": "8.1.0",
29
- "@rsbuild/shared": "0.4.14",
30
- "@rsbuild/plugin-react": "0.4.14"
29
+ "@rsbuild/shared": "0.5.0",
30
+ "@rsbuild/plugin-react": "0.5.0"
31
31
  },
32
32
  "devDependencies": {
33
33
  "@types/node": "16.x",
34
34
  "typescript": "^5.4.2",
35
- "@rsbuild/core": "0.4.14",
36
- "@scripts/test-helper": "0.4.14"
35
+ "@rsbuild/core": "0.5.0",
36
+ "@scripts/test-helper": "0.5.0"
37
37
  },
38
38
  "peerDependencies": {
39
- "@rsbuild/core": "^0.4.14"
39
+ "@rsbuild/core": "^0.5.0"
40
40
  },
41
41
  "publishConfig": {
42
42
  "access": "public",