vite-plugin-svgr 1.0.1 → 2.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
@@ -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
@@ -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;
@@ -1,7 +1,11 @@
1
1
  "use strict";
2
2
  var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
3
  if (k2 === undefined) k2 = k;
4
- Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[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);
5
9
  }) : (function(o, m, k, k2) {
6
10
  if (k2 === undefined) k2 = k;
7
11
  o[k2] = m[k];
@@ -21,21 +25,23 @@ var __importStar = (this && this.__importStar) || function (mod) {
21
25
  var __importDefault = (this && this.__importDefault) || function (mod) {
22
26
  return (mod && mod.__esModule) ? mod : { "default": mod };
23
27
  };
28
+ Object.defineProperty(exports, "__esModule", { value: true });
24
29
  const fs_1 = __importDefault(require("fs"));
25
30
  const vite_1 = require("vite");
26
- module.exports = function svgrPlugin({ svgrOptions, esbuildOptions, } = {}) {
31
+ function viteSvgr({ exportAsDefault, svgrOptions, esbuildOptions, } = {}) {
27
32
  return {
28
- name: 'vite:svgr',
33
+ name: 'vite-plugin-svgr',
29
34
  async transform(code, id) {
30
35
  if (id.endsWith('.svg')) {
31
- const { transform: convert } = await Promise.resolve().then(() => __importStar(require('@svgr/core')));
36
+ const { transform } = await Promise.resolve().then(() => __importStar(require('@svgr/core')));
32
37
  const svgCode = await fs_1.default.promises.readFile(id, 'utf8');
33
- const componentCode = await convert(svgCode, svgrOptions, {
34
- componentName: 'ReactComponent',
35
- }).then((res) => {
36
- return res.replace('export default ReactComponent', `export { ReactComponent }`);
38
+ const componentCode = await transform(svgCode, svgrOptions, {
39
+ filePath: id,
40
+ caller: {
41
+ previousExport: exportAsDefault ? null : code,
42
+ },
37
43
  });
38
- const res = await (0, vite_1.transformWithEsbuild)(componentCode + '\n' + code, id, Object.assign({ loader: 'jsx' }, esbuildOptions));
44
+ const res = await (0, vite_1.transformWithEsbuild)(componentCode, id, Object.assign({ loader: 'jsx' }, esbuildOptions));
39
45
  return {
40
46
  code: res.code,
41
47
  map: null, // TODO:
@@ -43,4 +49,5 @@ module.exports = function svgrPlugin({ svgrOptions, esbuildOptions, } = {}) {
43
49
  }
44
50
  },
45
51
  };
46
- };
52
+ }
53
+ exports.default = viteSvgr;
package/dist/index.mjs ADDED
@@ -0,0 +1,24 @@
1
+ import fs from 'fs';
2
+ import { transformWithEsbuild } from 'vite';
3
+ export default function viteSvgr({ exportAsDefault, svgrOptions, esbuildOptions, } = {}) {
4
+ return {
5
+ name: 'vite-plugin-svgr',
6
+ async transform(code, id) {
7
+ if (id.endsWith('.svg')) {
8
+ const { transform } = await import('@svgr/core');
9
+ const svgCode = await fs.promises.readFile(id, 'utf8');
10
+ const componentCode = await transform(svgCode, svgrOptions, {
11
+ filePath: id,
12
+ caller: {
13
+ previousExport: exportAsDefault ? null : code,
14
+ },
15
+ });
16
+ const res = await transformWithEsbuild(componentCode, id, Object.assign({ loader: 'jsx' }, esbuildOptions));
17
+ return {
18
+ code: res.code,
19
+ map: null, // TODO:
20
+ };
21
+ }
22
+ },
23
+ };
24
+ }
package/package.json CHANGED
@@ -1,11 +1,25 @@
1
1
  {
2
2
  "name": "vite-plugin-svgr",
3
- "version": "1.0.1",
3
+ "version": "2.1.0",
4
4
  "description": "Vite plugin to transform SVGs into React components",
5
- "main": "lib/index.js",
5
+ "main": "./dist/index.js",
6
+ "module": "./dist/index.mjs",
7
+ "types": "./dist/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./dist/index.d.ts",
11
+ "import": "./dist/index.mjs",
12
+ "require": "./dist/index.js"
13
+ }
14
+ },
15
+ "scripts": {
16
+ "dev": "tsc --watch",
17
+ "build": "tsc && tsc --module esnext --outDir temp && mv temp/index.js dist/index.mjs && rm -rf temp",
18
+ "prepare": "rm -rf lib && npm run build"
19
+ },
6
20
  "repository": "pd4d10/vite-plugin-svgr",
7
21
  "files": [
8
- "lib",
22
+ "dist",
9
23
  "client.d.ts"
10
24
  ],
11
25
  "keywords": [
@@ -15,20 +29,14 @@
15
29
  "author": "Rongjian Zhang",
16
30
  "license": "MIT",
17
31
  "devDependencies": {
18
- "@types/node": "^17.0.8",
19
- "typescript": "^4.5.4",
20
- "vite": "^2.7.10"
32
+ "@types/node": "^17.0.30",
33
+ "typescript": "^4.6.4",
34
+ "vite": "^2.9.6"
21
35
  },
22
36
  "peerDependencies": {
23
37
  "vite": "^2.6.0"
24
38
  },
25
39
  "dependencies": {
26
- "@svgr/core": "^6.2.0"
27
- },
28
- "scripts": {
29
- "dev": "tsc --watch",
30
- "build": "tsc",
31
- "prepublish": "npm run build"
32
- },
33
- "readme": "# vite-plugin-svgr\n\n[![npm](https://img.shields.io/npm/v/vite-plugin-svgr.svg)](https://www.npmjs.com/package/vite-plugin-svgr)\n\nVite plugin to transform SVGs into React components. Uses [svgr](https://github.com/gregberge/svgr) under the hood.\n\n## Usage\n\n```js\n// vite.config.js\nimport svgrPlugin from 'vite-plugin-svgr'\n\nexport default {\n // ...\n plugins: [\n svgrPlugin({\n svgrOptions: {\n icon: true,\n // ...svgr options (https://react-svgr.com/docs/options/)\n },\n }),\n ],\n}\n```\n\nThen 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:\n\n```js\nimport { ReactComponent as Logo } from './logo.svg'\n```\n\nIf you are using TypeScript, `vite-plugin-svgr/client` can be added to `tsconfig.json`:\n\n```json\n{\n // ...\n \"compilerOptions\": {\n // ...\n \"types\": [\"vite-plugin-svgr/client\"]\n }\n}\n```\n\n## License\n\nMIT\n"
34
- }
40
+ "@svgr/core": "^6.2.1"
41
+ }
42
+ }
package/lib/index.d.ts DELETED
@@ -1,9 +0,0 @@
1
- import type { Config } from '@svgr/core';
2
- import { transformWithEsbuild } from 'vite';
3
- import type { Plugin } from 'vite';
4
- declare type Options = {
5
- svgrOptions?: Config;
6
- esbuildOptions?: Parameters<typeof transformWithEsbuild>[2];
7
- };
8
- declare const _default: ({ svgrOptions, esbuildOptions, }?: Options) => Plugin;
9
- export = _default;