vite-plugin-svgr 3.2.0 → 4.0.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
@@ -16,10 +16,10 @@ export default {
16
16
  };
17
17
  ```
18
18
 
19
- Then SVG files can be imported as React components, just like [create-react-app](https://create-react-app.dev/docs/adding-images-fonts-and-files#adding-svgs) does:
19
+ Then SVG files can be imported as React components:
20
20
 
21
21
  ```js
22
- import { ReactComponent as Logo } from "./logo.svg";
22
+ import Logo from "./logo.svg?react";
23
23
  ```
24
24
 
25
25
  If you are using TypeScript, there is also a declaration helper for better type inference:
@@ -32,10 +32,6 @@ If you are using TypeScript, there is also a declaration helper for better type
32
32
 
33
33
  ```js
34
34
  svgr({
35
- // Set it to `true` to export React component as default.
36
- // Notice that it will override the default behavior of Vite.
37
- exportAsDefault: false,
38
-
39
35
  // svgr options: https://react-svgr.com/docs/options/
40
36
  svgrOptions: {
41
37
  // ...
@@ -46,8 +42,8 @@ svgr({
46
42
  // ...
47
43
  },
48
44
 
49
- // 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.
50
- include: "**/*.svg",
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.
46
+ include: "**/*.svg?react",
51
47
 
52
48
  // A minimatch pattern, or array of patterns, which specifies the files in the build the plugin should ignore. By default no files are ignored.
53
49
  exclude: "",
package/client.d.ts CHANGED
@@ -1,10 +1,12 @@
1
1
  // https://github.com/facebook/create-react-app/blob/0ee4765c39f820e5f4820abf4bf2e47b3324da7f/packages/react-scripts/lib/react-app.d.ts#L47-L56
2
2
  // https://github.com/pd4d10/vite-plugin-svgr/pull/56 for preact compatiblility
3
3
 
4
- declare module "*.svg" {
4
+ declare module "*.svg?react" {
5
5
  import * as React from "react";
6
6
 
7
- export const ReactComponent: React.FunctionComponent<
7
+ const ReactComponent: React.FunctionComponent<
8
8
  React.ComponentProps<"svg"> & { title?: string }
9
9
  >;
10
+
11
+ export default ReactComponent;
10
12
  }
package/dist/index.cjs CHANGED
@@ -29,19 +29,20 @@ Object.defineProperty(exports, "__esModule", { value: true });
29
29
  const pluginutils_1 = require("@rollup/pluginutils");
30
30
  const fs_1 = __importDefault(require("fs"));
31
31
  const vite_1 = require("vite");
32
- function viteSvgr({ exportAsDefault, svgrOptions, esbuildOptions, include = "**/*.svg", exclude, } = {}) {
32
+ function vitePluginSvgr({ svgrOptions, esbuildOptions, include = "**/*.svg?react", exclude, } = {}) {
33
33
  const filter = (0, pluginutils_1.createFilter)(include, exclude);
34
+ const postfixRE = /[?#].*$/s;
34
35
  return {
35
36
  name: "vite-plugin-svgr",
36
37
  async transform(code, id) {
37
38
  if (filter(id)) {
38
39
  const { transform } = await Promise.resolve().then(() => __importStar(require("@svgr/core")));
39
40
  const { default: jsx } = await Promise.resolve().then(() => __importStar(require("@svgr/plugin-jsx")));
40
- const svgCode = await fs_1.default.promises.readFile(id.replace(/\?.*$/, ""), "utf8");
41
+ const filePath = id.replace(postfixRE, "");
42
+ const svgCode = await fs_1.default.promises.readFile(filePath, "utf8");
41
43
  const componentCode = await transform(svgCode, svgrOptions, {
42
- filePath: id,
44
+ filePath,
43
45
  caller: {
44
- previousExport: exportAsDefault ? null : code,
45
46
  defaultPlugins: [jsx],
46
47
  },
47
48
  });
@@ -57,4 +58,4 @@ function viteSvgr({ exportAsDefault, svgrOptions, esbuildOptions, include = "**/
57
58
  },
58
59
  };
59
60
  }
60
- exports.default = viteSvgr;
61
+ exports.default = vitePluginSvgr;
package/dist/index.d.ts CHANGED
@@ -2,17 +2,10 @@ import { FilterPattern } from "@rollup/pluginutils";
2
2
  import type { Config } from "@svgr/core";
3
3
  import type { Plugin } from "vite";
4
4
  import { transformWithEsbuild } from "vite";
5
- export interface ViteSvgrOptions {
6
- /**
7
- * Export React component as default. Notice that it will overrides
8
- * the default behavior of Vite, which exports the URL as default
9
- *
10
- * @default false
11
- */
12
- exportAsDefault?: boolean;
5
+ export interface VitePluginSvgrOptions {
13
6
  svgrOptions?: Config;
14
7
  esbuildOptions?: Parameters<typeof transformWithEsbuild>[2];
15
8
  exclude?: FilterPattern;
16
9
  include?: FilterPattern;
17
10
  }
18
- export default function viteSvgr({ exportAsDefault, svgrOptions, esbuildOptions, include, exclude, }?: ViteSvgrOptions): Plugin;
11
+ export default function vitePluginSvgr({ svgrOptions, esbuildOptions, include, exclude, }?: VitePluginSvgrOptions): Plugin;
package/dist/index.js CHANGED
@@ -1,19 +1,20 @@
1
1
  import { createFilter } from "@rollup/pluginutils";
2
2
  import fs from "fs";
3
3
  import { transformWithEsbuild } from "vite";
4
- export default function viteSvgr({ exportAsDefault, svgrOptions, esbuildOptions, include = "**/*.svg", exclude, } = {}) {
4
+ export default function vitePluginSvgr({ svgrOptions, esbuildOptions, include = "**/*.svg?react", exclude, } = {}) {
5
5
  const filter = createFilter(include, exclude);
6
+ const postfixRE = /[?#].*$/s;
6
7
  return {
7
8
  name: "vite-plugin-svgr",
8
9
  async transform(code, id) {
9
10
  if (filter(id)) {
10
11
  const { transform } = await import("@svgr/core");
11
12
  const { default: jsx } = await import("@svgr/plugin-jsx");
12
- const svgCode = await fs.promises.readFile(id.replace(/\?.*$/, ""), "utf8");
13
+ const filePath = id.replace(postfixRE, "");
14
+ const svgCode = await fs.promises.readFile(filePath, "utf8");
13
15
  const componentCode = await transform(svgCode, svgrOptions, {
14
- filePath: id,
16
+ filePath,
15
17
  caller: {
16
- previousExport: exportAsDefault ? null : code,
17
18
  defaultPlugins: [jsx],
18
19
  },
19
20
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vite-plugin-svgr",
3
- "version": "3.2.0",
3
+ "version": "4.0.0",
4
4
  "description": "Vite plugin to transform SVGs into React components",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -32,17 +32,16 @@
32
32
  "author": "Rongjian Zhang",
33
33
  "license": "MIT",
34
34
  "devDependencies": {
35
- "@types/node": "^18.16.0",
36
- "typescript": "^5.0.4",
37
- "vite": "^4.3.2"
35
+ "@types/node": "^20.6.2",
36
+ "typescript": "^5.2.2"
38
37
  },
39
38
  "peerDependencies": {
40
39
  "vite": "^2.6.0 || 3 || 4"
41
40
  },
42
41
  "dependencies": {
43
- "@rollup/pluginutils": "^5.0.2",
44
- "@svgr/core": "^7.0.0",
45
- "@svgr/plugin-jsx": "^7.0.0"
42
+ "@rollup/pluginutils": "^5.0.4",
43
+ "@svgr/core": "^8.1.0",
44
+ "@svgr/plugin-jsx": "^8.1.0"
46
45
  },
47
- "packageManager": "pnpm@8.4.0"
46
+ "packageManager": "pnpm@8.7.6"
48
47
  }