vite-plugin-svg-sprite 0.3.2 → 0.4.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 +29 -3
- package/{es → cjs}/index.d.ts +1 -1
- package/{lib → cjs}/index.js +8 -3
- package/cjs/package.json +3 -0
- package/client.d.ts +9 -0
- package/{lib → esm}/index.d.ts +1 -1
- package/{es → esm}/index.js +8 -3
- package/package.json +40 -16
- package/src/index.ts +11 -5
- package/index.mjs +0 -3
- /package/{es → cjs}/runtime.d.ts +0 -0
- /package/{lib → cjs}/runtime.js +0 -0
- /package/{lib → esm}/runtime.d.ts +0 -0
- /package/{es → esm}/runtime.js +0 -0
package/README.md
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
# vite-plugin-svg-sprite
|
|
2
|
+
|
|
2
3
|
SVG sprite plugin for [Vite](https://github.com/vitejs/vite)
|
|
3
4
|
|
|
4
5
|
## install
|
|
@@ -7,7 +8,8 @@ npm i vite-plugin-svg-sprite -D
|
|
|
7
8
|
```
|
|
8
9
|
|
|
9
10
|
## Usage
|
|
10
|
-
|
|
11
|
+
|
|
12
|
+
Add the plugin to your `vite.config.js`:
|
|
11
13
|
|
|
12
14
|
```javascript
|
|
13
15
|
import createSvgSpritePlugin from 'vite-plugin-svg-sprite';
|
|
@@ -21,8 +23,9 @@ const config = {
|
|
|
21
23
|
}
|
|
22
24
|
```
|
|
23
25
|
|
|
24
|
-
app code:
|
|
25
|
-
|
|
26
|
+
Then use it like that in your app code:
|
|
27
|
+
|
|
28
|
+
```jsx
|
|
26
29
|
import appIconId from './path/to/icons/app.svg';
|
|
27
30
|
|
|
28
31
|
// react or vue component, as you want
|
|
@@ -37,6 +40,29 @@ export default function App() {
|
|
|
37
40
|
}
|
|
38
41
|
```
|
|
39
42
|
|
|
43
|
+
You can also access the `width`/`height` attributes of the SVG with the `size` export:
|
|
44
|
+
|
|
45
|
+
```jsx
|
|
46
|
+
import appIconId, { size } from './path/to/icons/app.svg';
|
|
47
|
+
|
|
48
|
+
// react or vue component, as you want
|
|
49
|
+
export default function App() {
|
|
50
|
+
return (
|
|
51
|
+
<svg {...size}>
|
|
52
|
+
<use
|
|
53
|
+
xlinkHref={`#${appIconId}`}
|
|
54
|
+
/>
|
|
55
|
+
</svg>
|
|
56
|
+
);
|
|
57
|
+
}
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
If you're using TypeScript, add the following line to your `vite-env.d.ts`:
|
|
61
|
+
```diff
|
|
62
|
+
/// <reference types="vite/client" />
|
|
63
|
+
+ /// <reference types="vite-plugin-svg-sprite/client" />
|
|
64
|
+
```
|
|
65
|
+
|
|
40
66
|
## options
|
|
41
67
|
|
|
42
68
|
```javascript
|
package/{es → cjs}/index.d.ts
RENAMED
package/{lib → cjs}/index.js
RENAMED
|
@@ -8,6 +8,7 @@ const fs_1 = __importDefault(require("fs"));
|
|
|
8
8
|
const crypto_1 = __importDefault(require("crypto"));
|
|
9
9
|
const micromatch_1 = __importDefault(require("micromatch"));
|
|
10
10
|
const svg_baker_1 = __importDefault(require("svg-baker"));
|
|
11
|
+
const svg_parser_1 = require("svg-parser");
|
|
11
12
|
const svgo_1 = require("svgo");
|
|
12
13
|
const { stringify } = JSON;
|
|
13
14
|
exports.default = (options) => {
|
|
@@ -18,18 +19,21 @@ exports.default = (options) => {
|
|
|
18
19
|
const plugin = {
|
|
19
20
|
name: 'svg-sprite',
|
|
20
21
|
async transform(src, filepath) {
|
|
22
|
+
var _a, _b;
|
|
21
23
|
if (!micromatch_1.default.isMatch(filepath, match, {
|
|
22
24
|
dot: true,
|
|
23
25
|
})) {
|
|
24
26
|
return undefined;
|
|
25
27
|
}
|
|
26
28
|
let code = await fs_1.default.promises.readFile(filepath, 'utf-8');
|
|
29
|
+
const root = (0, svg_parser_1.parse)(code);
|
|
30
|
+
let topLevelAttributes = {};
|
|
31
|
+
if (((_a = root.children[0]) === null || _a === void 0 ? void 0 : _a.type) === 'element') {
|
|
32
|
+
topLevelAttributes = (_b = root.children[0].properties) !== null && _b !== void 0 ? _b : {};
|
|
33
|
+
}
|
|
27
34
|
const { name } = path_1.default.parse(filepath);
|
|
28
35
|
if (svgoOptions !== false) {
|
|
29
36
|
const result = ((0, svgo_1.optimize)(code, svgoOptions === true ? undefined : svgoOptions));
|
|
30
|
-
if (result.error != null) {
|
|
31
|
-
throw new Error(result.error);
|
|
32
|
-
}
|
|
33
37
|
code = result.data;
|
|
34
38
|
}
|
|
35
39
|
let id = name;
|
|
@@ -51,6 +55,7 @@ exports.default = (options) => {
|
|
|
51
55
|
import addSymbol from 'vite-plugin-svg-sprite/runtime';
|
|
52
56
|
addSymbol(${stringify(symbol.render())}, ${stringify(id)});
|
|
53
57
|
export default ${stringify(id)};
|
|
58
|
+
export const size = { width: ${stringify(topLevelAttributes.width)}, height: ${stringify(topLevelAttributes.height)} };
|
|
54
59
|
`;
|
|
55
60
|
return {
|
|
56
61
|
code: codeToReturn,
|
package/cjs/package.json
ADDED
package/client.d.ts
ADDED
package/{lib → esm}/index.d.ts
RENAMED
package/{es → esm}/index.js
RENAMED
|
@@ -3,6 +3,7 @@ import fs from 'fs';
|
|
|
3
3
|
import crypto from 'crypto';
|
|
4
4
|
import micromatch from 'micromatch';
|
|
5
5
|
import SVGCompiler from 'svg-baker';
|
|
6
|
+
import { parse } from 'svg-parser';
|
|
6
7
|
import { optimize } from 'svgo';
|
|
7
8
|
const { stringify } = JSON;
|
|
8
9
|
export default (options) => {
|
|
@@ -13,18 +14,21 @@ export default (options) => {
|
|
|
13
14
|
const plugin = {
|
|
14
15
|
name: 'svg-sprite',
|
|
15
16
|
async transform(src, filepath) {
|
|
17
|
+
var _a, _b;
|
|
16
18
|
if (!micromatch.isMatch(filepath, match, {
|
|
17
19
|
dot: true,
|
|
18
20
|
})) {
|
|
19
21
|
return undefined;
|
|
20
22
|
}
|
|
21
23
|
let code = await fs.promises.readFile(filepath, 'utf-8');
|
|
24
|
+
const root = parse(code);
|
|
25
|
+
let topLevelAttributes = {};
|
|
26
|
+
if (((_a = root.children[0]) === null || _a === void 0 ? void 0 : _a.type) === 'element') {
|
|
27
|
+
topLevelAttributes = (_b = root.children[0].properties) !== null && _b !== void 0 ? _b : {};
|
|
28
|
+
}
|
|
22
29
|
const { name } = p.parse(filepath);
|
|
23
30
|
if (svgoOptions !== false) {
|
|
24
31
|
const result = (optimize(code, svgoOptions === true ? undefined : svgoOptions));
|
|
25
|
-
if (result.error != null) {
|
|
26
|
-
throw new Error(result.error);
|
|
27
|
-
}
|
|
28
32
|
code = result.data;
|
|
29
33
|
}
|
|
30
34
|
let id = name;
|
|
@@ -46,6 +50,7 @@ export default (options) => {
|
|
|
46
50
|
import addSymbol from 'vite-plugin-svg-sprite/runtime';
|
|
47
51
|
addSymbol(${stringify(symbol.render())}, ${stringify(id)});
|
|
48
52
|
export default ${stringify(id)};
|
|
53
|
+
export const size = { width: ${stringify(topLevelAttributes.width)}, height: ${stringify(topLevelAttributes.height)} };
|
|
49
54
|
`;
|
|
50
55
|
return {
|
|
51
56
|
code: codeToReturn,
|
package/package.json
CHANGED
|
@@ -1,26 +1,46 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "vite-plugin-svg-sprite",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"description": "SVG sprite plugin for Vite",
|
|
5
|
-
"
|
|
6
|
-
"types": "lib/index.d.ts",
|
|
5
|
+
"type": "module",
|
|
7
6
|
"exports": {
|
|
8
7
|
".": {
|
|
9
|
-
"import":
|
|
10
|
-
|
|
8
|
+
"import": {
|
|
9
|
+
"types": "./esm/index.d.ts",
|
|
10
|
+
"default": "./esm/index.js"
|
|
11
|
+
},
|
|
12
|
+
"require": {
|
|
13
|
+
"types": "./cjs/index.d.ts",
|
|
14
|
+
"default": "./cjs/index.js"
|
|
15
|
+
}
|
|
11
16
|
},
|
|
12
|
-
"./runtime":
|
|
17
|
+
"./runtime": {
|
|
18
|
+
"import": {
|
|
19
|
+
"types": "./esm/runtime.d.ts",
|
|
20
|
+
"default": "./esm/runtime.js"
|
|
21
|
+
},
|
|
22
|
+
"require": {
|
|
23
|
+
"types": "./cjs/runtime.d.ts",
|
|
24
|
+
"default": "./cjs/runtime.js"
|
|
25
|
+
}
|
|
26
|
+
},
|
|
27
|
+
"./client": {
|
|
28
|
+
"types": "./client.d.ts"
|
|
29
|
+
}
|
|
13
30
|
},
|
|
31
|
+
"main": "cjs/index.js",
|
|
32
|
+
"types": "cjs/index.d.ts",
|
|
14
33
|
"files": [
|
|
15
|
-
"
|
|
16
|
-
"
|
|
34
|
+
"cjs",
|
|
35
|
+
"esm",
|
|
17
36
|
"src",
|
|
37
|
+
"client.d.ts",
|
|
18
38
|
"index.mjs",
|
|
19
39
|
"LICENSE"
|
|
20
40
|
],
|
|
21
41
|
"scripts": {
|
|
22
42
|
"lint": "eslint src",
|
|
23
|
-
"build": "tsc && npx tsc -m
|
|
43
|
+
"build": "rm -rf cjs esm && tsc && npx tsc -m commonjs --outDir cjs && cp ./package-cjs.json cjs/package.json",
|
|
24
44
|
"prepublish": "npm run build"
|
|
25
45
|
},
|
|
26
46
|
"keywords": [
|
|
@@ -35,19 +55,23 @@
|
|
|
35
55
|
"license": "MIT",
|
|
36
56
|
"devDependencies": {
|
|
37
57
|
"@types/micromatch": "^4.0.1",
|
|
38
|
-
"@types/node": "^
|
|
39
|
-
"@types/svgo": "^
|
|
40
|
-
"@
|
|
41
|
-
|
|
42
|
-
"eslint-
|
|
58
|
+
"@types/node": "^20.4.2",
|
|
59
|
+
"@types/svgo": "^3.0.0",
|
|
60
|
+
"@types/svg-parser": "^2.0.3",
|
|
61
|
+
|
|
62
|
+
"@typescript-eslint/eslint-plugin": "^6.0.0",
|
|
63
|
+
"eslint": "^8.45.0",
|
|
64
|
+
"eslint-config-airbnb-base": "^15.0.0",
|
|
65
|
+
"eslint-config-airbnb-typescript": "^17.1.0",
|
|
43
66
|
"eslint-plugin-import": "^2.22.1",
|
|
44
|
-
"typescript": "^
|
|
67
|
+
"typescript": "^5.1.6",
|
|
45
68
|
"vite": "^4.0.0"
|
|
46
69
|
},
|
|
47
70
|
"dependencies": {
|
|
48
71
|
"micromatch": "^4.0.2",
|
|
49
72
|
"svg-baker": "~1.7.0",
|
|
50
|
-
"
|
|
73
|
+
"svg-parser": "^2.0.4",
|
|
74
|
+
"svgo": "^3.0.2"
|
|
51
75
|
},
|
|
52
76
|
"peerDependencies": {
|
|
53
77
|
"vite": "^2 || ^3 || ^4"
|
package/src/index.ts
CHANGED
|
@@ -3,7 +3,8 @@ import fs from 'fs';
|
|
|
3
3
|
import crypto from 'crypto';
|
|
4
4
|
import micromatch from 'micromatch';
|
|
5
5
|
import SVGCompiler from 'svg-baker';
|
|
6
|
-
import {
|
|
6
|
+
import { parse } from 'svg-parser';
|
|
7
|
+
import { optimize, Config as SvgoOptimizeOptions } from 'svgo';
|
|
7
8
|
import { Plugin } from 'vite';
|
|
8
9
|
|
|
9
10
|
const { stringify } = JSON;
|
|
@@ -31,12 +32,16 @@ export default (options?: SvgSpriteOptions) => {
|
|
|
31
32
|
}
|
|
32
33
|
|
|
33
34
|
let code = await fs.promises.readFile(filepath, 'utf-8');
|
|
35
|
+
|
|
36
|
+
const root = parse(code);
|
|
37
|
+
let topLevelAttributes: Record<string, string | number> = {};
|
|
38
|
+
if (root.children[0]?.type === 'element') {
|
|
39
|
+
topLevelAttributes = root.children[0].properties ?? {};
|
|
40
|
+
}
|
|
41
|
+
|
|
34
42
|
const { name } = p.parse(filepath);
|
|
35
43
|
if (svgoOptions !== false) {
|
|
36
44
|
const result = (optimize(code, svgoOptions === true ? undefined : svgoOptions));
|
|
37
|
-
if (result.error != null) {
|
|
38
|
-
throw new Error(result.error);
|
|
39
|
-
}
|
|
40
45
|
code = result.data;
|
|
41
46
|
}
|
|
42
47
|
|
|
@@ -64,12 +69,13 @@ export default (options?: SvgSpriteOptions) => {
|
|
|
64
69
|
import addSymbol from 'vite-plugin-svg-sprite/runtime';
|
|
65
70
|
addSymbol(${stringify(symbol.render())}, ${stringify(id)});
|
|
66
71
|
export default ${stringify(id)};
|
|
72
|
+
export const size = { width: ${stringify(topLevelAttributes.width)}, height: ${stringify(topLevelAttributes.height)} };
|
|
67
73
|
`;
|
|
68
74
|
|
|
69
75
|
return {
|
|
70
76
|
code: codeToReturn,
|
|
71
77
|
map: { mappings: '' },
|
|
72
|
-
}
|
|
78
|
+
};
|
|
73
79
|
},
|
|
74
80
|
};
|
|
75
81
|
|
package/index.mjs
DELETED
/package/{es → cjs}/runtime.d.ts
RENAMED
|
File without changes
|
/package/{lib → cjs}/runtime.js
RENAMED
|
File without changes
|
|
File without changes
|
/package/{es → esm}/runtime.js
RENAMED
|
File without changes
|