vite-plugin-svgr 0.5.0 → 1.0.1

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
@@ -12,7 +12,14 @@ import svgrPlugin from 'vite-plugin-svgr'
12
12
 
13
13
  export default {
14
14
  // ...
15
- plugins: [svgrPlugin()],
15
+ plugins: [
16
+ svgrPlugin({
17
+ svgrOptions: {
18
+ icon: true,
19
+ // ...svgr options (https://react-svgr.com/docs/options/)
20
+ },
21
+ }),
22
+ ],
16
23
  }
17
24
  ```
18
25
 
package/lib/index.d.ts CHANGED
@@ -1,2 +1,9 @@
1
- import { Plugin } from 'vite';
2
- export default function svgrPlugin(): Plugin;
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;
package/lib/index.js CHANGED
@@ -1,24 +1,41 @@
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
- Object.defineProperty(exports, "__esModule", { value: true });
6
24
  const fs_1 = __importDefault(require("fs"));
7
25
  const vite_1 = require("vite");
8
- function svgrPlugin() {
9
- // TODO: options
26
+ module.exports = function svgrPlugin({ svgrOptions, esbuildOptions, } = {}) {
10
27
  return {
11
28
  name: 'vite:svgr',
12
29
  async transform(code, id) {
13
- if (id.endsWith('.svg') && !id.includes('node_modules')) {
14
- const svgr = require('@svgr/core').default;
30
+ if (id.endsWith('.svg')) {
31
+ const { transform: convert } = await Promise.resolve().then(() => __importStar(require('@svgr/core')));
15
32
  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' });
33
+ const componentCode = await convert(svgCode, svgrOptions, {
34
+ componentName: 'ReactComponent',
35
+ }).then((res) => {
36
+ return res.replace('export default ReactComponent', `export { ReactComponent }`);
37
+ });
38
+ const res = await (0, vite_1.transformWithEsbuild)(componentCode + '\n' + code, id, Object.assign({ loader: 'jsx' }, esbuildOptions));
22
39
  return {
23
40
  code: res.code,
24
41
  map: null, // TODO:
@@ -26,5 +43,4 @@ function svgrPlugin() {
26
43
  }
27
44
  },
28
45
  };
29
- }
30
- exports.default = svgrPlugin;
46
+ };
package/package.json CHANGED
@@ -1,13 +1,8 @@
1
1
  {
2
2
  "name": "vite-plugin-svgr",
3
- "version": "0.5.0",
3
+ "version": "1.0.1",
4
4
  "description": "Vite plugin to transform SVGs into React components",
5
5
  "main": "lib/index.js",
6
- "scripts": {
7
- "dev": "tsc --watch",
8
- "build": "tsc",
9
- "prepublish": "npm run build"
10
- },
11
6
  "repository": "pd4d10/vite-plugin-svgr",
12
7
  "files": [
13
8
  "lib",
@@ -20,14 +15,20 @@
20
15
  "author": "Rongjian Zhang",
21
16
  "license": "MIT",
22
17
  "devDependencies": {
23
- "@types/node": "^16.11.6",
24
- "typescript": "^4.4.4",
25
- "vite": "^2.6.13"
18
+ "@types/node": "^17.0.8",
19
+ "typescript": "^4.5.4",
20
+ "vite": "^2.7.10"
26
21
  },
27
22
  "peerDependencies": {
28
- "vite": "^2.0.0"
23
+ "vite": "^2.6.0"
29
24
  },
30
25
  "dependencies": {
31
- "@svgr/core": "^6.0.0-alpha.2"
32
- }
33
- }
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
+ }