vite-plugin-svgr 0.1.0 → 0.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
@@ -1,16 +1,36 @@
1
1
  # vite-plugin-svgr
2
2
 
3
+ [![npm](https://img.shields.io/npm/v/vite-plugin-svgr.svg)](https://www.npmjs.com/package/vite-plugin-svgr)
4
+
3
5
  Vite plugin to transform SVGs into React components. Uses [svgr](https://github.com/gregberge/svgr) under the hood.
4
6
 
5
7
  ## Usage
6
8
 
7
9
  ```js
8
10
  // vite.config.js
9
- import svgr from 'vite-plugin-svgr'
11
+ import svgrPlugin from 'vite-plugin-svgr'
10
12
 
11
13
  export default {
12
14
  // ...
13
- plugins: [svgr()],
15
+ plugins: [svgrPlugin()],
16
+ }
17
+ ```
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:
20
+
21
+ ```js
22
+ import { ReactComponent as Logo } from './logo.svg'
23
+ ```
24
+
25
+ If you are using TypeScript, `vite-plugin-svgr/client` can be added to `tsconfig.json`:
26
+
27
+ ```json
28
+ {
29
+ // ...
30
+ "compilerOptions": {
31
+ // ...
32
+ "types": ["vite-plugin-svgr/client"]
33
+ }
14
34
  }
15
35
  ```
16
36
 
package/client.d.ts ADDED
@@ -0,0 +1,9 @@
1
+ // https://github.com/facebook/create-react-app/blob/0ee4765c39f820e5f4820abf4bf2e47b3324da7f/packages/react-scripts/lib/react-app.d.ts#L47-L56
2
+
3
+ declare module '*.svg' {
4
+ import * as React from 'react'
5
+
6
+ export const ReactComponent: React.FunctionComponent<
7
+ React.SVGProps<SVGSVGElement> & { title?: string }
8
+ >
9
+ }
package/lib/index.d.ts CHANGED
@@ -1,3 +1,2 @@
1
- import type { Plugin } from 'vite';
2
- declare const _default: () => Plugin;
3
- export = _default;
1
+ import { Plugin } from 'vite';
2
+ export default function svgrPlugin(): Plugin;
package/lib/index.js CHANGED
@@ -2,27 +2,29 @@
2
2
  var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
5
6
  const fs_1 = __importDefault(require("fs"));
6
- module.exports = function svgrPlugin() {
7
+ const vite_1 = require("vite");
8
+ function svgrPlugin() {
7
9
  // TODO: options
8
10
  return {
9
11
  name: 'vite:svgr',
10
12
  async transform(code, id) {
11
- if (id.endsWith('.svg')) {
13
+ if (id.endsWith('.svg') && !id.includes('node_modules')) {
12
14
  const svgr = require('@svgr/core').default;
13
- const esbuild = require('esbuild');
14
- const svg = await fs_1.default.promises.readFile(id, 'utf8');
15
- const componentCode = await svgr(svg, {}, { componentName: 'ReactComponent' }).then((res) => {
16
- return res.replace('export default ReactComponent', `export { ReactComponent }`);
17
- });
18
- const res = await esbuild.transform(componentCode + '\n' + code, {
19
- loader: 'jsx',
20
- });
15
+ const svgCode = await fs_1.default.promises.readFile(id, 'utf8');
16
+ const componentCode = await svgr(svgCode, {}, { componentName: 'ReactComponent' });
17
+ code =
18
+ code +
19
+ '\n' +
20
+ componentCode.replace('export default ReactComponent', 'export { ReactComponent }');
21
+ const res = await (0, vite_1.transformWithEsbuild)(code, id, { loader: 'jsx' });
21
22
  return {
22
23
  code: res.code,
23
- map: res.map,
24
+ map: null, // TODO:
24
25
  };
25
26
  }
26
27
  },
27
28
  };
28
- };
29
+ }
30
+ exports.default = svgrPlugin;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vite-plugin-svgr",
3
- "version": "0.1.0",
3
+ "version": "0.5.0",
4
4
  "description": "Vite plugin to transform SVGs into React components",
5
5
  "main": "lib/index.js",
6
6
  "scripts": {
@@ -8,31 +8,26 @@
8
8
  "build": "tsc",
9
9
  "prepublish": "npm run build"
10
10
  },
11
- "repository": {
12
- "type": "git",
13
- "url": "git+https://github.com/pd4d10/vite-plugin-svgr.git"
14
- },
11
+ "repository": "pd4d10/vite-plugin-svgr",
15
12
  "files": [
16
- "lib"
13
+ "lib",
14
+ "client.d.ts"
17
15
  ],
18
16
  "keywords": [
19
- "vite"
17
+ "vite",
18
+ "vite-plugin"
20
19
  ],
21
20
  "author": "Rongjian Zhang",
22
21
  "license": "MIT",
23
- "bugs": {
24
- "url": "https://github.com/pd4d10/vite-plugin-svgr/issues"
25
- },
26
- "homepage": "https://github.com/pd4d10/vite-plugin-svgr#readme",
27
22
  "devDependencies": {
28
- "typescript": "^4.1.3",
29
- "vite": "^2.0.0-beta.15",
30
- "@types/node": "^14.14.20"
23
+ "@types/node": "^16.11.6",
24
+ "typescript": "^4.4.4",
25
+ "vite": "^2.6.13"
31
26
  },
32
27
  "peerDependencies": {
33
- "vite": "^2.0.0-beta.1"
28
+ "vite": "^2.0.0"
34
29
  },
35
30
  "dependencies": {
36
- "@svgr/core": "^5.5.0"
31
+ "@svgr/core": "^6.0.0-alpha.2"
37
32
  }
38
33
  }