@rsbuild/plugin-svgr 1.0.5 → 1.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/compiled/file-loader/index.js +8 -8
- package/compiled/url-loader/index.js +8 -8
- package/dist/index.cjs +60 -146
- package/dist/index.js +26 -86
- package/dist/loader.mjs +49 -0
- package/package.json +7 -6
- package/dist/loader.cjs +0 -108
package/dist/index.cjs
CHANGED
|
@@ -1,216 +1,130 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
2
|
+
let __rslib_import_meta_url__ = 'undefined' == typeof document ? new (require('url'.replace('', ''))).URL('file:' + __filename).href : document.currentScript && document.currentScript.src || new URL('main.js', document.baseURI).href;
|
|
3
3
|
var __webpack_require__ = {};
|
|
4
|
-
|
|
5
|
-
(
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
return module1['default'];
|
|
10
|
-
} : function() {
|
|
11
|
-
return module1;
|
|
12
|
-
};
|
|
13
|
-
__webpack_require__.d(getter, {
|
|
14
|
-
a: getter
|
|
15
|
-
});
|
|
16
|
-
return getter;
|
|
4
|
+
__webpack_require__.n = function(module) {
|
|
5
|
+
var getter = module && module.__esModule ? function() {
|
|
6
|
+
return module.default;
|
|
7
|
+
} : function() {
|
|
8
|
+
return module;
|
|
17
9
|
};
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
(
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
})
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
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
|
|
10
|
+
return __webpack_require__.d(getter, {
|
|
11
|
+
a: getter
|
|
12
|
+
}), getter;
|
|
13
|
+
}, __webpack_require__.d = function(exports1, definition) {
|
|
14
|
+
for(var key in definition)__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key) && Object.defineProperty(exports1, key, {
|
|
15
|
+
enumerable: !0,
|
|
16
|
+
get: definition[key]
|
|
17
|
+
});
|
|
18
|
+
}, __webpack_require__.o = function(obj, prop) {
|
|
19
|
+
return Object.prototype.hasOwnProperty.call(obj, prop);
|
|
20
|
+
}, __webpack_require__.r = function(exports1) {
|
|
21
|
+
'undefined' != typeof Symbol && Symbol.toStringTag && Object.defineProperty(exports1, Symbol.toStringTag, {
|
|
22
|
+
value: 'Module'
|
|
23
|
+
}), Object.defineProperty(exports1, '__esModule', {
|
|
24
|
+
value: !0
|
|
25
|
+
});
|
|
26
|
+
};
|
|
27
|
+
var __webpack_exports__ = {};
|
|
28
|
+
__webpack_require__.r(__webpack_exports__), __webpack_require__.d(__webpack_exports__, {
|
|
29
|
+
PLUGIN_SVGR_NAME: ()=>PLUGIN_SVGR_NAME,
|
|
30
|
+
pluginSvgr: ()=>pluginSvgr
|
|
53
31
|
});
|
|
54
|
-
|
|
55
|
-
var external_node_path_default =
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
var external_deepmerge_default = /*#__PURE__*/ __webpack_require__.n(external_deepmerge_namespaceObject);
|
|
60
|
-
const src_dirname = external_node_path_default().dirname((0, external_node_url_namespaceObject.fileURLToPath)(/*#__PURE__*/ function() {
|
|
61
|
-
return 'undefined' == typeof document ? new (module.require('url'.replace('', ''))).URL('file:' + __filename).href : document.currentScript && document.currentScript.src || new URL('main.js', document.baseURI).href;
|
|
62
|
-
}()));
|
|
63
|
-
const SVG_REGEX = /\.svg$/;
|
|
64
|
-
const getSvgoDefaultConfig = ()=>({
|
|
32
|
+
let external_node_path_namespaceObject = require("node:path");
|
|
33
|
+
var external_node_path_default = __webpack_require__.n(external_node_path_namespaceObject);
|
|
34
|
+
let external_node_url_namespaceObject = require("node:url"), plugin_react_namespaceObject = require("@rsbuild/plugin-react"), external_deepmerge_namespaceObject = require("deepmerge");
|
|
35
|
+
var external_deepmerge_default = __webpack_require__.n(external_deepmerge_namespaceObject);
|
|
36
|
+
let src_dirname = external_node_path_default().dirname((0, external_node_url_namespaceObject.fileURLToPath)(__rslib_import_meta_url__)), SVG_REGEX = /\.svg$/, getSvgoDefaultConfig = ()=>({
|
|
65
37
|
plugins: [
|
|
66
38
|
{
|
|
67
39
|
name: 'preset-default',
|
|
68
40
|
params: {
|
|
69
41
|
overrides: {
|
|
70
|
-
|
|
71
|
-
// @see https://github.com/svg/svgo/issues/1128
|
|
72
|
-
removeViewBox: false
|
|
42
|
+
removeViewBox: !1
|
|
73
43
|
}
|
|
74
44
|
}
|
|
75
45
|
},
|
|
76
46
|
'prefixIds'
|
|
77
47
|
]
|
|
78
|
-
})
|
|
79
|
-
/**
|
|
80
|
-
* Dedupe SVGO plugins config.
|
|
81
|
-
*
|
|
82
|
-
* @example
|
|
83
|
-
* Input:
|
|
84
|
-
* {
|
|
85
|
-
* plugins: [
|
|
86
|
-
* { name: 'preset-default', params: { foo: true }],
|
|
87
|
-
* { name: 'preset-default', params: { bar: true }],
|
|
88
|
-
* ]
|
|
89
|
-
* }
|
|
90
|
-
* Output:
|
|
91
|
-
* {
|
|
92
|
-
* plugins: [
|
|
93
|
-
* { name: 'preset-default', params: { foo: true, bar: true }],
|
|
94
|
-
* ]
|
|
95
|
-
* }
|
|
96
|
-
*/ const dedupeSvgoPlugins = (config)=>{
|
|
48
|
+
}), dedupeSvgoPlugins = (config)=>{
|
|
97
49
|
if (!config.plugins) return config;
|
|
98
50
|
let mergedPlugins = [];
|
|
99
|
-
for (
|
|
51
|
+
for (let plugin of config.plugins){
|
|
100
52
|
if ('string' == typeof plugin) {
|
|
101
|
-
|
|
102
|
-
if (!exist) mergedPlugins.push(plugin);
|
|
53
|
+
!mergedPlugins.find((item)=>item === plugin || 'object' == typeof item && item.name === plugin) && mergedPlugins.push(plugin);
|
|
103
54
|
continue;
|
|
104
55
|
}
|
|
105
|
-
|
|
56
|
+
let strIndex = mergedPlugins.findIndex((item)=>'string' == typeof item && item === plugin.name);
|
|
106
57
|
if (-1 !== strIndex) {
|
|
107
58
|
mergedPlugins[strIndex] = plugin;
|
|
108
59
|
continue;
|
|
109
60
|
}
|
|
110
|
-
let isMerged =
|
|
111
|
-
mergedPlugins = mergedPlugins.map((item)=>
|
|
112
|
-
if ('object' == typeof item && item.name === plugin.name) {
|
|
113
|
-
isMerged = true;
|
|
114
|
-
return external_deepmerge_default()(item, plugin);
|
|
115
|
-
}
|
|
116
|
-
return item;
|
|
117
|
-
});
|
|
118
|
-
if (!isMerged) mergedPlugins.push(plugin);
|
|
61
|
+
let isMerged = !1;
|
|
62
|
+
mergedPlugins = mergedPlugins.map((item)=>'object' == typeof item && item.name === plugin.name ? (isMerged = !0, external_deepmerge_default()(item, plugin)) : item), !isMerged && mergedPlugins.push(plugin);
|
|
119
63
|
}
|
|
120
|
-
config.plugins = mergedPlugins;
|
|
121
|
-
|
|
122
|
-
};
|
|
123
|
-
const PLUGIN_SVGR_NAME = 'rsbuild:svgr';
|
|
124
|
-
const pluginSvgr = (options = {})=>({
|
|
64
|
+
return config.plugins = mergedPlugins, config;
|
|
65
|
+
}, PLUGIN_SVGR_NAME = 'rsbuild:svgr', pluginSvgr = (options = {})=>({
|
|
125
66
|
name: PLUGIN_SVGR_NAME,
|
|
126
67
|
pre: [
|
|
127
68
|
plugin_react_namespaceObject.PLUGIN_REACT_NAME
|
|
128
69
|
],
|
|
129
70
|
setup (api) {
|
|
130
71
|
api.modifyBundlerChain(async (chain, { CHAIN_ID, environment })=>{
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
if (chain.module.rules.has(CHAIN_ID.RULE.SVG)) {
|
|
136
|
-
generatorOptions = chain.module.rules.get(CHAIN_ID.RULE.SVG).oneOfs.get(CHAIN_ID.ONE_OF.SVG_URL).get('generator');
|
|
137
|
-
// delete Rsbuild builtin SVG rules
|
|
138
|
-
chain.module.rules.delete(CHAIN_ID.RULE.SVG);
|
|
139
|
-
}
|
|
140
|
-
const rule = chain.module.rule(CHAIN_ID.RULE.SVG).test(SVG_REGEX);
|
|
141
|
-
const svgrOptions = external_deepmerge_default()({
|
|
142
|
-
svgo: true,
|
|
72
|
+
let { config } = environment, { dataUriLimit } = config.output, maxSize = 'number' == typeof dataUriLimit ? dataUriLimit : dataUriLimit.svg, generatorOptions = {};
|
|
73
|
+
chain.module.rules.has(CHAIN_ID.RULE.SVG) && (generatorOptions = chain.module.rules.get(CHAIN_ID.RULE.SVG).oneOfs.get(CHAIN_ID.ONE_OF.SVG_URL).get('generator'), chain.module.rules.delete(CHAIN_ID.RULE.SVG));
|
|
74
|
+
let rule = chain.module.rule(CHAIN_ID.RULE.SVG).test(SVG_REGEX), svgrOptions = external_deepmerge_default()({
|
|
75
|
+
svgo: !0,
|
|
143
76
|
svgoConfig: getSvgoDefaultConfig()
|
|
144
77
|
}, options.svgrOptions || {});
|
|
145
|
-
svgrOptions.svgoConfig = dedupeSvgoPlugins(svgrOptions.svgoConfig)
|
|
146
|
-
// force to url: "foo.svg?url",
|
|
147
|
-
rule.oneOf(CHAIN_ID.ONE_OF.SVG_URL).type('asset/resource').resourceQuery(/(__inline=false|url)/).set('generator', generatorOptions);
|
|
148
|
-
// force to inline: "foo.svg?inline"
|
|
149
|
-
rule.oneOf(CHAIN_ID.ONE_OF.SVG_INLINE).type('asset/inline').resourceQuery(/inline/);
|
|
150
|
-
// force to react component: "foo.svg?react"
|
|
151
|
-
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(src_dirname, './loader.cjs')).options({
|
|
78
|
+
svgrOptions.svgoConfig = dedupeSvgoPlugins(svgrOptions.svgoConfig), rule.oneOf(CHAIN_ID.ONE_OF.SVG_URL).type('asset/resource').resourceQuery(/(__inline=false|url)/).set('generator', generatorOptions), rule.oneOf(CHAIN_ID.ONE_OF.SVG_INLINE).type('asset/inline').resourceQuery(/inline/), 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(src_dirname, './loader.mjs')).options({
|
|
152
79
|
...svgrOptions,
|
|
153
80
|
exportType: 'default'
|
|
154
81
|
}).end();
|
|
155
|
-
|
|
156
|
-
const { mixedImport = false } = options;
|
|
82
|
+
let { mixedImport = !1 } = options;
|
|
157
83
|
if (mixedImport || svgrOptions.exportType) {
|
|
158
|
-
|
|
159
|
-
const issuerInclude = [
|
|
84
|
+
let { exportType = mixedImport ? 'named' : void 0 } = svgrOptions, issuerInclude = [
|
|
160
85
|
/\.(?:js|jsx|mjs|cjs|ts|tsx|mts|cts)$/,
|
|
161
86
|
/\.mdx$/
|
|
162
|
-
]
|
|
163
|
-
const issuer = options.excludeImporter ? {
|
|
87
|
+
], issuer = options.excludeImporter ? {
|
|
164
88
|
and: [
|
|
165
89
|
issuerInclude,
|
|
166
90
|
{
|
|
167
91
|
not: options.excludeImporter
|
|
168
92
|
}
|
|
169
93
|
]
|
|
170
|
-
} : issuerInclude;
|
|
171
|
-
|
|
172
|
-
if (options.exclude) svgRule.exclude.add(options.exclude);
|
|
173
|
-
svgRule.type('javascript/auto') // The issuer option ensures that SVGR will only apply if the SVG is imported from a JS file.
|
|
174
|
-
.set('issuer', issuer).use(CHAIN_ID.USE.SVGR).loader(external_node_path_default().resolve(src_dirname, './loader.cjs')).options({
|
|
94
|
+
} : issuerInclude, svgRule = rule.oneOf(CHAIN_ID.ONE_OF.SVG);
|
|
95
|
+
options.exclude && svgRule.exclude.add(options.exclude), svgRule.type('javascript/auto').set('issuer', issuer).use(CHAIN_ID.USE.SVGR).loader(external_node_path_default().resolve(src_dirname, './loader.mjs')).options({
|
|
175
96
|
...svgrOptions,
|
|
176
97
|
exportType
|
|
177
|
-
}).end()
|
|
178
|
-
/**
|
|
179
|
-
* For mixed import.
|
|
180
|
-
* @example import logoUrl, { ReactComponent } from './logo.svg';`
|
|
181
|
-
*/ if (mixedImport && 'named' === exportType) svgRule.use(CHAIN_ID.USE.URL).loader(external_node_path_default().join(src_dirname, '../compiled', 'url-loader/index.js')).options({
|
|
98
|
+
}).end(), mixedImport && 'named' === exportType && svgRule.use(CHAIN_ID.USE.URL).loader(external_node_path_default().join(src_dirname, '../compiled', 'url-loader/index.js')).options({
|
|
182
99
|
limit: maxSize,
|
|
183
100
|
name: null == generatorOptions ? void 0 : generatorOptions.filename
|
|
184
101
|
});
|
|
185
102
|
}
|
|
186
|
-
// SVG as assets
|
|
187
103
|
rule.oneOf(CHAIN_ID.ONE_OF.SVG_ASSET).type('asset').parser({
|
|
188
|
-
// Inline SVG if size < maxSize
|
|
189
104
|
dataUrlCondition: {
|
|
190
105
|
maxSize
|
|
191
106
|
}
|
|
192
107
|
}).set('generator', generatorOptions);
|
|
193
|
-
|
|
194
|
-
const jsRule = chain.module.rules.get(CHAIN_ID.RULE.JS);
|
|
108
|
+
let jsRule = chain.module.rules.get(CHAIN_ID.RULE.JS);
|
|
195
109
|
[
|
|
196
110
|
CHAIN_ID.USE.SWC,
|
|
197
111
|
CHAIN_ID.USE.BABEL
|
|
198
112
|
].some((jsUseId)=>{
|
|
199
|
-
|
|
200
|
-
if (!use) return
|
|
201
|
-
for (
|
|
113
|
+
let use = jsRule.uses.get(jsUseId);
|
|
114
|
+
if (!use) return !1;
|
|
115
|
+
for (let oneOfId of [
|
|
202
116
|
CHAIN_ID.ONE_OF.SVG,
|
|
203
117
|
CHAIN_ID.ONE_OF.SVG_REACT
|
|
204
118
|
]){
|
|
205
119
|
if (!!rule.oneOfs.has(oneOfId)) rule.oneOf(oneOfId).use(jsUseId).before(CHAIN_ID.USE.SVGR).loader(use.get('loader')).options(use.get('options'));
|
|
206
120
|
}
|
|
207
|
-
return
|
|
121
|
+
return !0;
|
|
208
122
|
});
|
|
209
123
|
});
|
|
210
124
|
}
|
|
211
125
|
});
|
|
212
126
|
var __webpack_export_target__ = exports;
|
|
213
|
-
for(var
|
|
214
|
-
|
|
215
|
-
value:
|
|
127
|
+
for(var __webpack_i__ in __webpack_exports__)__webpack_export_target__[__webpack_i__] = __webpack_exports__[__webpack_i__];
|
|
128
|
+
__webpack_exports__.__esModule && Object.defineProperty(__webpack_export_target__, '__esModule', {
|
|
129
|
+
value: !0
|
|
216
130
|
});
|
package/dist/index.js
CHANGED
|
@@ -2,152 +2,92 @@ import * as __WEBPACK_EXTERNAL_MODULE_node_path__ from "node:path";
|
|
|
2
2
|
import * as __WEBPACK_EXTERNAL_MODULE_node_url__ from "node:url";
|
|
3
3
|
import * as __WEBPACK_EXTERNAL_MODULE__rsbuild_plugin_react__ from "@rsbuild/plugin-react";
|
|
4
4
|
import * as __WEBPACK_EXTERNAL_MODULE_deepmerge__ from "deepmerge";
|
|
5
|
-
|
|
6
|
-
const SVG_REGEX = /\.svg$/;
|
|
7
|
-
const getSvgoDefaultConfig = ()=>({
|
|
5
|
+
let src_dirname = __WEBPACK_EXTERNAL_MODULE_node_path__.default.dirname((0, __WEBPACK_EXTERNAL_MODULE_node_url__.fileURLToPath)(import.meta.url)), SVG_REGEX = /\.svg$/, getSvgoDefaultConfig = ()=>({
|
|
8
6
|
plugins: [
|
|
9
7
|
{
|
|
10
8
|
name: 'preset-default',
|
|
11
9
|
params: {
|
|
12
10
|
overrides: {
|
|
13
|
-
|
|
14
|
-
// @see https://github.com/svg/svgo/issues/1128
|
|
15
|
-
removeViewBox: false
|
|
11
|
+
removeViewBox: !1
|
|
16
12
|
}
|
|
17
13
|
}
|
|
18
14
|
},
|
|
19
15
|
'prefixIds'
|
|
20
16
|
]
|
|
21
|
-
})
|
|
22
|
-
/**
|
|
23
|
-
* Dedupe SVGO plugins config.
|
|
24
|
-
*
|
|
25
|
-
* @example
|
|
26
|
-
* Input:
|
|
27
|
-
* {
|
|
28
|
-
* plugins: [
|
|
29
|
-
* { name: 'preset-default', params: { foo: true }],
|
|
30
|
-
* { name: 'preset-default', params: { bar: true }],
|
|
31
|
-
* ]
|
|
32
|
-
* }
|
|
33
|
-
* Output:
|
|
34
|
-
* {
|
|
35
|
-
* plugins: [
|
|
36
|
-
* { name: 'preset-default', params: { foo: true, bar: true }],
|
|
37
|
-
* ]
|
|
38
|
-
* }
|
|
39
|
-
*/ const dedupeSvgoPlugins = (config)=>{
|
|
17
|
+
}), dedupeSvgoPlugins = (config)=>{
|
|
40
18
|
if (!config.plugins) return config;
|
|
41
19
|
let mergedPlugins = [];
|
|
42
|
-
for (
|
|
20
|
+
for (let plugin of config.plugins){
|
|
43
21
|
if ('string' == typeof plugin) {
|
|
44
|
-
|
|
45
|
-
if (!exist) mergedPlugins.push(plugin);
|
|
22
|
+
!mergedPlugins.find((item)=>item === plugin || 'object' == typeof item && item.name === plugin) && mergedPlugins.push(plugin);
|
|
46
23
|
continue;
|
|
47
24
|
}
|
|
48
|
-
|
|
25
|
+
let strIndex = mergedPlugins.findIndex((item)=>'string' == typeof item && item === plugin.name);
|
|
49
26
|
if (-1 !== strIndex) {
|
|
50
27
|
mergedPlugins[strIndex] = plugin;
|
|
51
28
|
continue;
|
|
52
29
|
}
|
|
53
|
-
let isMerged =
|
|
54
|
-
mergedPlugins = mergedPlugins.map((item)=>
|
|
55
|
-
if ('object' == typeof item && item.name === plugin.name) {
|
|
56
|
-
isMerged = true;
|
|
57
|
-
return (0, __WEBPACK_EXTERNAL_MODULE_deepmerge__["default"])(item, plugin);
|
|
58
|
-
}
|
|
59
|
-
return item;
|
|
60
|
-
});
|
|
61
|
-
if (!isMerged) mergedPlugins.push(plugin);
|
|
30
|
+
let isMerged = !1;
|
|
31
|
+
mergedPlugins = mergedPlugins.map((item)=>'object' == typeof item && item.name === plugin.name ? (isMerged = !0, (0, __WEBPACK_EXTERNAL_MODULE_deepmerge__.default)(item, plugin)) : item), !isMerged && mergedPlugins.push(plugin);
|
|
62
32
|
}
|
|
63
|
-
config.plugins = mergedPlugins;
|
|
64
|
-
|
|
65
|
-
};
|
|
66
|
-
const PLUGIN_SVGR_NAME = 'rsbuild:svgr';
|
|
67
|
-
const pluginSvgr = (options = {})=>({
|
|
33
|
+
return config.plugins = mergedPlugins, config;
|
|
34
|
+
}, PLUGIN_SVGR_NAME = 'rsbuild:svgr', pluginSvgr = (options = {})=>({
|
|
68
35
|
name: PLUGIN_SVGR_NAME,
|
|
69
36
|
pre: [
|
|
70
37
|
__WEBPACK_EXTERNAL_MODULE__rsbuild_plugin_react__.PLUGIN_REACT_NAME
|
|
71
38
|
],
|
|
72
39
|
setup (api) {
|
|
73
40
|
api.modifyBundlerChain(async (chain, { CHAIN_ID, environment })=>{
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
if (chain.module.rules.has(CHAIN_ID.RULE.SVG)) {
|
|
79
|
-
generatorOptions = chain.module.rules.get(CHAIN_ID.RULE.SVG).oneOfs.get(CHAIN_ID.ONE_OF.SVG_URL).get('generator');
|
|
80
|
-
// delete Rsbuild builtin SVG rules
|
|
81
|
-
chain.module.rules.delete(CHAIN_ID.RULE.SVG);
|
|
82
|
-
}
|
|
83
|
-
const rule = chain.module.rule(CHAIN_ID.RULE.SVG).test(SVG_REGEX);
|
|
84
|
-
const svgrOptions = (0, __WEBPACK_EXTERNAL_MODULE_deepmerge__["default"])({
|
|
85
|
-
svgo: true,
|
|
41
|
+
let { config } = environment, { dataUriLimit } = config.output, maxSize = 'number' == typeof dataUriLimit ? dataUriLimit : dataUriLimit.svg, generatorOptions = {};
|
|
42
|
+
chain.module.rules.has(CHAIN_ID.RULE.SVG) && (generatorOptions = chain.module.rules.get(CHAIN_ID.RULE.SVG).oneOfs.get(CHAIN_ID.ONE_OF.SVG_URL).get('generator'), chain.module.rules.delete(CHAIN_ID.RULE.SVG));
|
|
43
|
+
let rule = chain.module.rule(CHAIN_ID.RULE.SVG).test(SVG_REGEX), svgrOptions = (0, __WEBPACK_EXTERNAL_MODULE_deepmerge__.default)({
|
|
44
|
+
svgo: !0,
|
|
86
45
|
svgoConfig: getSvgoDefaultConfig()
|
|
87
46
|
}, options.svgrOptions || {});
|
|
88
|
-
svgrOptions.svgoConfig = dedupeSvgoPlugins(svgrOptions.svgoConfig)
|
|
89
|
-
// force to url: "foo.svg?url",
|
|
90
|
-
rule.oneOf(CHAIN_ID.ONE_OF.SVG_URL).type('asset/resource').resourceQuery(/(__inline=false|url)/).set('generator', generatorOptions);
|
|
91
|
-
// force to inline: "foo.svg?inline"
|
|
92
|
-
rule.oneOf(CHAIN_ID.ONE_OF.SVG_INLINE).type('asset/inline').resourceQuery(/inline/);
|
|
93
|
-
// force to react component: "foo.svg?react"
|
|
94
|
-
rule.oneOf(CHAIN_ID.ONE_OF.SVG_REACT).type('javascript/auto').resourceQuery(options.query || /react/).use(CHAIN_ID.USE.SVGR).loader(__WEBPACK_EXTERNAL_MODULE_node_path__["default"].resolve(src_dirname, './loader.cjs')).options({
|
|
47
|
+
svgrOptions.svgoConfig = dedupeSvgoPlugins(svgrOptions.svgoConfig), rule.oneOf(CHAIN_ID.ONE_OF.SVG_URL).type('asset/resource').resourceQuery(/(__inline=false|url)/).set('generator', generatorOptions), rule.oneOf(CHAIN_ID.ONE_OF.SVG_INLINE).type('asset/inline').resourceQuery(/inline/), rule.oneOf(CHAIN_ID.ONE_OF.SVG_REACT).type('javascript/auto').resourceQuery(options.query || /react/).use(CHAIN_ID.USE.SVGR).loader(__WEBPACK_EXTERNAL_MODULE_node_path__.default.resolve(src_dirname, './loader.mjs')).options({
|
|
95
48
|
...svgrOptions,
|
|
96
49
|
exportType: 'default'
|
|
97
50
|
}).end();
|
|
98
|
-
|
|
99
|
-
const { mixedImport = false } = options;
|
|
51
|
+
let { mixedImport = !1 } = options;
|
|
100
52
|
if (mixedImport || svgrOptions.exportType) {
|
|
101
|
-
|
|
102
|
-
const issuerInclude = [
|
|
53
|
+
let { exportType = mixedImport ? 'named' : void 0 } = svgrOptions, issuerInclude = [
|
|
103
54
|
/\.(?:js|jsx|mjs|cjs|ts|tsx|mts|cts)$/,
|
|
104
55
|
/\.mdx$/
|
|
105
|
-
]
|
|
106
|
-
const issuer = options.excludeImporter ? {
|
|
56
|
+
], issuer = options.excludeImporter ? {
|
|
107
57
|
and: [
|
|
108
58
|
issuerInclude,
|
|
109
59
|
{
|
|
110
60
|
not: options.excludeImporter
|
|
111
61
|
}
|
|
112
62
|
]
|
|
113
|
-
} : issuerInclude;
|
|
114
|
-
|
|
115
|
-
if (options.exclude) svgRule.exclude.add(options.exclude);
|
|
116
|
-
svgRule.type('javascript/auto') // The issuer option ensures that SVGR will only apply if the SVG is imported from a JS file.
|
|
117
|
-
.set('issuer', issuer).use(CHAIN_ID.USE.SVGR).loader(__WEBPACK_EXTERNAL_MODULE_node_path__["default"].resolve(src_dirname, './loader.cjs')).options({
|
|
63
|
+
} : issuerInclude, svgRule = rule.oneOf(CHAIN_ID.ONE_OF.SVG);
|
|
64
|
+
options.exclude && svgRule.exclude.add(options.exclude), svgRule.type('javascript/auto').set('issuer', issuer).use(CHAIN_ID.USE.SVGR).loader(__WEBPACK_EXTERNAL_MODULE_node_path__.default.resolve(src_dirname, './loader.mjs')).options({
|
|
118
65
|
...svgrOptions,
|
|
119
66
|
exportType
|
|
120
|
-
}).end()
|
|
121
|
-
/**
|
|
122
|
-
* For mixed import.
|
|
123
|
-
* @example import logoUrl, { ReactComponent } from './logo.svg';`
|
|
124
|
-
*/ if (mixedImport && 'named' === exportType) svgRule.use(CHAIN_ID.USE.URL).loader(__WEBPACK_EXTERNAL_MODULE_node_path__["default"].join(src_dirname, '../compiled', 'url-loader/index.js')).options({
|
|
67
|
+
}).end(), mixedImport && 'named' === exportType && svgRule.use(CHAIN_ID.USE.URL).loader(__WEBPACK_EXTERNAL_MODULE_node_path__.default.join(src_dirname, '../compiled', 'url-loader/index.js')).options({
|
|
125
68
|
limit: maxSize,
|
|
126
69
|
name: null == generatorOptions ? void 0 : generatorOptions.filename
|
|
127
70
|
});
|
|
128
71
|
}
|
|
129
|
-
// SVG as assets
|
|
130
72
|
rule.oneOf(CHAIN_ID.ONE_OF.SVG_ASSET).type('asset').parser({
|
|
131
|
-
// Inline SVG if size < maxSize
|
|
132
73
|
dataUrlCondition: {
|
|
133
74
|
maxSize
|
|
134
75
|
}
|
|
135
76
|
}).set('generator', generatorOptions);
|
|
136
|
-
|
|
137
|
-
const jsRule = chain.module.rules.get(CHAIN_ID.RULE.JS);
|
|
77
|
+
let jsRule = chain.module.rules.get(CHAIN_ID.RULE.JS);
|
|
138
78
|
[
|
|
139
79
|
CHAIN_ID.USE.SWC,
|
|
140
80
|
CHAIN_ID.USE.BABEL
|
|
141
81
|
].some((jsUseId)=>{
|
|
142
|
-
|
|
143
|
-
if (!use) return
|
|
144
|
-
for (
|
|
82
|
+
let use = jsRule.uses.get(jsUseId);
|
|
83
|
+
if (!use) return !1;
|
|
84
|
+
for (let oneOfId of [
|
|
145
85
|
CHAIN_ID.ONE_OF.SVG,
|
|
146
86
|
CHAIN_ID.ONE_OF.SVG_REACT
|
|
147
87
|
]){
|
|
148
88
|
if (!!rule.oneOfs.has(oneOfId)) rule.oneOf(oneOfId).use(jsUseId).before(CHAIN_ID.USE.SVGR).loader(use.get('loader')).options(use.get('options'));
|
|
149
89
|
}
|
|
150
|
-
return
|
|
90
|
+
return !0;
|
|
151
91
|
});
|
|
152
92
|
});
|
|
153
93
|
}
|
package/dist/loader.mjs
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import * as __WEBPACK_EXTERNAL_MODULE_node_path__ from "node:path";
|
|
2
|
+
import * as __WEBPACK_EXTERNAL_MODULE_node_util__ from "node:util";
|
|
3
|
+
import * as __WEBPACK_EXTERNAL_MODULE__svgr_core__ from "@svgr/core";
|
|
4
|
+
import * as __WEBPACK_EXTERNAL_MODULE__svgr_plugin_jsx__ from "@svgr/plugin-jsx";
|
|
5
|
+
import * as __WEBPACK_EXTERNAL_MODULE__svgr_plugin_svgo__ from "@svgr/plugin-svgo";
|
|
6
|
+
/**
|
|
7
|
+
* Copyright (c) 2017, Smooth Code
|
|
8
|
+
*
|
|
9
|
+
* This source code is licensed under the MIT license found in the
|
|
10
|
+
* LICENSE file in the root directory of this source tree.
|
|
11
|
+
* modified from https://github.com/gregberge/svgr/blob/7595d378b73d4826a4cead165b3f32386b07315b/packages/webpack/src/index.ts
|
|
12
|
+
*/ const transformSvg = (0, __WEBPACK_EXTERNAL_MODULE_node_util__.callbackify)(async (contents, config, state)=>(0, __WEBPACK_EXTERNAL_MODULE__svgr_core__.transform)(contents, config, state));
|
|
13
|
+
function svgrLoader(contents) {
|
|
14
|
+
this?.cacheable();
|
|
15
|
+
const callback = this.async();
|
|
16
|
+
const options = this.getOptions();
|
|
17
|
+
const previousExport = (()=>{
|
|
18
|
+
if (contents.startsWith('export ')) return contents;
|
|
19
|
+
const exportMatches = contents.match(/^module.exports\s*=\s*(.*)/);
|
|
20
|
+
return exportMatches ? `export default ${exportMatches[1]}` : null;
|
|
21
|
+
})();
|
|
22
|
+
const state = {
|
|
23
|
+
caller: {
|
|
24
|
+
name: '@rsbuild/plugin-svgr',
|
|
25
|
+
previousExport,
|
|
26
|
+
defaultPlugins: [
|
|
27
|
+
__WEBPACK_EXTERNAL_MODULE__svgr_plugin_svgo__["default"],
|
|
28
|
+
__WEBPACK_EXTERNAL_MODULE__svgr_plugin_jsx__["default"]
|
|
29
|
+
]
|
|
30
|
+
},
|
|
31
|
+
filePath: (0, __WEBPACK_EXTERNAL_MODULE_node_path__.normalize)(this.resourcePath)
|
|
32
|
+
};
|
|
33
|
+
if (previousExport) this.fs.readFile(this.resourcePath, (err, result)=>{
|
|
34
|
+
if (err) {
|
|
35
|
+
callback(err);
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
transformSvg(String(result), options, state, (err, content)=>{
|
|
39
|
+
if (err) {
|
|
40
|
+
callback(err);
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
callback(null, content);
|
|
44
|
+
});
|
|
45
|
+
});
|
|
46
|
+
else transformSvg(contents, options, state, callback);
|
|
47
|
+
}
|
|
48
|
+
/* ESM default export */ const loader_rslib_entry_ = svgrLoader;
|
|
49
|
+
export { loader_rslib_entry_ as default };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rsbuild/plugin-svgr",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.6",
|
|
4
4
|
"description": "svgr plugin for Rsbuild",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -28,16 +28,17 @@
|
|
|
28
28
|
"@svgr/plugin-svgo": "8.1.0",
|
|
29
29
|
"deepmerge": "^4.3.1",
|
|
30
30
|
"loader-utils": "^2.0.4",
|
|
31
|
-
"@rsbuild/plugin-react": "1.0
|
|
31
|
+
"@rsbuild/plugin-react": "1.1.0"
|
|
32
32
|
},
|
|
33
33
|
"devDependencies": {
|
|
34
|
-
"@
|
|
34
|
+
"@rslib/core": "0.1.3",
|
|
35
|
+
"@types/node": "^22.10.1",
|
|
35
36
|
"file-loader": "6.2.0",
|
|
36
|
-
"prebundle": "1.2.
|
|
37
|
+
"prebundle": "1.2.5",
|
|
37
38
|
"svgo": "^3.3.2",
|
|
38
|
-
"typescript": "^5.
|
|
39
|
+
"typescript": "^5.7.2",
|
|
39
40
|
"url-loader": "4.1.1",
|
|
40
|
-
"@rsbuild/core": "1.
|
|
41
|
+
"@rsbuild/core": "1.1.9",
|
|
41
42
|
"@scripts/test-helper": "1.0.1"
|
|
42
43
|
},
|
|
43
44
|
"peerDependencies": {
|
package/dist/loader.cjs
DELETED
|
@@ -1,108 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
// The require scope
|
|
3
|
-
var __webpack_require__ = {};
|
|
4
|
-
/************************************************************************/ // webpack/runtime/compat_get_default_export
|
|
5
|
-
(()=>{
|
|
6
|
-
// getDefaultExport function for compatibility with non-ESM 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
|
-
default: ()=>/* binding */ loader
|
|
52
|
-
});
|
|
53
|
-
const external_node_path_namespaceObject = require("node:path");
|
|
54
|
-
const external_node_util_namespaceObject = require("node:util");
|
|
55
|
-
const core_namespaceObject = require("@svgr/core");
|
|
56
|
-
const plugin_jsx_namespaceObject = require("@svgr/plugin-jsx");
|
|
57
|
-
var plugin_jsx_default = /*#__PURE__*/ __webpack_require__.n(plugin_jsx_namespaceObject);
|
|
58
|
-
const plugin_svgo_namespaceObject = require("@svgr/plugin-svgo");
|
|
59
|
-
var plugin_svgo_default = /*#__PURE__*/ __webpack_require__.n(plugin_svgo_namespaceObject);
|
|
60
|
-
/**
|
|
61
|
-
* Copyright (c) 2017, Smooth Code
|
|
62
|
-
*
|
|
63
|
-
* This source code is licensed under the MIT license found in the
|
|
64
|
-
* LICENSE file in the root directory of this source tree.
|
|
65
|
-
* modified from https://github.com/gregberge/svgr/blob/7595d378b73d4826a4cead165b3f32386b07315b/packages/webpack/src/index.ts
|
|
66
|
-
*/ const transformSvg = (0, external_node_util_namespaceObject.callbackify)(async (contents, config, state)=>(0, core_namespaceObject.transform)(contents, config, state));
|
|
67
|
-
function svgrLoader(contents) {
|
|
68
|
-
var _this;
|
|
69
|
-
null === (_this = this) || void 0 === _this || _this.cacheable();
|
|
70
|
-
const callback = this.async();
|
|
71
|
-
const options = this.getOptions();
|
|
72
|
-
const previousExport = (()=>{
|
|
73
|
-
if (contents.startsWith('export ')) return contents;
|
|
74
|
-
const exportMatches = contents.match(/^module.exports\s*=\s*(.*)/);
|
|
75
|
-
return exportMatches ? `export default ${exportMatches[1]}` : null;
|
|
76
|
-
})();
|
|
77
|
-
const state = {
|
|
78
|
-
caller: {
|
|
79
|
-
name: '@rsbuild/plugin-svgr',
|
|
80
|
-
previousExport,
|
|
81
|
-
defaultPlugins: [
|
|
82
|
-
plugin_svgo_default(),
|
|
83
|
-
plugin_jsx_default()
|
|
84
|
-
]
|
|
85
|
-
},
|
|
86
|
-
filePath: (0, external_node_path_namespaceObject.normalize)(this.resourcePath)
|
|
87
|
-
};
|
|
88
|
-
if (previousExport) this.fs.readFile(this.resourcePath, (err, result)=>{
|
|
89
|
-
if (err) {
|
|
90
|
-
callback(err);
|
|
91
|
-
return;
|
|
92
|
-
}
|
|
93
|
-
transformSvg(String(result), options, state, (err, content)=>{
|
|
94
|
-
if (err) {
|
|
95
|
-
callback(err);
|
|
96
|
-
return;
|
|
97
|
-
}
|
|
98
|
-
callback(null, content);
|
|
99
|
-
});
|
|
100
|
-
});
|
|
101
|
-
else transformSvg(contents, options, state, callback);
|
|
102
|
-
}
|
|
103
|
-
/* ESM default export */ const loader = svgrLoader;
|
|
104
|
-
var __webpack_export_target__ = exports;
|
|
105
|
-
for(var i in __webpack_exports__)__webpack_export_target__[i] = __webpack_exports__[i];
|
|
106
|
-
if (__webpack_exports__.__esModule) Object.defineProperty(__webpack_export_target__, '__esModule', {
|
|
107
|
-
value: true
|
|
108
|
-
});
|