@rsbuild/plugin-svgr 1.0.2 → 1.0.4

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.cjs CHANGED
@@ -1,175 +1,216 @@
1
1
  "use strict";
2
- var __create = Object.create;
3
- var __defProp = Object.defineProperty;
4
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
- var __getOwnPropNames = Object.getOwnPropertyNames;
6
- var __getProtoOf = Object.getPrototypeOf;
7
- var __hasOwnProp = Object.prototype.hasOwnProperty;
8
- var __export = (target, all) => {
9
- for (var name in all)
10
- __defProp(target, name, { get: all[name], enumerable: true });
11
- };
12
- var __copyProps = (to, from, except, desc) => {
13
- if (from && typeof from === "object" || typeof from === "function") {
14
- for (let key of __getOwnPropNames(from))
15
- if (!__hasOwnProp.call(to, key) && key !== except)
16
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
- }
18
- return to;
19
- };
20
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
- // If the importer is in node compatibility mode or this is not an ESM
22
- // file that has been converted to a CommonJS file using a Babel-
23
- // compatible transform (i.e. "__esModule" has not been set), then set
24
- // "default" to the CommonJS "module.exports" for node compatibility.
25
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
- mod
27
- ));
28
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
-
30
- // src/index.ts
31
- var src_exports = {};
32
- __export(src_exports, {
33
- PLUGIN_SVGR_NAME: () => PLUGIN_SVGR_NAME,
34
- pluginSvgr: () => pluginSvgr
2
+ // The require scope
3
+ var __webpack_require__ = {};
4
+ /************************************************************************/ // webpack/runtime/compat_get_default_export
5
+ (()=>{
6
+ // getDefaultExport function for compatibility with non-harmony modules
7
+ __webpack_require__.n = function(module) {
8
+ var getter = module && module.__esModule ? function() {
9
+ return module['default'];
10
+ } : function() {
11
+ return module;
12
+ };
13
+ __webpack_require__.d(getter, {
14
+ a: getter
15
+ });
16
+ return getter;
17
+ };
18
+ })();
19
+ // webpack/runtime/define_property_getters
20
+ (()=>{
21
+ __webpack_require__.d = function(exports1, definition) {
22
+ for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
23
+ enumerable: true,
24
+ get: definition[key]
25
+ });
26
+ };
27
+ })();
28
+ // webpack/runtime/has_own_property
29
+ (()=>{
30
+ __webpack_require__.o = function(obj, prop) {
31
+ return Object.prototype.hasOwnProperty.call(obj, prop);
32
+ };
33
+ })();
34
+ // webpack/runtime/make_namespace_object
35
+ (()=>{
36
+ // define __esModule on exports
37
+ __webpack_require__.r = function(exports1) {
38
+ if ('undefined' != typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
39
+ value: 'Module'
40
+ });
41
+ Object.defineProperty(exports1, '__esModule', {
42
+ value: true
43
+ });
44
+ };
45
+ })();
46
+ /************************************************************************/ var __webpack_exports__ = {};
47
+ // ESM COMPAT FLAG
48
+ __webpack_require__.r(__webpack_exports__);
49
+ // EXPORTS
50
+ __webpack_require__.d(__webpack_exports__, {
51
+ PLUGIN_SVGR_NAME: ()=>/* binding */ PLUGIN_SVGR_NAME,
52
+ pluginSvgr: ()=>/* binding */ pluginSvgr
35
53
  });
