vite-plugin-svgr 3.3.0 → 4.1.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,18 @@
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
+ ```sh
9
+ # npm
10
+ npm install --save-dev vite-plugin-svgr
11
+
12
+ # yarn
13
+ yarn add -D vite-plugin-svgr
14
+
15
+ # pnpm
16
+ pnpm add -D vite-plugin-svgr
17
+ ```
18
+
7
19
  ## Usage
8
20
 
9
21
  ```js
@@ -16,10 +28,10 @@ export default {
16
28
  };
17
29
  ```
18
30
 
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:
31
+ Then SVG files can be imported as React components:
20
32
 
21
33
  ```js
22
- import { ReactComponent as Logo } from "./logo.svg";
34
+ import Logo from "./logo.svg?react";
23
35
  ```
24
36
 
25
37
  If you are using TypeScript, there is also a declaration helper for better type inference:
@@ -32,10 +44,6 @@ If you are using TypeScript, there is also a declaration helper for better type
32
44
 
33
45
  ```js
34
46
  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
47
  // svgr options: https://react-svgr.com/docs/options/
40
48
  svgrOptions: {
41
49
  // ...
@@ -46,8 +54,8 @@ svgr({
46
54
  // ...
47
55
  },
48
56
 
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",
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
+ include: "**/*.svg?react",
51
59
 
52
60
  // 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
61
  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,12 +29,13 @@ 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
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")));
@@ -43,7 +44,6 @@ function viteSvgr({ exportAsDefault, svgrOptions, esbuildOptions, include = "**/
43
44
  const componentCode = await transform(svgCode, svgrOptions, {
44
45
  filePath,
45
46
  caller: {
46
- previousExport: exportAsDefault ? null : code,
47
47
  defaultPlugins: [jsx],
48
48
  },
49
49
  });
@@ -59,4 +59,4 @@ function viteSvgr({ exportAsDefault, svgrOptions, esbuildOptions, include = "**/
59
59
  },
60
60
  };
61
61
  }
62
- exports.default = viteSvgr;
62
+ 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,12 +1,13 @@
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
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");
@@ -15,7 +16,6 @@ export default function viteSvgr({ exportAsDefault, svgrOptions, esbuildOptions,
15
16
  const componentCode = await transform(svgCode, svgrOptions, {
16
17
  filePath,
17
18
  caller: {
18
- previousExport: exportAsDefault ? null : code,
19
19
  defaultPlugins: [jsx],
20
20
  },
21
21
  });
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": "3.3.0",
3
+ "version": "4.1.0",
4
4
  "description": "Vite plugin to transform SVGs into React components",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -32,7 +32,7 @@
32
32
  "author": "Rongjian Zhang",
33
33
  "license": "MIT",
34
34
  "devDependencies": {
35
- "@types/node": "^20.6.2",
35
+ "@types/node": "^20.7.2",
36
36
  "typescript": "^5.2.2"
37
37
  },
38
38
  "peerDependencies": {