vite-plugin-svgr 0.3.0 → 0.6.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,24 +1,25 @@
1
1
  # vite-plugin-svgr
2
2
 
3
- Vite plugin to transform SVGs into React components. Uses [svgr](https://github.com/gregberge/svgr) under the hood.
4
-
5
- ## Prerequisites:
6
-
7
- add `vite-plugin-svgr/client` to `compilerOptions.types` of your `tsconfig`
3
+ [![npm](https://img.shields.io/npm/v/vite-plugin-svgr.svg)](https://www.npmjs.com/package/vite-plugin-svgr)
8
4
 
9
- ```js
10
- "types": ["vite-plugin-svgr/client"]
11
- ```
5
+ Vite plugin to transform SVGs into React components. Uses [svgr](https://github.com/gregberge/svgr) under the hood.
12
6
 
13
7
  ## Usage
14
8
 
15
9
  ```js
16
10
  // vite.config.js
17
- import svgr from 'vite-plugin-svgr'
11
+ import svgrPlugin from 'vite-plugin-svgr'
18
12
 
19
13
  export default {
20
14
  // ...
21
- plugins: [svgr()],
15
+ plugins: [
16
+ svgrPlugin({
17
+ svgrOptions: {
18
+ icon: true,
19
+ // ...svgr options (https://react-svgr.com/docs/options/)
20
+ },
21
+ }),
22
+ ],
22
23
  }
23
24
  ```
24
25
 
@@ -28,6 +29,18 @@ Then SVG files can be imported as React components, just like [create-react-app]
28
29
  import { ReactComponent as Logo } from './logo.svg'
29
30
  ```
30
31
 
32
+ If you are using TypeScript, `vite-plugin-svgr/client` can be added to `tsconfig.json`:
33
+
34
+ ```json
35
+ {
36
+ // ...
37
+ "compilerOptions": {
38
+ // ...
39
+ "types": ["vite-plugin-svgr/client"]
40
+ }
41
+ }
42
+ ```
43
+
31
44
  ## License
32
45
 
33
46
  MIT
package/client.d.ts CHANGED
@@ -6,7 +6,4 @@ declare module '*.svg' {
6
6
  export const ReactComponent: React.FunctionComponent<
7
7
  React.SVGProps<SVGSVGElement> & { title?: string }
8
8
  >
9
-
10
- const src: string
11
- export default src
12
9
  }
package/lib/index.d.ts CHANGED
@@ -1,3 +1,7 @@
1
+ import type { Config } from '@svgr/core';
1
2
  import type { Plugin } from 'vite';
2
- declare const _default: () => Plugin;
3
+ declare type Options = {
4
+ svgrOptions?: Config;
5
+ };
6
+ declare const _default: (options?: Options) => Plugin;
3
7
  export = _default;
package/lib/index.js CHANGED
@@ -1,26 +1,45 @@
1
1
  "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
+ }) : (function(o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ o[k2] = m[k];
8
+ }));
9
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
10
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
11
+ }) : function(o, v) {
12
+ o["default"] = v;
13
+ });
14
+ var __importStar = (this && this.__importStar) || function (mod) {
15
+ if (mod && mod.__esModule) return mod;
16
+ var result = {};
17
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
18
+ __setModuleDefault(result, mod);
19
+ return result;
20
+ };
2
21
  var __importDefault = (this && this.__importDefault) || function (mod) {
3
22
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
23
  };
5
24
  const fs_1 = __importDefault(require("fs"));
6
- module.exports = function svgrPlugin() {
7
- // TODO: options
25
+ const vite_1 = require("vite");
26
+ module.exports = function svgrPlugin(options = {}) {
27
+ const { svgrOptions = {} } = options;
8
28
  return {
9
29
  name: 'vite:svgr',
10
30
  async transform(code, id) {
11
31
  if (id.endsWith('.svg')) {
12
- 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) => {
32
+ const { transform: convert } = await Promise.resolve().then(() => __importStar(require('@svgr/core')));
33
+ const svgCode = await fs_1.default.promises.readFile(id, 'utf8');
34
+ const componentCode = await convert(svgCode, svgrOptions, {
35
+ componentName: 'ReactComponent',
36
+ }).then((res) => {
16
37
  return res.replace('export default ReactComponent', `export { ReactComponent }`);
17
38
  });
18
- const res = await esbuild.transform(componentCode + '\n' + code, {
19
- loader: 'jsx',
20
- });
39
+ const res = await (0, vite_1.transformWithEsbuild)(componentCode + '\n' + code, id, { loader: 'jsx' });
21
40
  return {
22
41
  code: res.code,
23
- // map: res.map, // TODO:
42
+ map: null, // TODO:
24
43
  };
25
44
  }
26
45
  },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vite-plugin-svgr",
3
- "version": "0.3.0",
3
+ "version": "0.6.0",
4
4
  "description": "Vite plugin to transform SVGs into React components",
5
5
  "main": "lib/index.js",
6
6
  "scripts": {
@@ -8,32 +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
13
  "lib",
17
14
  "client.d.ts"
18
15
  ],
19
16
  "keywords": [
20
- "vite"
17
+ "vite",
18
+ "vite-plugin"
21
19
  ],
22
20
  "author": "Rongjian Zhang",
23
21
  "license": "MIT",
24
- "bugs": {
25
- "url": "https://github.com/pd4d10/vite-plugin-svgr/issues"
26
- },
27
- "homepage": "https://github.com/pd4d10/vite-plugin-svgr#readme",
28
22
  "devDependencies": {
29
- "@types/node": "^15.12.2",
30
- "typescript": "^4.3.2",
31
- "vite": "^2.3.7"
23
+ "@types/node": "^16.11.6",
24
+ "typescript": "^4.4.4",
25
+ "vite": "^2.6.13"
32
26
  },
33
27
  "peerDependencies": {
34
- "vite": "^2.0.0"
28
+ "vite": "^2.6.0"
35
29
  },
36
30
  "dependencies": {
37
- "@svgr/core": "^5.5.0"
31
+ "@svgr/core": "^6.0.0-alpha.3"
38
32
  }
39
33
  }