36
- module.exports = __toCommonJS(src_exports);
37
- var import_node_path = __toESM(require("path"));
38
- var import_plugin_react = require("@rsbuild/plugin-react");
39
- var import_deepmerge = __toESM(require("deepmerge"));
40
- var SVG_REGEX = /\.svg$/;
41
- var getSvgoDefaultConfig = () => ({
42
- plugins: [
43
- {
44
- name: "preset-default",
45
- params: {
46
- overrides: {
47
- // viewBox is required to resize SVGs with CSS.
48
- // @see https://github.com/svg/svgo/issues/1128
49
- removeViewBox: false
50
- }
51
- }
52
- },
53
- "prefixIds"
54
- ]
55
- });
56
- var dedupeSvgoPlugins = (config) => {
57
- if (!config.plugins) {
58
- return config;
59
- }
60
- let mergedPlugins = [];
61
- for (const plugin of config.plugins) {
62
- if (typeof plugin === "string") {
63
- const exist = mergedPlugins.find(
64
- (item) => item === plugin || typeof item === "object" && item.name === plugin
65
- );
66
- if (!exist) {
67
- mergedPlugins.push(plugin);
68
- }
69
- continue;
70
- }
71
- const strIndex = mergedPlugins.findIndex(
72
- (item) => typeof item === "string" && item === plugin.name
73
- );
74
- if (strIndex !== -1) {
75
- mergedPlugins[strIndex] = plugin;
76
- continue;
77
- }
78
- let isMerged = false;
79
- mergedPlugins = mergedPlugins.map((item) => {
80
- if (typeof item === "object" && item.name === plugin.name) {
81
- isMerged = true;
82
- return (0, import_deepmerge.default)(item, plugin);
83
- }
84
- return item;
54
+ const external_node_path_namespaceObject = require("node:path");
55
+ var external_node_path_default = /*#__PURE__*/ __webpack_require__.n(external_node_path_namespaceObject);
56
+ const plugin_react_namespaceObject = require("@rsbuild/plugin-react");
57
+ const external_deepmerge_namespaceObject = require("deepmerge");
58
+ var external_deepmerge_default = /*#__PURE__*/ __webpack_require__.n(external_deepmerge_namespaceObject);
59
+ const SVG_REGEX = /\.svg$/;
60
+ const getSvgoDefaultConfig = ()=>({
61
+ plugins: [
62
+ {
63
+ name: 'preset-default',
64
+ params: {
65
+ overrides: {
66
+ // viewBox is required to resize SVGs with CSS.
67
+ // @see https://github.com/svg/svgo/issues/1128
68
+ removeViewBox: false
69
+ }
70
+ }
71
+ },
72
+ 'prefixIds'
73
+ ]
85
74
  });
86
- if (!isMerged) {
87
- mergedPlugins.push(plugin);
88
- }
89
- }
90
- config.plugins = mergedPlugins;
91
- return config;
92
- };
93
- var PLUGIN_SVGR_NAME = "rsbuild:svgr";
94
- var pluginSvgr = (options = {}) => ({
95
- name: PLUGIN_SVGR_NAME,
96
- pre: [import_plugin_react.PLUGIN_REACT_NAME],
97
- setup(api) {
98
- api.modifyBundlerChain(async (chain, { CHAIN_ID, environment }) => {
99
- const { config } = environment;
100
- const { dataUriLimit } = config.output;
101
- const maxSize = typeof dataUriLimit === "number" ? dataUriLimit : dataUriLimit.svg;
102
- let generatorOptions = {};
103
- if (chain.module.rules.has(CHAIN_ID.RULE.SVG)) {
104
- generatorOptions = chain.module.rules.get(CHAIN_ID.RULE.SVG).oneOfs.get(CHAIN_ID.ONE_OF.SVG_URL).get("generator");
105
- chain.module.rules.delete(CHAIN_ID.RULE.SVG);
106
- }
107
- const rule = chain.module.rule(CHAIN_ID.RULE.SVG).test(SVG_REGEX);
108
- const svgrOptions = (0, import_deepmerge.default)(
109
- {
110
- svgo: true,
111
- svgoConfig: getSvgoDefaultConfig()
112
- },
113
- options.svgrOptions || {}
114
- );
115
- svgrOptions.svgoConfig = dedupeSvgoPlugins(svgrOptions.svgoConfig);
116
- rule.oneOf(CHAIN_ID.ONE_OF.SVG_URL).type("asset/resource").resourceQuery(/(__inline=false|url)/).set("generator", generatorOptions);
117
- rule.oneOf(CHAIN_ID.ONE_OF.SVG_INLINE).type("asset/inline").resourceQuery(/inline/);
118
- rule.oneOf(CHAIN_ID.ONE_OF.SVG_REACT).type("javascript/auto").resourceQuery(options.query || /react/).use(CHAIN_ID.USE.SVGR).loader(import_node_path.default.resolve(__dirname, "./loader.cjs")).options({
119
- ...svgrOptions,
120
- exportType: "default"
121
- }).end();
122
- const { mixedImport = false } = options;
123
- if (mixedImport || svgrOptions.exportType) {
124
- const { exportType = mixedImport ? "named" : void 0 } = svgrOptions;
125
- const issuerInclude = [
126
- /\.(?:js|jsx|mjs|cjs|ts|tsx|mts|cts)$/,
127
- /\.mdx$/
128
- ];
129
- const issuer = options.excludeImporter ? { and: [issuerInclude, { not: options.excludeImporter }] } : issuerInclude;
130
- const svgRule = rule.oneOf(CHAIN_ID.ONE_OF.SVG);
131
- if (options.exclude) {
132
- svgRule.exclude.add(options.exclude);
133
- }
134
- svgRule.type("javascript/auto").set("issuer", issuer).use(CHAIN_ID.USE.SVGR).loader(import_node_path.default.resolve(__dirname, "./loader.cjs")).options({
135
- ...svgrOptions,
136
- exportType
137
- }).end();
138
- if (mixedImport && exportType === "named") {
139
- svgRule.use(CHAIN_ID.USE.URL).loader(import_node_path.default.join(__dirname, "../compiled", "url-loader/index.js")).options({
140
- limit: maxSize,
141
- name: generatorOptions?.filename
142
- });
143
- }
144
- }
145
- rule.oneOf(CHAIN_ID.ONE_OF.SVG_ASSET).type("asset").parser({
146
- // Inline SVG if size < maxSize
147
- dataUrlCondition: {
148
- maxSize
149
- }
150
- }).set("generator", generatorOptions);
151
- const jsRule = chain.module.rules.get(CHAIN_ID.RULE.JS);
152
- [CHAIN_ID.USE.SWC, CHAIN_ID.USE.BABEL].some((jsUseId) => {
153
- const use = jsRule.uses.get(jsUseId);
154
- if (!use) {
155
- return false;
75
+ /**
76
+ * Dedupe SVGO plugins config.
77
+ *
78
+ * @example
79
+ * Input:
80
+ * {
81
+ * plugins: [
82
+ * { name: 'preset-default', params: { foo: true }],
83
+ * { name: 'preset-default', params: { bar: true }],
84
+ * ]
85
+ * }
86
+ * Output:
87
+ * {
88
+ * plugins: [
89
+ * { name: 'preset-default', params: { foo: true, bar: true }],
90
+ * ]
91
+ * }
92
+ */ const dedupeSvgoPlugins = (config)=>{
93
+ if (!config.plugins) return config;
94
+ let mergedPlugins = [];
95
+ for (const plugin of config.plugins){
96
+ if ('string' == typeof plugin) {
97
+ const exist = mergedPlugins.find((item)=>item === plugin || 'object' == typeof item && item.name === plugin);
98
+ if (!exist) mergedPlugins.push(plugin);
99
+ continue;
156
100
  }
157
- for (const oneOfId of [
158
- CHAIN_ID.ONE_OF.SVG,
159
- CHAIN_ID.ONE_OF.SVG_REACT
160
- ]) {
161
- if (!rule.oneOfs.has(oneOfId)) {
101
+ const strIndex = mergedPlugins.findIndex((item)=>'string' == typeof item && item === plugin.name);
102
+ if (-1 !== strIndex) {
103
+ mergedPlugins[strIndex] = plugin;
162
104
  continue;
163
- }
164
- rule.oneOf(oneOfId).use(jsUseId).before(CHAIN_ID.USE.SVGR).loader(use.get("loader")).options(use.get("options"));
165
105
  }
166
- return true;
167
- });
168
- });
169
- }
170
- });
171
- // Annotate the CommonJS export names for ESM import in node:
172
- 0 && (module.exports = {
173
- PLUGIN_SVGR_NAME,
174
- pluginSvgr
106
+ let isMerged = false;
107
+ mergedPlugins = mergedPlugins.map((item)=>{
108
+ if ('object' == typeof item && item.name === plugin.name) {
109
+ isMerged = true;
110
+ return external_deepmerge_default()(item, plugin);
111
+ }
112
+ return item;
113
+ });
114
+ if (!isMerged) mergedPlugins.push(plugin);
115
+ }
116
+ config.plugins = mergedPlugins;
117
+ return config;
118
+ };
119
+ const PLUGIN_SVGR_NAME = 'rsbuild:svgr';
120
+ const pluginSvgr = function() {
121
+ let options = arguments.length > 0 && void 0 !== arguments[0] ? arguments[0] : {};
122
+ return {
123
+ name: PLUGIN_SVGR_NAME,
124
+ pre: [
125
+ plugin_react_namespaceObject.PLUGIN_REACT_NAME
126
+ ],
127
+ setup (api) {
128
+ api.modifyBundlerChain(async (chain, param)=>{
129
+ let { CHAIN_ID, environment } = param;
130
+ const { config } = environment;
131
+ const { dataUriLimit } = config.output;
132
+ const maxSize = 'number' == typeof dataUriLimit ? dataUriLimit : dataUriLimit.svg;
133
+ let generatorOptions = {};
134
+ if (chain.module.rules.has(CHAIN_ID.RULE.SVG)) {
135
+ generatorOptions = chain.module.rules.get(CHAIN_ID.RULE.SVG).oneOfs.get(CHAIN_ID.ONE_OF.SVG_URL).get('generator');
136
+ // delete Rsbuild builtin SVG rules
137
+ chain.module.rules.delete(CHAIN_ID.RULE.SVG);
138
+ }
139
+ const rule = chain.module.rule(CHAIN_ID.RULE.SVG).test(SVG_REGEX);
140
+ const svgrOptions = external_deepmerge_default()({
141
+ svgo: true,
142
+ svgoConfig: getSvgoDefaultConfig()
143
+ }, options.svgrOptions || {});
144
+ svgrOptions.svgoConfig = dedupeSvgoPlugins(svgrOptions.svgoConfig);
145
+ // force to url: "foo.svg?url",
146
+ rule.oneOf(CHAIN_ID.ONE_OF.SVG_URL).type('asset/resource').resourceQuery(/(__inline=false|url)/).set('generator', generatorOptions);
147
+ // force to inline: "foo.svg?inline"
148
+ rule.oneOf(CHAIN_ID.ONE_OF.SVG_INLINE).type('asset/inline').resourceQuery(/inline/);
149
+ // force to react component: "foo.svg?react"
150
+ rule.oneOf(CHAIN_ID.ONE_OF.SVG_REACT).type('javascript/auto').resourceQuery(options.query || /react/).use(CHAIN_ID.USE.SVGR).loader(external_node_path_default().resolve(__dirname, './loader.cjs')).options({
151
+ ...svgrOptions,
152
+ exportType: 'default'
153
+ }).end();
154
+ // SVG in JS files
155
+ const { mixedImport = false } = options;
156
+ if (mixedImport || svgrOptions.exportType) {
157
+ const { exportType = mixedImport ? 'named' : void 0 } = svgrOptions;
158
+ const issuerInclude = [
159
+ /\.(?:js|jsx|mjs|cjs|ts|tsx|mts|cts)$/,
160
+ /\.mdx$/
161
+ ];
162
+ const issuer = options.excludeImporter ? {
163
+ and: [
164
+ issuerInclude,
165
+ {
166
+ not: options.excludeImporter
167
+ }
168
+ ]
169
+ } : issuerInclude;
170
+ const svgRule = rule.oneOf(CHAIN_ID.ONE_OF.SVG);
171
+ if (options.exclude) svgRule.exclude.add(options.exclude);
172
+ svgRule.type('javascript/auto') // The issuer option ensures that SVGR will only apply if the SVG is imported from a JS file.
173
+ .set('issuer', issuer).use(CHAIN_ID.USE.SVGR).loader(external_node_path_default().resolve(__dirname, './loader.cjs')).options({
174
+ ...svgrOptions,
175
+ exportType
176
+ }).end();
177
+ /**
178
+ * For mixed import.
179
+ * @example import logoUrl, { ReactComponent } from './logo.svg';`
180
+ */ if (mixedImport && 'named' === exportType) svgRule.use(CHAIN_ID.USE.URL).loader(external_node_path_default().join(__dirname, '../compiled', 'url-loader/index.js')).options({
181
+ limit: maxSize,
182
+ name: null == generatorOptions ? void 0 : generatorOptions.filename
183
+ });
184
+ }
185
+ // SVG as assets
186
+ rule.oneOf(CHAIN_ID.ONE_OF.SVG_ASSET).type('asset').parser({
187
+ // Inline SVG if size < maxSize
188
+ dataUrlCondition: {
189
+ maxSize
190
+ }
191
+ }).set('generator', generatorOptions);
192
+ // apply current JS transform rule to SVGR rules
193
+ const jsRule = chain.module.rules.get(CHAIN_ID.RULE.JS);
194
+ [
195
+ CHAIN_ID.USE.SWC,
196
+ CHAIN_ID.USE.BABEL
197
+ ].some((jsUseId)=>{
198
+ const use = jsRule.uses.get(jsUseId);
199
+ if (!use) return false;
200
+ for (const oneOfId of [
201
+ CHAIN_ID.ONE_OF.SVG,
202
+ CHAIN_ID.ONE_OF.SVG_REACT
203
+ ]){
204
+ if (!!rule.oneOfs.has(oneOfId)) rule.oneOf(oneOfId).use(jsUseId).before(CHAIN_ID.USE.SVGR).loader(use.get('loader')).options(use.get('options'));
205
+ }
206
+ return true;
207
+ });
208
+ });
209
+ }
210
+ };
211
+ };
212
+ var __webpack_export_target__ = exports;
213
+ for(var i in __webpack_exports__)__webpack_export_target__[i] = __webpack_exports__[i];
214
+ if (__webpack_exports__.__esModule) Object.defineProperty(__webpack_export_target__, '__esModule', {
215
+ value: true
175
216
  });