@rsbuild/plugin-svgr 0.2.10 → 0.2.12
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.js +12 -1
- package/dist/index.mjs +17 -3
- package/dist/loader.d.ts +14 -0
- package/dist/loader.js +80 -0
- package/package.json +7 -5
package/dist/index.js
CHANGED
|
@@ -54,6 +54,7 @@ function getSvgoDefaultConfig() {
|
|
|
54
54
|
}
|
|
55
55
|
var pluginSvgr = (options = {}) => ({
|
|
56
56
|
name: "rsbuild:svgr",
|
|
57
|
+
pre: ["rsbuild:babel", "uni-builder:babel", "rsbuild-webpack:swc"],
|
|
57
58
|
setup(api) {
|
|
58
59
|
api.modifyBundlerChain(async (chain, { isProd, CHAIN_ID }) => {
|
|
59
60
|
const config = api.getNormalizedConfig();
|
|
@@ -79,7 +80,17 @@ var pluginSvgr = (options = {}) => ({
|
|
|
79
80
|
rule.oneOf(CHAIN_ID.ONE_OF.SVG_URL).type("asset/resource").resourceQuery(/url/).set("generator", {
|
|
80
81
|
filename: outputName
|
|
81
82
|
});
|
|
82
|
-
|
|
83
|
+
const jsRule = chain.module.rules.get(CHAIN_ID.RULE.JS);
|
|
84
|
+
const svgrRule = rule.oneOf(CHAIN_ID.ONE_OF.SVG).type("javascript/auto");
|
|
85
|
+
[CHAIN_ID.USE.SWC, CHAIN_ID.USE.BABEL].some((id) => {
|
|
86
|
+
const use = jsRule.uses.get(id);
|
|
87
|
+
if (use) {
|
|
88
|
+
svgrRule.use(id).loader(use.get("loader")).options(use.get("options"));
|
|
89
|
+
return true;
|
|
90
|
+
}
|
|
91
|
+
return false;
|
|
92
|
+
});
|
|
93
|
+
svgrRule.use(CHAIN_ID.USE.SVGR).loader(import_path.default.resolve(__dirname, "./loader")).options({
|
|
83
94
|
svgo: true,
|
|
84
95
|
svgoConfig: getSvgoDefaultConfig()
|
|
85
96
|
}).end().when(
|
package/dist/index.mjs
CHANGED
|
@@ -9,6 +9,9 @@ var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require
|
|
|
9
9
|
// ../../node_modules/.pnpm/@modern-js+module-tools@2.43.0_typescript@5.3.2/node_modules/@modern-js/module-tools/shims/esm.js
|
|
10
10
|
import { fileURLToPath } from "url";
|
|
11
11
|
import path from "path";
|
|
12
|
+
var getFilename = () => fileURLToPath(import.meta.url);
|
|
13
|
+
var getDirname = () => path.dirname(getFilename());
|
|
14
|
+
var __dirname = /* @__PURE__ */ getDirname();
|
|
12
15
|
|
|
13
16
|
// ../../scripts/requireShims.js
|
|
14
17
|
import { createRequire } from "module";
|
|
@@ -21,7 +24,7 @@ import {
|
|
|
21
24
|
TS_REGEX,
|
|
22
25
|
SVG_REGEX,
|
|
23
26
|
getDistPath,
|
|
24
|
-
getFilename,
|
|
27
|
+
getFilename as getFilename2,
|
|
25
28
|
chainStaticAssetRule
|
|
26
29
|
} from "@rsbuild/shared";
|
|
27
30
|
function getSvgoDefaultConfig() {
|
|
@@ -43,13 +46,14 @@ function getSvgoDefaultConfig() {
|
|
|
43
46
|
}
|
|
44
47
|
var pluginSvgr = (options = {}) => ({
|
|
45
48
|
name: "rsbuild:svgr",
|
|
49
|
+
pre: ["rsbuild:babel", "uni-builder:babel", "rsbuild-webpack:swc"],
|
|
46
50
|
setup(api) {
|
|
47
51
|
api.modifyBundlerChain(async (chain, { isProd, CHAIN_ID }) => {
|
|
48
52
|
const config = api.getNormalizedConfig();
|
|
49
53
|
const { svgDefaultExport = "url" } = options;
|
|
50
54
|
const assetType = "svg";
|
|
51
55
|
const distDir = getDistPath(config, assetType);
|
|
52
|
-
const filename =
|
|
56
|
+
const filename = getFilename2(config, assetType, isProd);
|
|
53
57
|
const outputName = path2.posix.join(distDir, filename);
|
|
54
58
|
const maxSize = config.output.dataUriLimit[assetType];
|
|
55
59
|
chain.module.rules.delete(CHAIN_ID.RULE.SVG);
|
|
@@ -68,7 +72,17 @@ var pluginSvgr = (options = {}) => ({
|
|
|
68
72
|
rule.oneOf(CHAIN_ID.ONE_OF.SVG_URL).type("asset/resource").resourceQuery(/url/).set("generator", {
|
|
69
73
|
filename: outputName
|
|
70
74
|
});
|
|
71
|
-
|
|
75
|
+
const jsRule = chain.module.rules.get(CHAIN_ID.RULE.JS);
|
|
76
|
+
const svgrRule = rule.oneOf(CHAIN_ID.ONE_OF.SVG).type("javascript/auto");
|
|
77
|
+
[CHAIN_ID.USE.SWC, CHAIN_ID.USE.BABEL].some((id) => {
|
|
78
|
+
const use = jsRule.uses.get(id);
|
|
79
|
+
if (use) {
|
|
80
|
+
svgrRule.use(id).loader(use.get("loader")).options(use.get("options"));
|
|
81
|
+
return true;
|
|
82
|
+
}
|
|
83
|
+
return false;
|
|
84
|
+
});
|
|
85
|
+
svgrRule.use(CHAIN_ID.USE.SVGR).loader(path2.resolve(__dirname, "./loader")).options({
|
|
72
86
|
svgo: true,
|
|
73
87
|
svgoConfig: getSvgoDefaultConfig()
|
|
74
88
|
}).end().when(
|
package/dist/loader.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { Config } from '@svgr/core';
|
|
2
|
+
import { Rspack } from '@rsbuild/core';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Copyright (c) 2017, Smooth Code
|
|
6
|
+
*
|
|
7
|
+
* This source code is licensed under the MIT license found in the
|
|
8
|
+
* LICENSE file in the root directory of this source tree.
|
|
9
|
+
* modified from https://github.com/gregberge/svgr/blob/7595d378b73d4826a4cead165b3f32386b07315b/packages/webpack/src/index.ts
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
declare function svgrLoader(this: Rspack.LoaderContext<Config>, contents: string): void;
|
|
13
|
+
|
|
14
|
+
export { svgrLoader as default };
|
package/dist/loader.js
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_util = require("util");
|
|
37
|
+
var import_core = require("@svgr/core");
|
|
38
|
+
var import_path = require("path");
|
|
39
|
+
var import_plugin_svgo = __toESM(require("@svgr/plugin-svgo"));
|
|
40
|
+
var import_plugin_jsx = __toESM(require("@svgr/plugin-jsx"));
|
|
41
|
+
var transformSvg = (0, import_util.callbackify)(
|
|
42
|
+
async (contents, config, state) => (0, import_core.transform)(contents, config, state)
|
|
43
|
+
);
|
|
44
|
+
function svgrLoader(contents) {
|
|
45
|
+
this.cacheable && 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_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/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rsbuild/plugin-svgr",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.12",
|
|
4
4
|
"description": "svgr plugin for Rsbuild",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -22,15 +22,17 @@
|
|
|
22
22
|
"dist"
|
|
23
23
|
],
|
|
24
24
|
"dependencies": {
|
|
25
|
-
"@svgr/
|
|
25
|
+
"@svgr/core": "8.1.0",
|
|
26
|
+
"@svgr/plugin-jsx": "8.1.0",
|
|
27
|
+
"@svgr/plugin-svgo": "8.1.0",
|
|
26
28
|
"url-loader": "4.1.1",
|
|
27
|
-
"@rsbuild/shared": "0.2.
|
|
29
|
+
"@rsbuild/shared": "0.2.12"
|
|
28
30
|
},
|
|
29
31
|
"devDependencies": {
|
|
30
32
|
"@types/node": "16.x",
|
|
31
33
|
"typescript": "^5.3.0",
|
|
32
|
-
"@rsbuild/
|
|
33
|
-
"@rsbuild/
|
|
34
|
+
"@rsbuild/test-helper": "0.2.12",
|
|
35
|
+
"@rsbuild/core": "0.2.12"
|
|
34
36
|
},
|
|
35
37
|
"publishConfig": {
|
|
36
38
|
"access": "public",
|