@rsbuild/plugin-svgr 1.0.0 → 1.0.1-beta.1
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/README.md +1 -1
- package/compiled/file-loader/index.d.ts +1 -0
- package/compiled/file-loader/index.js +147 -0
- package/compiled/file-loader/license +20 -0
- package/compiled/file-loader/package.json +1 -0
- package/compiled/file-loader/schema-utils.js +1 -0
- package/compiled/url-loader/index.d.ts +1 -0
- package/compiled/url-loader/index.js +298 -0
- package/compiled/url-loader/license +20 -0
- package/compiled/url-loader/package.json +1 -0
- package/compiled/url-loader/schema-utils.js +1 -0
- package/dist/index.cjs +139 -0
- package/dist/index.d.ts +27 -9
- package/dist/index.js +87 -71
- package/dist/loader.cjs +80 -0
- package/dist/loader.d.ts +11 -0
- package/package.json +25 -13
- package/dist/index.mjs +0 -86
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
Copyright JS Foundation and other contributors
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
|
4
|
+
a copy of this software and associated documentation files (the
|
|
5
|
+
'Software'), to deal in the Software without restriction, including
|
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
|
9
|
+
the following conditions:
|
|
10
|
+
|
|
11
|
+
The above copyright notice and this permission notice shall be
|
|
12
|
+
included in all copies or substantial portions of the Software.
|
|
13
|
+
|
|
14
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
17
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
|
18
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
|
19
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
|
20
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"name":"url-loader","author":"Tobias Koppers @sokra","version":"4.1.1","funding":{"type":"opencollective","url":"https://opencollective.com/webpack"},"license":"MIT","types":"index.d.ts","type":"commonjs"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
module.exports.validate = () => {};
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,139 @@
|
|
|
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
|
|
35
|
+
});
|
|
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
|
+
function getSvgoDefaultConfig() {
|
|
42
|
+
return {
|
|
43
|
+
plugins: [
|
|
44
|
+
{
|
|
45
|
+
name: "preset-default",
|
|
46
|
+
params: {
|
|
47
|
+
overrides: {
|
|
48
|
+
// viewBox is required to resize SVGs with CSS.
|
|
49
|
+
// @see https://github.com/svg/svgo/issues/1128
|
|
50
|
+
removeViewBox: false
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
},
|
|
54
|
+
"prefixIds"
|
|
55
|
+
]
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
var PLUGIN_SVGR_NAME = "rsbuild:svgr";
|
|
59
|
+
var pluginSvgr = (options = {}) => ({
|
|
60
|
+
name: PLUGIN_SVGR_NAME,
|
|
61
|
+
pre: [import_plugin_react.PLUGIN_REACT_NAME],
|
|
62
|
+
setup(api) {
|
|
63
|
+
api.modifyBundlerChain(async (chain, { CHAIN_ID, environment }) => {
|
|
64
|
+
const { config } = environment;
|
|
65
|
+
const { dataUriLimit } = config.output;
|
|
66
|
+
const maxSize = typeof dataUriLimit === "number" ? dataUriLimit : dataUriLimit.svg;
|
|
67
|
+
let generatorOptions = {};
|
|
68
|
+
if (chain.module.rules.has(CHAIN_ID.RULE.SVG)) {
|
|
69
|
+
generatorOptions = chain.module.rules.get(CHAIN_ID.RULE.SVG).oneOfs.get(CHAIN_ID.ONE_OF.SVG_URL).get("generator");
|
|
70
|
+
chain.module.rules.delete(CHAIN_ID.RULE.SVG);
|
|
71
|
+
}
|
|
72
|
+
const rule = chain.module.rule(CHAIN_ID.RULE.SVG).test(SVG_REGEX);
|
|
73
|
+
const svgrOptions = (0, import_deepmerge.default)(
|
|
74
|
+
{
|
|
75
|
+
svgo: true,
|
|
76
|
+
svgoConfig: getSvgoDefaultConfig()
|
|
77
|
+
},
|
|
78
|
+
options.svgrOptions || {}
|
|
79
|
+
);
|
|
80
|
+
rule.oneOf(CHAIN_ID.ONE_OF.SVG_URL).type("asset/resource").resourceQuery(/(__inline=false|url)/).set("generator", generatorOptions);
|
|
81
|
+
rule.oneOf(CHAIN_ID.ONE_OF.SVG_INLINE).type("asset/inline").resourceQuery(/inline/);
|
|
82
|
+
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({
|
|
83
|
+
...svgrOptions,
|
|
84
|
+
exportType: "default"
|
|
85
|
+
}).end();
|
|
86
|
+
const { mixedImport = false } = options;
|
|
87
|
+
if (mixedImport || svgrOptions.exportType) {
|
|
88
|
+
const { exportType = mixedImport ? "named" : void 0 } = svgrOptions;
|
|
89
|
+
const issuerInclude = [
|
|
90
|
+
/\.(?:js|jsx|mjs|cjs|ts|tsx|mts|cts)$/,
|
|
91
|
+
/\.mdx$/
|
|
92
|
+
];
|
|
93
|
+
const issuer = options.excludeImporter ? { and: [issuerInclude, { not: options.excludeImporter }] } : issuerInclude;
|
|
94
|
+
const svgRule = rule.oneOf(CHAIN_ID.ONE_OF.SVG);
|
|
95
|
+
if (options.exclude) {
|
|
96
|
+
svgRule.exclude.add(options.exclude);
|
|
97
|
+
}
|
|
98
|
+
svgRule.type("javascript/auto").set("issuer", issuer).use(CHAIN_ID.USE.SVGR).loader(import_node_path.default.resolve(__dirname, "./loader.cjs")).options({
|
|
99
|
+
...svgrOptions,
|
|
100
|
+
exportType
|
|
101
|
+
}).end();
|
|
102
|
+
if (mixedImport && exportType === "named") {
|
|
103
|
+
svgRule.use(CHAIN_ID.USE.URL).loader(import_node_path.default.join(__dirname, "../compiled", "url-loader/index.js")).options({
|
|
104
|
+
limit: maxSize,
|
|
105
|
+
name: generatorOptions?.filename
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
rule.oneOf(CHAIN_ID.ONE_OF.SVG_ASSET).type("asset").parser({
|
|
110
|
+
// Inline SVG if size < maxSize
|
|
111
|
+
dataUrlCondition: {
|
|
112
|
+
maxSize
|
|
113
|
+
}
|
|
114
|
+
}).set("generator", generatorOptions);
|
|
115
|
+
const jsRule = chain.module.rules.get(CHAIN_ID.RULE.JS);
|
|
116
|
+
[CHAIN_ID.USE.SWC, CHAIN_ID.USE.BABEL].some((jsUseId) => {
|
|
117
|
+
const use = jsRule.uses.get(jsUseId);
|
|
118
|
+
if (!use) {
|
|
119
|
+
return false;
|
|
120
|
+
}
|
|
121
|
+
for (const oneOfId of [
|
|
122
|
+
CHAIN_ID.ONE_OF.SVG,
|
|
123
|
+
CHAIN_ID.ONE_OF.SVG_REACT
|
|
124
|
+
]) {
|
|
125
|
+
if (!rule.oneOfs.has(oneOfId)) {
|
|
126
|
+
continue;
|
|
127
|
+
}
|
|
128
|
+
rule.oneOf(oneOfId).use(jsUseId).before(CHAIN_ID.USE.SVGR).loader(use.get("loader")).options(use.get("options"));
|
|
129
|
+
}
|
|
130
|
+
return true;
|
|
131
|
+
});
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
});
|
|
135
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
136
|
+
0 && (module.exports = {
|
|
137
|
+
PLUGIN_SVGR_NAME,
|
|
138
|
+
pluginSvgr
|
|
139
|
+
});
|
package/dist/index.d.ts
CHANGED
|
@@ -1,12 +1,30 @@
|
|
|
1
|
-
import { RsbuildPlugin } from '@rsbuild/core';
|
|
2
|
-
|
|
3
|
-
type SvgDefaultExport = 'component' | 'url';
|
|
4
|
-
type PluginSvgrOptions = {
|
|
1
|
+
import type { RsbuildPlugin, Rspack } from '@rsbuild/core';
|
|
2
|
+
import type { Config } from '@svgr/core';
|
|
3
|
+
export type SvgDefaultExport = 'component' | 'url';
|
|
4
|
+
export type PluginSvgrOptions = {
|
|
5
5
|
/**
|
|
6
|
-
* Configure
|
|
6
|
+
* Configure SVGR options.
|
|
7
|
+
* @see https://react-svgr.com/docs/options/
|
|
7
8
|
*/
|
|
8
|
-
|
|
9
|
+
svgrOptions?: Config;
|
|
10
|
+
/**
|
|
11
|
+
* Whether to allow the use of default import and named import at the same time.
|
|
12
|
+
* @default false
|
|
13
|
+
*/
|
|
14
|
+
mixedImport?: boolean;
|
|
15
|
+
/**
|
|
16
|
+
* Custom query suffix to match SVGR transformation.
|
|
17
|
+
* @default /react/
|
|
18
|
+
*/
|
|
19
|
+
query?: RegExp;
|
|
20
|
+
/**
|
|
21
|
+
* Exclude some SVG files, they will not be transformed by SVGR.
|
|
22
|
+
*/
|
|
23
|
+
exclude?: Rspack.RuleSetCondition;
|
|
24
|
+
/**
|
|
25
|
+
* Exclude some modules, the SVGs imported by these modules will not be transformed by SVGR.
|
|
26
|
+
*/
|
|
27
|
+
excludeImporter?: Rspack.RuleSetCondition;
|
|
9
28
|
};
|
|
10
|
-
declare const
|
|
11
|
-
|
|
12
|
-
export { PluginSvgrOptions, SvgDefaultExport, pluginSvgr };
|
|
29
|
+
export declare const PLUGIN_SVGR_NAME = "rsbuild:svgr";
|
|
30
|
+
export declare const pluginSvgr: (options?: PluginSvgrOptions) => RsbuildPlugin;
|
package/dist/index.js
CHANGED
|
@@ -1,40 +1,19 @@
|
|
|
1
|
-
|
|
2
|
-
var
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
var
|
|
9
|
-
|
|
10
|
-
|
|
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);
|
|
1
|
+
import { createRequire } from 'module';
|
|
2
|
+
var require = createRequire(import.meta['url']);
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
// ../../node_modules/.pnpm/@modern-js+module-tools@2.55.0_eslint@9.6.0_typescript@5.5.2/node_modules/@modern-js/module-tools/shims/esm.js
|
|
6
|
+
import { fileURLToPath } from "url";
|
|
7
|
+
import path from "path";
|
|
8
|
+
var getFilename = () => fileURLToPath(import.meta.url);
|
|
9
|
+
var getDirname = () => path.dirname(getFilename());
|
|
10
|
+
var __dirname = /* @__PURE__ */ getDirname();
|
|
29
11
|
|
|
30
12
|
// src/index.ts
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
module.exports = __toCommonJS(src_exports);
|
|
36
|
-
var import_path = __toESM(require("path"));
|
|
37
|
-
var import_shared = require("@rsbuild/shared");
|
|
13
|
+
import path2 from "path";
|
|
14
|
+
import { PLUGIN_REACT_NAME } from "@rsbuild/plugin-react";
|
|
15
|
+
import deepmerge from "deepmerge";
|
|
16
|
+
var SVG_REGEX = /\.svg$/;
|
|
38
17
|
function getSvgoDefaultConfig() {
|
|
39
18
|
return {
|
|
40
19
|
plugins: [
|
|
@@ -52,47 +31,84 @@ function getSvgoDefaultConfig() {
|
|
|
52
31
|
]
|
|
53
32
|
};
|
|
54
33
|
}
|
|
34
|
+
var PLUGIN_SVGR_NAME = "rsbuild:svgr";
|
|
55
35
|
var pluginSvgr = (options = {}) => ({
|
|
56
|
-
name:
|
|
36
|
+
name: PLUGIN_SVGR_NAME,
|
|
37
|
+
pre: [PLUGIN_REACT_NAME],
|
|
57
38
|
setup(api) {
|
|
58
|
-
api.modifyBundlerChain(async (chain, {
|
|
59
|
-
const config =
|
|
60
|
-
const {
|
|
61
|
-
const
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
const rule = chain.module.rule(CHAIN_ID.RULE.SVG).test(
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
}
|
|
77
|
-
});
|
|
39
|
+
api.modifyBundlerChain(async (chain, { CHAIN_ID, environment }) => {
|
|
40
|
+
const { config } = environment;
|
|
41
|
+
const { dataUriLimit } = config.output;
|
|
42
|
+
const maxSize = typeof dataUriLimit === "number" ? dataUriLimit : dataUriLimit.svg;
|
|
43
|
+
let generatorOptions = {};
|
|
44
|
+
if (chain.module.rules.has(CHAIN_ID.RULE.SVG)) {
|
|
45
|
+
generatorOptions = chain.module.rules.get(CHAIN_ID.RULE.SVG).oneOfs.get(CHAIN_ID.ONE_OF.SVG_URL).get("generator");
|
|
46
|
+
chain.module.rules.delete(CHAIN_ID.RULE.SVG);
|
|
47
|
+
}
|
|
48
|
+
const rule = chain.module.rule(CHAIN_ID.RULE.SVG).test(SVG_REGEX);
|
|
49
|
+
const svgrOptions = deepmerge(
|
|
50
|
+
{
|
|
51
|
+
svgo: true,
|
|
52
|
+
svgoConfig: getSvgoDefaultConfig()
|
|
53
|
+
},
|
|
54
|
+
options.svgrOptions || {}
|
|
55
|
+
);
|
|
56
|
+
rule.oneOf(CHAIN_ID.ONE_OF.SVG_URL).type("asset/resource").resourceQuery(/(__inline=false|url)/).set("generator", generatorOptions);
|
|
78
57
|
rule.oneOf(CHAIN_ID.ONE_OF.SVG_INLINE).type("asset/inline").resourceQuery(/inline/);
|
|
79
|
-
rule.oneOf(CHAIN_ID.ONE_OF.
|
|
80
|
-
|
|
58
|
+
rule.oneOf(CHAIN_ID.ONE_OF.SVG_REACT).type("javascript/auto").resourceQuery(options.query || /react/).use(CHAIN_ID.USE.SVGR).loader(path2.resolve(__dirname, "./loader.cjs")).options({
|
|
59
|
+
...svgrOptions,
|
|
60
|
+
exportType: "default"
|
|
61
|
+
}).end();
|
|
62
|
+
const { mixedImport = false } = options;
|
|
63
|
+
if (mixedImport || svgrOptions.exportType) {
|
|
64
|
+
const { exportType = mixedImport ? "named" : void 0 } = svgrOptions;
|
|
65
|
+
const issuerInclude = [
|
|
66
|
+
/\.(?:js|jsx|mjs|cjs|ts|tsx|mts|cts)$/,
|
|
67
|
+
/\.mdx$/
|
|
68
|
+
];
|
|
69
|
+
const issuer = options.excludeImporter ? { and: [issuerInclude, { not: options.excludeImporter }] } : issuerInclude;
|
|
70
|
+
const svgRule = rule.oneOf(CHAIN_ID.ONE_OF.SVG);
|
|
71
|
+
if (options.exclude) {
|
|
72
|
+
svgRule.exclude.add(options.exclude);
|
|
73
|
+
}
|
|
74
|
+
svgRule.type("javascript/auto").set("issuer", issuer).use(CHAIN_ID.USE.SVGR).loader(path2.resolve(__dirname, "./loader.cjs")).options({
|
|
75
|
+
...svgrOptions,
|
|
76
|
+
exportType
|
|
77
|
+
}).end();
|
|
78
|
+
if (mixedImport && exportType === "named") {
|
|
79
|
+
svgRule.use(CHAIN_ID.USE.URL).loader(path2.join(__dirname, "../compiled", "url-loader/index.js")).options({
|
|
80
|
+
limit: maxSize,
|
|
81
|
+
name: generatorOptions?.filename
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
rule.oneOf(CHAIN_ID.ONE_OF.SVG_ASSET).type("asset").parser({
|
|
86
|
+
// Inline SVG if size < maxSize
|
|
87
|
+
dataUrlCondition: {
|
|
88
|
+
maxSize
|
|
89
|
+
}
|
|
90
|
+
}).set("generator", generatorOptions);
|
|
91
|
+
const jsRule = chain.module.rules.get(CHAIN_ID.RULE.JS);
|
|
92
|
+
[CHAIN_ID.USE.SWC, CHAIN_ID.USE.BABEL].some((jsUseId) => {
|
|
93
|
+
const use = jsRule.uses.get(jsUseId);
|
|
94
|
+
if (!use) {
|
|
95
|
+
return false;
|
|
96
|
+
}
|
|
97
|
+
for (const oneOfId of [
|
|
98
|
+
CHAIN_ID.ONE_OF.SVG,
|
|
99
|
+
CHAIN_ID.ONE_OF.SVG_REACT
|
|
100
|
+
]) {
|
|
101
|
+
if (!rule.oneOfs.has(oneOfId)) {
|
|
102
|
+
continue;
|
|
103
|
+
}
|
|
104
|
+
rule.oneOf(oneOfId).use(jsUseId).before(CHAIN_ID.USE.SVGR).loader(use.get("loader")).options(use.get("options"));
|
|
105
|
+
}
|
|
106
|
+
return true;
|
|
81
107
|
});
|
|
82
|
-
rule.oneOf(CHAIN_ID.ONE_OF.SVG).type("javascript/auto").use(CHAIN_ID.USE.SVGR).loader(require.resolve("@svgr/webpack")).options({
|
|
83
|
-
svgo: true,
|
|
84
|
-
svgoConfig: getSvgoDefaultConfig()
|
|
85
|
-
}).end().when(
|
|
86
|
-
svgDefaultExport === "url",
|
|
87
|
-
(c) => c.use(CHAIN_ID.USE.URL).loader(require.resolve("url-loader")).options({
|
|
88
|
-
limit: config.output.dataUriLimit.svg,
|
|
89
|
-
name: outputName
|
|
90
|
-
})
|
|
91
|
-
);
|
|
92
108
|
});
|
|
93
109
|
}
|
|
94
110
|
});
|
|
95
|
-
|
|
96
|
-
|
|
111
|
+
export {
|
|
112
|
+
PLUGIN_SVGR_NAME,
|
|
97
113
|
pluginSvgr
|
|
98
|
-
}
|
|
114
|
+
};
|
package/dist/loader.cjs
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
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/loader.ts
|
|
31
|
+
var loader_exports = {};
|
|
32
|
+
__export(loader_exports, {
|
|
33
|
+
default: () => loader_default
|
|
34
|
+
});
|
|
35
|
+
module.exports = __toCommonJS(loader_exports);
|
|
36
|
+
var import_node_path = require("path");
|
|
37
|
+
var import_node_util = require("util");
|
|
38
|
+
var import_core = require("@svgr/core");
|
|
39
|
+
var import_plugin_jsx = __toESM(require("@svgr/plugin-jsx"));
|
|
40
|
+
var import_plugin_svgo = __toESM(require("@svgr/plugin-svgo"));
|
|
41
|
+
var transformSvg = (0, import_node_util.callbackify)(
|
|
42
|
+
async (contents, config, state) => (0, import_core.transform)(contents, config, state)
|
|
43
|
+
);
|
|
44
|
+
function svgrLoader(contents) {
|
|
45
|
+
this?.cacheable();
|
|
46
|
+
const callback = this.async();
|
|
47
|
+
const options = this.getOptions();
|
|
48
|
+
const previousExport = (() => {
|
|
49
|
+
if (contents.startsWith("export "))
|
|
50
|
+
return contents;
|
|
51
|
+
const exportMatches = contents.match(/^module.exports\s*=\s*(.*)/);
|
|
52
|
+
return exportMatches ? `export default ${exportMatches[1]}` : null;
|
|
53
|
+
})();
|
|
54
|
+
const state = {
|
|
55
|
+
caller: {
|
|
56
|
+
name: "@rsbuild/plugin-svgr",
|
|
57
|
+
previousExport,
|
|
58
|
+
defaultPlugins: [import_plugin_svgo.default, import_plugin_jsx.default]
|
|
59
|
+
},
|
|
60
|
+
filePath: (0, import_node_path.normalize)(this.resourcePath)
|
|
61
|
+
};
|
|
62
|
+
if (!previousExport) {
|
|
63
|
+
transformSvg(contents, options, state, callback);
|
|
64
|
+
} else {
|
|
65
|
+
this.fs.readFile(this.resourcePath, (err, result) => {
|
|
66
|
+
if (err) {
|
|
67
|
+
callback(err);
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
transformSvg(String(result), options, state, (err2, content) => {
|
|
71
|
+
if (err2) {
|
|
72
|
+
callback(err2);
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
callback(null, content);
|
|
76
|
+
});
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
var loader_default = svgrLoader;
|
package/dist/loader.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) 2017, Smooth Code
|
|
3
|
+
*
|
|
4
|
+
* This source code is licensed under the MIT license found in the
|
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
|
6
|
+
* modified from https://github.com/gregberge/svgr/blob/7595d378b73d4826a4cead165b3f32386b07315b/packages/webpack/src/index.ts
|
|
7
|
+
*/
|
|
8
|
+
import type { Rspack } from '@rsbuild/core';
|
|
9
|
+
import { type Config } from '@svgr/core';
|
|
10
|
+
declare function svgrLoader(this: Rspack.LoaderContext<Config>, contents: string): void;
|
|
11
|
+
export default svgrLoader;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rsbuild/plugin-svgr",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.1-beta.1",
|
|
4
4
|
"description": "svgr plugin for Rsbuild",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -8,28 +8,39 @@
|
|
|
8
8
|
"directory": "packages/plugin-svgr"
|
|
9
9
|
},
|
|
10
10
|
"license": "MIT",
|
|
11
|
+
"type": "module",
|
|
11
12
|
"exports": {
|
|
12
13
|
".": {
|
|
13
14
|
"types": "./dist/index.d.ts",
|
|
14
|
-
"import": "./dist/index.
|
|
15
|
-
"
|
|
15
|
+
"import": "./dist/index.js",
|
|
16
|
+
"require": "./dist/index.cjs"
|
|
16
17
|
}
|
|
17
18
|
},
|
|
18
|
-
"main": "./dist/index.
|
|
19
|
+
"main": "./dist/index.cjs",
|
|
19
20
|
"types": "./dist/index.d.ts",
|
|
20
21
|
"files": [
|
|
21
|
-
"dist"
|
|
22
|
+
"dist",
|
|
23
|
+
"compiled"
|
|
22
24
|
],
|
|
23
25
|
"dependencies": {
|
|
24
|
-
"@svgr/
|
|
25
|
-
"
|
|
26
|
-
"@
|
|
26
|
+
"@svgr/core": "8.1.0",
|
|
27
|
+
"@svgr/plugin-jsx": "8.1.0",
|
|
28
|
+
"@svgr/plugin-svgo": "8.1.0",
|
|
29
|
+
"deepmerge": "^4.3.1",
|
|
30
|
+
"loader-utils": "^2.0.4",
|
|
31
|
+
"@rsbuild/plugin-react": "1.0.1-beta.1"
|
|
27
32
|
},
|
|
28
33
|
"devDependencies": {
|
|
29
|
-
"@types/node": "
|
|
30
|
-
"
|
|
31
|
-
"
|
|
32
|
-
"
|
|
34
|
+
"@types/node": "18.x",
|
|
35
|
+
"file-loader": "6.2.0",
|
|
36
|
+
"prebundle": "1.2.2",
|
|
37
|
+
"typescript": "^5.5.2",
|
|
38
|
+
"url-loader": "4.1.1",
|
|
39
|
+
"@rsbuild/core": "1.0.1-beta.1",
|
|
40
|
+
"@scripts/test-helper": "1.0.1-beta.1"
|
|
41
|
+
},
|
|
42
|
+
"peerDependencies": {
|
|
43
|
+
"@rsbuild/core": "^1.0.1-beta.0"
|
|
33
44
|
},
|
|
34
45
|
"publishConfig": {
|
|
35
46
|
"access": "public",
|
|
@@ -38,6 +49,7 @@
|
|
|
38
49
|
},
|
|
39
50
|
"scripts": {
|
|
40
51
|
"build": "modern build",
|
|
41
|
-
"dev": "modern build --watch"
|
|
52
|
+
"dev": "modern build --watch",
|
|
53
|
+
"prebundle": "prebundle"
|
|
42
54
|
}
|
|
43
55
|
}
|
package/dist/index.mjs
DELETED
|
@@ -1,86 +0,0 @@
|
|
|
1
|
-
var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
|
|
2
|
-
get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
|
|
3
|
-
}) : x)(function(x) {
|
|
4
|
-
if (typeof require !== "undefined")
|
|
5
|
-
return require.apply(this, arguments);
|
|
6
|
-
throw Error('Dynamic require of "' + x + '" is not supported');
|
|
7
|
-
});
|
|
8
|
-
|
|
9
|
-
// ../../node_modules/.pnpm/@modern-js+module-tools@2.40.0_typescript@5.3.2/node_modules/@modern-js/module-tools/shims/esm.js
|
|
10
|
-
import { fileURLToPath } from "url";
|
|
11
|
-
import path from "path";
|
|
12
|
-
|
|
13
|
-
// ../../scripts/require_shims.js
|
|
14
|
-
import { createRequire } from "module";
|
|
15
|
-
global.require = createRequire(import.meta.url);
|
|
16
|
-
|
|
17
|
-
// src/index.ts
|
|
18
|
-
import path2 from "path";
|
|
19
|
-
import {
|
|
20
|
-
JS_REGEX,
|
|
21
|
-
TS_REGEX,
|
|
22
|
-
SVG_REGEX,
|
|
23
|
-
getDistPath,
|
|
24
|
-
getFilename,
|
|
25
|
-
chainStaticAssetRule
|
|
26
|
-
} from "@rsbuild/shared";
|
|
27
|
-
function getSvgoDefaultConfig() {
|
|
28
|
-
return {
|
|
29
|
-
plugins: [
|
|
30
|
-
{
|
|
31
|
-
name: "preset-default",
|
|
32
|
-
params: {
|
|
33
|
-
overrides: {
|
|
34
|
-
// viewBox is required to resize SVGs with CSS.
|
|
35
|
-
// @see https://github.com/svg/svgo/issues/1128
|
|
36
|
-
removeViewBox: false
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
},
|
|
40
|
-
"prefixIds"
|
|
41
|
-
]
|
|
42
|
-
};
|
|
43
|
-
}
|
|
44
|
-
var pluginSvgr = (options = {}) => ({
|
|
45
|
-
name: "rsbuild:svgr",
|
|
46
|
-
setup(api) {
|
|
47
|
-
api.modifyBundlerChain(async (chain, { isProd, CHAIN_ID }) => {
|
|
48
|
-
const config = api.getNormalizedConfig();
|
|
49
|
-
const { svgDefaultExport = "url" } = options;
|
|
50
|
-
const assetType = "svg";
|
|
51
|
-
const distDir = getDistPath(config.output, assetType);
|
|
52
|
-
const filename = getFilename(config.output, assetType, isProd);
|
|
53
|
-
const outputName = path2.posix.join(distDir, filename);
|
|
54
|
-
const maxSize = config.output.dataUriLimit[assetType];
|
|
55
|
-
chain.module.rules.delete(CHAIN_ID.RULE.SVG);
|
|
56
|
-
const rule = chain.module.rule(CHAIN_ID.RULE.SVG).test(SVG_REGEX);
|
|
57
|
-
chainStaticAssetRule({
|
|
58
|
-
rule,
|
|
59
|
-
maxSize,
|
|
60
|
-
filename: path2.posix.join(distDir, filename),
|
|
61
|
-
assetType,
|
|
62
|
-
issuer: {
|
|
63
|
-
// The issuer option ensures that SVGR will only apply if the SVG is imported from a JS file.
|
|
64
|
-
not: [JS_REGEX, TS_REGEX]
|
|
65
|
-
}
|
|
66
|
-
});
|
|
67
|
-
rule.oneOf(CHAIN_ID.ONE_OF.SVG_INLINE).type("asset/inline").resourceQuery(/inline/);
|
|
68
|
-
rule.oneOf(CHAIN_ID.ONE_OF.SVG_URL).type("asset/resource").resourceQuery(/url/).set("generator", {
|
|
69
|
-
filename: outputName
|
|
70
|
-
});
|
|
71
|
-
rule.oneOf(CHAIN_ID.ONE_OF.SVG).type("javascript/auto").use(CHAIN_ID.USE.SVGR).loader(__require.resolve("@svgr/webpack")).options({
|
|
72
|
-
svgo: true,
|
|
73
|
-
svgoConfig: getSvgoDefaultConfig()
|
|
74
|
-
}).end().when(
|
|
75
|
-
svgDefaultExport === "url",
|
|
76
|
-
(c) => c.use(CHAIN_ID.USE.URL).loader(__require.resolve("url-loader")).options({
|
|
77
|
-
limit: config.output.dataUriLimit.svg,
|
|
78
|
-
name: outputName
|
|
79
|
-
})
|
|
80
|
-
);
|
|
81
|
-
});
|
|
82
|
-
}
|
|
83
|
-
});
|
|
84
|
-
export {
|
|
85
|
-
pluginSvgr
|
|
86
|
-
};
|