vite-plugin-svgr 4.2.0 → 4.5.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
@@ -35,7 +35,7 @@ Then SVG files can be imported as React components:
35
35
  import Logo from "./logo.svg?react";
36
36
  ```
37
37
 
38
- 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`:
39
39
 
40
40
  ```ts
41
41
  /// <reference types="vite-plugin-svgr/client" />
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
@@ -15,26 +15,42 @@ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (
15
15
  }) : function(o, v) {
16
16
  o["default"] = v;
17
17
  });
18
- var __importStar = (this && this.__importStar) || function (mod) {
19
- if (mod && mod.__esModule) return mod;
20
- var result = {};
21
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
- __setModuleDefault(result, mod);
23
- return result;
24
- };
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
25
35
  var __importDefault = (this && this.__importDefault) || function (mod) {
26
36
  return (mod && mod.__esModule) ? mod : { "default": mod };
27
37
  };
28
38
  Object.defineProperty(exports, "__esModule", { value: true });
39
+ exports.default = vitePluginSvgr;
29
40
  const pluginutils_1 = require("@rollup/pluginutils");
30
41
  const fs_1 = __importDefault(require("fs"));
31
42
  const vite_1 = require("vite");
43
+ const viteModule = __importStar(require("vite"));
44
+ // @ts-ignore - check if transformWithOxc is available, i.e. rolldown-vite is installed and aliased
45
+ let useOxc = viteModule.transformWithOxc != null;
46
+ // @ts-ignore - assign transformer function
47
+ let transformWith = useOxc ? viteModule.transformWithOxc : vite_1.transformWithEsbuild;
32
48
  function vitePluginSvgr({ svgrOptions, esbuildOptions, include = "**/*.svg?react", exclude, } = {}) {
33
49
  const filter = (0, pluginutils_1.createFilter)(include, exclude);
34
50
  const postfixRE = /[?#].*$/s;
35
51
  return {
36
52
  name: "vite-plugin-svgr",
37
- enforce: "pre",
53
+ enforce: "pre", // to override `vite:asset`'s behavior
38
54
  async load(id) {
39
55
  if (filter(id)) {
40
56
  const { transform } = await Promise.resolve().then(() => __importStar(require("@svgr/core")));
@@ -47,7 +63,11 @@ function vitePluginSvgr({ svgrOptions, esbuildOptions, include = "**/*.svg?react
47
63
  defaultPlugins: [jsx],
48
64
  },
49
65
  });
50
- const res = await (0, vite_1.transformWithEsbuild)(componentCode, id, {
66
+ const res = await transformWith(componentCode, id, useOxc ? {
67
+ // @ts-ignore - "lang" is required for transformWithOxc
68
+ lang: "jsx",
69
+ ...esbuildOptions,
70
+ } : {
51
71
  loader: "jsx",
52
72
  ...esbuildOptions,
53
73
  });
@@ -59,4 +79,3 @@ function vitePluginSvgr({ svgrOptions, esbuildOptions, include = "**/*.svg?react
59
79
  },
60
80
  };
61
81
  }
62
- exports.default = vitePluginSvgr;
package/dist/index.js CHANGED
@@ -1,12 +1,17 @@
1
1
  import { createFilter } from "@rollup/pluginutils";
2
2
  import fs from "fs";
3
3
  import { transformWithEsbuild } from "vite";
4
+ import * as viteModule from "vite";
5
+ // @ts-ignore - check if transformWithOxc is available, i.e. rolldown-vite is installed and aliased
6
+ let useOxc = viteModule.transformWithOxc != null;
7
+ // @ts-ignore - assign transformer function
8
+ let transformWith = useOxc ? viteModule.transformWithOxc : transformWithEsbuild;
4
9
  export default function vitePluginSvgr({ svgrOptions, esbuildOptions, include = "**/*.svg?react", exclude, } = {}) {
5
10
  const filter = createFilter(include, exclude);
6
11
  const postfixRE = /[?#].*$/s;
7
12
  return {
8
13
  name: "vite-plugin-svgr",
9
- enforce: "pre",
14
+ enforce: "pre", // to override `vite:asset`'s behavior
10
15
  async load(id) {
11
16
  if (filter(id)) {
12
17
  const { transform } = await import("@svgr/core");
@@ -19,7 +24,11 @@ export default function vitePluginSvgr({ svgrOptions, esbuildOptions, include =
19
24
  defaultPlugins: [jsx],
20
25
  },
21
26
  });
22
- const res = await transformWithEsbuild(componentCode, id, {
27
+ const res = await transformWith(componentCode, id, useOxc ? {
28
+ // @ts-ignore - "lang" is required for transformWithOxc
29
+ lang: "jsx",
30
+ ...esbuildOptions,
31
+ } : {
23
32
  loader: "jsx",
24
33
  ...esbuildOptions,
25
34
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vite-plugin-svgr",
3
- "version": "4.2.0",
3
+ "version": "4.5.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.9.1",
36
- "typescript": "^5.2.2"
36
+ "@types/node": "^24.3.0",
37
+ "typescript": "^5.9.2"
37
38
  },
38
39
  "peerDependencies": {
39
- "vite": "^2.6.0 || 3 || 4 || 5"
40
+ "vite": ">=2.6.0"
40
41
  },
41
42
  "dependencies": {
42
- "@rollup/pluginutils": "^5.0.5",
43
+ "@rollup/pluginutils": "^5.2.0",
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@10.15.0+sha256.84c19e788d7d7ee248e4a6b7152f8ebba0f4fe7380a5f443ca17d76c030052d2"
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
- };