vite-plugin-svgr 1.0.0 → 2.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
@@ -8,18 +8,11 @@ Vite plugin to transform SVGs into React components. Uses [svgr](https://github.
8
8
 
9
9
  ```js
10
10
  // vite.config.js
11
- import svgrPlugin from 'vite-plugin-svgr'
11
+ import svgr from 'vite-plugin-svgr'
12
12
 
13
13
  export default {
14
14
  // ...
15
- plugins: [
16
- svgrPlugin({
17
- svgrOptions: {
18
- icon: true,
19
- // ...svgr options (https://react-svgr.com/docs/options/)
20
- },
21
- }),
22
- ],
15
+ plugins: [svgr()],
23
16
  }
24
17
  ```
25
18
 
@@ -29,16 +22,30 @@ Then SVG files can be imported as React components, just like [create-react-app]
29
22
  import { ReactComponent as Logo } from './logo.svg'
30
23
  ```
31
24
 
32
- If you are using TypeScript, `vite-plugin-svgr/client` can be added to `tsconfig.json`:
25
+ If you are using TypeScript, there is also a declaration helper for better type inference:
33
26
 
34
- ```json
35
- {
36
- // ...
37
- "compilerOptions": {
27
+ ```ts
28
+ /// <reference types="vite-plugin-svgr/client" />
29
+ ```
30
+
31
+ ### Options
32
+
33
+ ```js
34
+ svgr({
35
+ // Set it to `true` to export React component as default.
36
+ // Notice that it will overrides the default behavior of Vite.
37
+ exportAsDefault: false,
38
+
39
+ // svgr options: https://react-svgr.com/docs/options/
40
+ svgrOptions: {
38
41
  // ...
39
- "types": ["vite-plugin-svgr/client"]
40
- }
41
- }
42
+ },
43
+
44
+ // esbuild options, to transform jsx to js
45
+ esbuildOptions: {
46
+ // ...
47
+ },
48
+ })
42
49
  ```
43
50
 
44
51
  ## License
package/lib/index.d.ts ADDED
@@ -0,0 +1,15 @@
1
+ import type { Config } from '@svgr/core';
2
+ import { transformWithEsbuild } from 'vite';
3
+ import type { Plugin } from 'vite';
4
+ export interface ViteSvgrOptions {
5
+ /**
6
+ * Export React component as default. Notice that it will overrides
7
+ * the default behavior of Vite, which exports the URL as default
8
+ *
9
+ * @default false
10
+ */
11
+ exportAsDefault?: boolean;
12
+ svgrOptions?: Config;
13
+ esbuildOptions?: Parameters<typeof transformWithEsbuild>[2];
14
+ }
15
+ export default function viteSvgr({ exportAsDefault, svgrOptions, esbuildOptions, }?: ViteSvgrOptions): Plugin;
package/lib/index.js ADDED
@@ -0,0 +1,53 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
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
+ };
25
+ var __importDefault = (this && this.__importDefault) || function (mod) {
26
+ return (mod && mod.__esModule) ? mod : { "default": mod };
27
+ };
28
+ Object.defineProperty(exports, "__esModule", { value: true });
29
+ const fs_1 = __importDefault(require("fs"));
30
+ const vite_1 = require("vite");
31
+ function viteSvgr({ exportAsDefault, svgrOptions, esbuildOptions, } = {}) {
32
+ return {
33
+ name: 'vite-plugin-svgr',
34
+ async transform(code, id) {
35
+ if (id.endsWith('.svg')) {
36
+ const { transform } = await Promise.resolve().then(() => __importStar(require('@svgr/core')));
37
+ const svgCode = await fs_1.default.promises.readFile(id, 'utf8');
38
+ const componentCode = await transform(svgCode, svgrOptions, {
39
+ filePath: id,
40
+ caller: {
41
+ previousExport: exportAsDefault ? null : code,
42
+ },
43
+ });
44
+ const res = await (0, vite_1.transformWithEsbuild)(componentCode, id, Object.assign({ loader: 'jsx' }, esbuildOptions));
45
+ return {
46
+ code: res.code,
47
+ map: null, // TODO:
48
+ };
49
+ }
50
+ },
51
+ };
52
+ }
53
+ exports.default = viteSvgr;
package/package.json CHANGED
@@ -1,12 +1,13 @@
1
1
  {
2
2
  "name": "vite-plugin-svgr",
3
- "version": "1.0.0",
3
+ "version": "2.0.0",
4
4
  "description": "Vite plugin to transform SVGs into React components",
5
5
  "main": "lib/index.js",
6
+ "types": "lib/index.d.ts",
6
7
  "scripts": {
7
8
  "dev": "tsc --watch",
8
9
  "build": "tsc",
9
- "prepublish": "npm run build"
10
+ "prepare": "npm run build"
10
11
  },
11
12
  "repository": "pd4d10/vite-plugin-svgr",
12
13
  "files": [
@@ -20,14 +21,14 @@
20
21
  "author": "Rongjian Zhang",
21
22
  "license": "MIT",
22
23
  "devDependencies": {
23
- "@types/node": "^17.0.8",
24
- "typescript": "^4.5.4",
25
- "vite": "^2.7.10"
24
+ "@types/node": "^17.0.30",
25
+ "typescript": "^4.6.4",
26
+ "vite": "^2.9.6"
26
27
  },
27
28
  "peerDependencies": {
28
29
  "vite": "^2.6.0"
29
30
  },
30
31
  "dependencies": {
31
- "@svgr/core": "^6.2.0"
32
+ "@svgr/core": "^6.2.1"
32
33
  }
33
34
  }