vite-plugin-svgr 4.0.0 → 4.2.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/README.md CHANGED
@@ -4,6 +4,19 @@
4
4
 
5
5
  Vite plugin to transform SVGs into React components. Uses [svgr](https://github.com/gregberge/svgr) under the hood.
6
6
 
7
+ ## Installation
8
+
9
+ ```sh
10
+ # npm
11
+ npm install --save-dev vite-plugin-svgr
12
+
13
+ # yarn
14
+ yarn add -D vite-plugin-svgr
15
+
16
+ # pnpm
17
+ pnpm add -D vite-plugin-svgr
18
+ ```
19
+
7
20
  ## Usage
8
21
 
9
22
  ```js
@@ -42,7 +55,7 @@ svgr({
42
55
  // ...
43
56
  },
44
57
 
45
- // A minimatch pattern, or array of patterns, which specifies the files in the build the plugin should include. By default all svg files will be included.
58
+ // A minimatch pattern, or array of patterns, which specifies the files in the build the plugin should include.
46
59
  include: "**/*.svg?react",
47
60
 
48
61
  // A minimatch pattern, or array of patterns, which specifies the files in the build the plugin should ignore. By default no files are ignored.
@@ -50,6 +63,20 @@ svgr({
50
63
  });
51
64
  ```
52
65
 
66
+ If you want to enable SVGO you can install `@svgr/plugin-svgo` and use following options to enable and configure it:
67
+
68
+ ```js
69
+ svgr({
70
+ svgrOptions: {
71
+ plugins: ["@svgr/plugin-svgo", "@svgr/plugin-jsx"],
72
+ svgoConfig: {
73
+ floatPrecision: 2,
74
+ },
75
+ },
76
+ // ...
77
+ });
78
+ ```
79
+
53
80
  ## License
54
81
 
55
82
  MIT
package/dist/index.cjs CHANGED
@@ -34,7 +34,8 @@ function vitePluginSvgr({ svgrOptions, esbuildOptions, include = "**/*.svg?react
34
34
  const postfixRE = /[?#].*$/s;
35
35
  return {
36
36
  name: "vite-plugin-svgr",
37
- async transform(code, id) {
37
+ enforce: "pre",
38
+ async load(id) {
38
39
  if (filter(id)) {
39
40
  const { transform } = await Promise.resolve().then(() => __importStar(require("@svgr/core")));
40
41
  const { default: jsx } = await Promise.resolve().then(() => __importStar(require("@svgr/plugin-jsx")));
package/dist/index.js CHANGED
@@ -6,7 +6,8 @@ export default function vitePluginSvgr({ svgrOptions, esbuildOptions, include =
6
6
  const postfixRE = /[?#].*$/s;
7
7
  return {
8
8
  name: "vite-plugin-svgr",
9
- async transform(code, id) {
9
+ enforce: "pre",
10
+ async load(id) {
10
11
  if (filter(id)) {
11
12
  const { transform } = await import("@svgr/core");
12
13
  const { default: jsx } = await import("@svgr/plugin-jsx");
package/dist/index.mjs ADDED
@@ -0,0 +1,38 @@
1
+ import fs from "fs";
2
+ import { transformWithEsbuild } from "vite";
3
+ import { createFilter } from "@rollup/pluginutils";
4
+ function viteSvgr({
5
+ exportAsDefault,
6
+ svgrOptions,
7
+ esbuildOptions,
8
+ include = "**/*.svg",
9
+ exclude
10
+ } = {}) {
11
+ const filter = createFilter(include, exclude);
12
+ return {
13
+ name: "vite-plugin-svgr",
14
+ async transform(code, id) {
15
+ if (filter(id)) {
16
+ const { transform } = await import("@svgr/core");
17
+ const svgCode = await fs.promises.readFile(id, "utf8");
18
+ const componentCode = await transform(svgCode, svgrOptions, {
19
+ filePath: id,
20
+ caller: {
21
+ previousExport: exportAsDefault ? null : code
22
+ }
23
+ });
24
+ const res = await transformWithEsbuild(componentCode, id, {
25
+ loader: "jsx",
26
+ ...esbuildOptions
27
+ });
28
+ return {
29
+ code: res.code,
30
+ map: null
31
+ };
32
+ }
33
+ }
34
+ };
35
+ }
36
+ export {
37
+ viteSvgr as default
38
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vite-plugin-svgr",
3
- "version": "4.0.0",
3
+ "version": "4.2.0",
4
4
  "description": "Vite plugin to transform SVGs into React components",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -32,14 +32,14 @@
32
32
  "author": "Rongjian Zhang",
33
33
  "license": "MIT",
34
34
  "devDependencies": {
35
- "@types/node": "^20.6.2",
35
+ "@types/node": "^20.9.1",
36
36
  "typescript": "^5.2.2"
37
37
  },
38
38
  "peerDependencies": {
39
- "vite": "^2.6.0 || 3 || 4"
39
+ "vite": "^2.6.0 || 3 || 4 || 5"
40
40
  },
41
41
  "dependencies": {
42
- "@rollup/pluginutils": "^5.0.4",
42
+ "@rollup/pluginutils": "^5.0.5",
43
43
  "@svgr/core": "^8.1.0",
44
44
  "@svgr/plugin-jsx": "^8.1.0"
45
45
  },