vite-plugin-svgr 4.1.0 → 4.3.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
@@ -5,6 +5,7 @@
5
5
  Vite plugin to transform SVGs into React components. Uses [svgr](https://github.com/gregberge/svgr) under the hood.
6
6
 
7
7
  ## Installation
8
+
8
9
  ```sh
9
10
  # npm
10
11
  npm install --save-dev vite-plugin-svgr
@@ -34,7 +35,7 @@ Then SVG files can be imported as React components:
34
35
  import Logo from "./logo.svg?react";
35
36
  ```
36
37
 
37
- If you are using TypeScript, there is also a declaration helper for better type inference:
38
+ If you are using TypeScript, there is also a declaration helper for better type inference. Add the following to `vite-env.d.ts`:
38
39
 
39
40
  ```ts
40
41
  /// <reference types="vite-plugin-svgr/client" />
@@ -54,7 +55,7 @@ svgr({
54
55
  // ...
55
56
  },
56
57
 
57
- // 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.
58
59
  include: "**/*.svg?react",
59
60
 
60
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.
@@ -62,6 +63,20 @@ svgr({
62
63
  });
63
64
  ```
64
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
+
65
80
  ## License
66
81
 
67
82
  MIT
package/client.d.ts CHANGED
@@ -5,7 +5,7 @@ declare module "*.svg?react" {
5
5
  import * as React from "react";
6
6
 
7
7
  const ReactComponent: React.FunctionComponent<
8
- React.ComponentProps<"svg"> & { title?: string }
8
+ React.ComponentProps<"svg"> & { title?: string, titleId?: string, desc?: string, descId?: string }
9
9
  >;
10
10
 
11
11
  export default ReactComponent;
@@ -0,0 +1,8 @@
1
+ {
2
+ "name": "codemods",
3
+ "version": "0.0.0",
4
+ "type": "commonjs",
5
+ "exports": {
6
+ "./package.json": "./package.json"
7
+ }
8
+ }
@@ -0,0 +1,31 @@
1
+ # Codemod
2
+
3
+ ## V4
4
+
5
+ convert named export to default
6
+
7
+ If you want to run it against `.js` or `.jsx` files, please use the command below:
8
+
9
+ ```
10
+ npx jscodeshift@latest ./path/to/src/ \
11
+ --extensions=js,jsx \
12
+ --transform=./node_modules/vite-plugin-svgr/codemods/src/v4/default-export/default-export.js
13
+ ```
14
+
15
+ If you want to run it against `.ts` or `.tsx` files, please use the command below:
16
+
17
+ ```
18
+ npx jscodeshift@latest ./path/to/src/ \
19
+ --extensions=ts,tsx \
20
+ --parser=tsx \
21
+ --transform=./node_modules/vite-plugin-svgr/codemods/src/v4/default-export/default-export.js
22
+ ```
23
+
24
+ **Note:** Applying the codemod might break your code formatting, so please don't forget to run `prettier` and/or `eslint` after you've applied the codemod!
25
+
26
+ Above codemod will convert as imports as bellow
27
+
28
+ ```diff
29
+ - import { ReactComponent as NoticeModeIconActive2 } from 'assets/icon.svg';
30
+ + import NoticeModeIconActive2 from 'assets/icon.svg?react';
31
+ ```
@@ -0,0 +1,33 @@
1
+ module.exports = function (file, api) {
2
+ const jscodeshift = api.jscodeshift;
3
+ const root = jscodeshift(file.source);
4
+
5
+ root.find(jscodeshift.ImportDeclaration).forEach((path) => {
6
+ const importPath = path.node.source.value;
7
+
8
+ if (importPath.endsWith(".svg")) {
9
+ const hasDefaultSpecifier = path.node.specifiers.some((specifier) =>
10
+ jscodeshift.ImportDefaultSpecifier.check(specifier),
11
+ );
12
+
13
+ // Skip transformation if there is a default import specifier
14
+ if (!hasDefaultSpecifier) {
15
+ const updatedImportPath = `${importPath}?react`;
16
+
17
+ path.node.specifiers.forEach((specifier) => {
18
+ if (jscodeshift.ImportSpecifier.check(specifier)) {
19
+ // Convert named import to default import
20
+ const newSpecifier = jscodeshift.importDefaultSpecifier(
21
+ jscodeshift.identifier(specifier.local.name),
22
+ );
23
+ specifier.type = "ImportDefaultSpecifier";
24
+ specifier.local = newSpecifier.local;
25
+ }
26
+ });
27
+
28
+ path.node.source = jscodeshift.literal(updatedImportPath);
29
+ }
30
+ }
31
+ });
32
+ return root.toSource();
33
+ };
package/dist/index.cjs CHANGED
@@ -26,6 +26,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
26
26
  return (mod && mod.__esModule) ? mod : { "default": mod };
27
27
  };
28
28
  Object.defineProperty(exports, "__esModule", { value: true });
29
+ exports.default = vitePluginSvgr;
29
30
  const pluginutils_1 = require("@rollup/pluginutils");
30
31
  const fs_1 = __importDefault(require("fs"));
31
32
  const vite_1 = require("vite");
@@ -34,7 +35,7 @@ function vitePluginSvgr({ svgrOptions, esbuildOptions, include = "**/*.svg?react
34
35
  const postfixRE = /[?#].*$/s;
35
36
  return {
36
37
  name: "vite-plugin-svgr",
37
- enforce: "pre",
38
+ enforce: "pre", // to override `vite:asset`'s behavior
38
39
  async load(id) {
39
40
  if (filter(id)) {
40
41
  const { transform } = await Promise.resolve().then(() => __importStar(require("@svgr/core")));
@@ -59,4 +60,3 @@ function vitePluginSvgr({ svgrOptions, esbuildOptions, include = "**/*.svg?react
59
60
  },
60
61
  };
61
62
  }
62
- exports.default = vitePluginSvgr;
package/dist/index.js CHANGED
@@ -6,7 +6,7 @@ export default function vitePluginSvgr({ svgrOptions, esbuildOptions, include =
6
6
  const postfixRE = /[?#].*$/s;
7
7
  return {
8
8
  name: "vite-plugin-svgr",
9
- enforce: "pre",
9
+ enforce: "pre", // to override `vite:asset`'s behavior
10
10
  async load(id) {
11
11
  if (filter(id)) {
12
12
  const { transform } = await import("@svgr/core");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vite-plugin-svgr",
3
- "version": "4.1.0",
3
+ "version": "4.3.0",
4
4
  "description": "Vite plugin to transform SVGs into React components",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -23,7 +23,8 @@
23
23
  "repository": "pd4d10/vite-plugin-svgr",
24
24
  "files": [
25
25
  "dist",
26
- "client.d.ts"
26
+ "client.d.ts",
27
+ "codemods"
27
28
  ],
28
29
  "keywords": [
29
30
  "vite",
@@ -32,16 +33,16 @@
32
33
  "author": "Rongjian Zhang",
33
34
  "license": "MIT",
34
35
  "devDependencies": {
35
- "@types/node": "^20.7.2",
36
- "typescript": "^5.2.2"
36
+ "@types/node": "^22.8.6",
37
+ "typescript": "^5.6.3"
37
38
  },
38
39
  "peerDependencies": {
39
- "vite": "^2.6.0 || 3 || 4"
40
+ "vite": ">=2.6.0"
40
41
  },
41
42
  "dependencies": {
42
- "@rollup/pluginutils": "^5.0.4",
43
+ "@rollup/pluginutils": "^5.1.3",
43
44
  "@svgr/core": "^8.1.0",
44
45
  "@svgr/plugin-jsx": "^8.1.0"
45
46
  },
46
- "packageManager": "pnpm@8.7.6"
47
+ "packageManager": "pnpm@9.12.3+sha512.cce0f9de9c5a7c95bef944169cc5dfe8741abfb145078c0d508b868056848a87c81e626246cb60967cbd7fd29a6c062ef73ff840d96b3c86c40ac92cf4a813ee"
47
48
  }
package/dist/index.mjs DELETED
@@ -1,38 +0,0 @@
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
- };