vite-plugin-svg-sprite 0.6.2 → 0.6.3

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
@@ -63,7 +63,7 @@ For users not using React or Vue, set the `exportType` to `'vanilla'`. The impor
63
63
  import IconFoo from './icons/foo.svg';
64
64
  const html = `
65
65
  <svg>
66
- <use xlink:href="#${IconFoo}" />
66
+ <use href="#${IconFoo}" />
67
67
  </svg>
68
68
  `;
69
69
  ```
@@ -83,7 +83,7 @@ To get proper type hints in TypeScript, include the appropriate type definitions
83
83
 
84
84
  - **symbolId**: (`string`, optional) Controls the generated symbol ID. Default is `'icon-[name]'`.
85
85
 
86
- - **exportType**: (`'vanilla' | 'react' | 'vue'`, optional) Determines the type of the exported value. Default is `'vanilla'`.
86
+ - **exportType**: (`'vanilla' | 'react' | 'vue'`, optional) Determines the type of the exported value. Default is `'vanilla'`.
87
87
  - If set to `'vanilla'`, the value will be the `symbolId`.
88
88
  - If set to `'react'`, the value will be a React component.
89
89
  - If set to `'vue'`, the value will be a Vue component.
package/cjs/index.d.ts CHANGED
@@ -1,13 +1,31 @@
1
1
  import { Config as SvgoOptimizeOptions } from 'svgo';
2
2
  import { Plugin } from 'vite';
3
3
  declare const exportTypes: readonly ["vanilla", "react", "vue"];
4
+ export interface DefaultExportType {
5
+ exportType?: (typeof exportTypes)[number];
6
+ }
7
+ interface AdapterOptions {
8
+ id: string;
9
+ name: string;
10
+ symbolXml: string;
11
+ attributes: {
12
+ width?: string | null;
13
+ height?: string | null;
14
+ viewBox?: string | null;
15
+ };
16
+ }
17
+ export interface CustomExportType {
18
+ exportType: 'custom';
19
+ adapter: (options: AdapterOptions) => unknown;
20
+ }
21
+ type ExportType = DefaultExportType | CustomExportType;
4
22
  export type { SvgoOptimizeOptions };
5
- export interface SvgSpriteOptions {
23
+ export interface BaseSvgSpriteOptions {
6
24
  include?: string[] | string;
7
25
  symbolId?: string;
8
26
  svgo?: SvgoOptimizeOptions;
9
- exportType?: (typeof exportTypes)[number];
10
27
  moduleSideEffects?: boolean;
11
28
  }
29
+ type SvgSpriteOptions = BaseSvgSpriteOptions & ExportType;
12
30
  declare const _default: (options?: SvgSpriteOptions) => Plugin<any>;
13
31
  export default _default;
package/cjs/index.js CHANGED
@@ -16,10 +16,49 @@ function getHash(content) {
16
16
  h.update(content);
17
17
  return h.digest('hex');
18
18
  }
19
+ function prepareTemplate(props) {
20
+ return (options) => {
21
+ var _a;
22
+ return `
23
+ import addSymbol from 'vite-plugin-svg-sprite/runtime/inject.js';
24
+ ${(_a = props.adapterImport) !== null && _a !== void 0 ? _a : ''}
25
+
26
+ const id = ${stringify(options.id)};
27
+ const name = ${stringify(options.name)};
28
+ const symbolXml = ${stringify(options.symbolXml)};
29
+ const { dispose } = addSymbol(symbolXml, id);
30
+
31
+ export default ${props.adapter};
32
+ export const attributes = ${stringify(options.attributes)}
33
+
34
+ if (import.meta.hot) {
35
+ import.meta.hot.dispose(dispose);
36
+ import.meta.hot.accept();
37
+ }
38
+ `;
39
+ };
40
+ }
41
+ function getModuleTemplate(type = {}) {
42
+ if (type.exportType === 'custom') {
43
+ if (!type.adapter) {
44
+ throw new Error('Export type is set to "custom", but adapter is not provided. Please add "adapter" option in config.');
45
+ }
46
+ return prepareTemplate({ adapter: type.adapter.toString() });
47
+ }
48
+ const { exportType = 'vanilla' } = type;
49
+ if (!exportTypes.includes(exportType)) {
50
+ throw new Error('Unknown export type, supported types: "custom", "react", "vanilla", "vue".');
51
+ }
52
+ return prepareTemplate({
53
+ adapterImport: `import { adapter } from 'vite-plugin-svg-sprite/runtime/adapters/${exportType}.js';`,
54
+ adapter: 'adapter(id, name)'
55
+ });
56
+ }
19
57
  exports.default = (options) => {
20
58
  var _a;
21
59
  const match = (_a = options === null || options === void 0 ? void 0 : options.include) !== null && _a !== void 0 ? _a : '**.svg';
22
60
  const svgoOptions = options === null || options === void 0 ? void 0 : options.svgo;
61
+ const moduleTemplate = getModuleTemplate(options);
23
62
  const plugin = {
24
63
  name: 'svg-sprite',
25
64
  async transform(src, filepath) {
@@ -50,28 +89,8 @@ exports.default = (options) => {
50
89
  throw new Error(`invalid svg file: ${filepath}`);
51
90
  }
52
91
  const { symbolXml, attributes } = symbolResults;
53
- let exportType = options === null || options === void 0 ? void 0 : options.exportType;
54
- if (!exportType || !exportTypes.includes(exportType)) {
55
- exportType = 'vanilla';
56
- }
57
- const codeToReturn = `
58
- import addSymbol from 'vite-plugin-svg-sprite/runtime/inject.js';
59
- import { adapter } from 'vite-plugin-svg-sprite/runtime/adapters/${exportType}.js';
60
- const id = ${stringify(symbolId)};
61
- const name = ${stringify(name)};
62
- const symbolXml = ${stringify(symbolXml)};
63
- const { dispose } = addSymbol(symbolXml, id);
64
-
65
- export default adapter(id, name);
66
- export const attributes = ${stringify(attributes)}
67
-
68
- if (import.meta.hot) {
69
- import.meta.hot.dispose(dispose);
70
- import.meta.hot.accept();
71
- }
72
- `;
73
92
  return {
74
- code: codeToReturn,
93
+ code: moduleTemplate({ id: symbolId, name, symbolXml, attributes }),
75
94
  moduleSideEffects: (_c = options === null || options === void 0 ? void 0 : options.moduleSideEffects) !== null && _c !== void 0 ? _c : true,
76
95
  map: { mappings: '' },
77
96
  };
@@ -8,7 +8,7 @@ const adapter = (id, name) => {
8
8
  width: '1em',
9
9
  height: '1em',
10
10
  ...props,
11
- }, (0, react_1.createElement)('use', { xlinkHref: `#${id}` }))));
11
+ }, (0, react_1.createElement)('use', { href: `#${id}` }))));
12
12
  Icon.displayName = `Icon${(0, utils_js_1.capitalizeFirstLetter)(name)}`;
13
13
  return Icon;
14
14
  };
@@ -11,7 +11,7 @@ const adapter = (id, name) => (0, vue_1.defineComponent)({
11
11
  height: '1em',
12
12
  ...attrs,
13
13
  }, [
14
- (0, vue_1.h)('use', { 'xlink:href': `#${id}` }),
14
+ (0, vue_1.h)('use', { href: `#${id}` }),
15
15
  ]);
16
16
  },
17
17
  });
package/esm/index.d.ts CHANGED
@@ -1,13 +1,31 @@
1
1
  import { Config as SvgoOptimizeOptions } from 'svgo';
2
2
  import { Plugin } from 'vite';
3
3
  declare const exportTypes: readonly ["vanilla", "react", "vue"];
4
+ export interface DefaultExportType {
5
+ exportType?: (typeof exportTypes)[number];
6
+ }
7
+ interface AdapterOptions {
8
+ id: string;
9
+ name: string;
10
+ symbolXml: string;
11
+ attributes: {
12
+ width?: string | null;
13
+ height?: string | null;
14
+ viewBox?: string | null;
15
+ };
16
+ }
17
+ export interface CustomExportType {
18
+ exportType: 'custom';
19
+ adapter: (options: AdapterOptions) => unknown;
20
+ }
21
+ type ExportType = DefaultExportType | CustomExportType;
4
22
  export type { SvgoOptimizeOptions };
5
- export interface SvgSpriteOptions {
23
+ export interface BaseSvgSpriteOptions {
6
24
  include?: string[] | string;
7
25
  symbolId?: string;
8
26
  svgo?: SvgoOptimizeOptions;
9
- exportType?: (typeof exportTypes)[number];
10
27
  moduleSideEffects?: boolean;
11
28
  }
29
+ type SvgSpriteOptions = BaseSvgSpriteOptions & ExportType;
12
30
  declare const _default: (options?: SvgSpriteOptions) => Plugin<any>;
13
31
  export default _default;
package/esm/index.js CHANGED
@@ -11,10 +11,49 @@ function getHash(content) {
11
11
  h.update(content);
12
12
  return h.digest('hex');
13
13
  }
14
+ function prepareTemplate(props) {
15
+ return (options) => {
16
+ var _a;
17
+ return `
18
+ import addSymbol from 'vite-plugin-svg-sprite/runtime/inject.js';
19
+ ${(_a = props.adapterImport) !== null && _a !== void 0 ? _a : ''}
20
+
21
+ const id = ${stringify(options.id)};
22
+ const name = ${stringify(options.name)};
23
+ const symbolXml = ${stringify(options.symbolXml)};
24
+ const { dispose } = addSymbol(symbolXml, id);
25
+
26
+ export default ${props.adapter};
27
+ export const attributes = ${stringify(options.attributes)}
28
+
29
+ if (import.meta.hot) {
30
+ import.meta.hot.dispose(dispose);
31
+ import.meta.hot.accept();
32
+ }
33
+ `;
34
+ };
35
+ }
36
+ function getModuleTemplate(type = {}) {
37
+ if (type.exportType === 'custom') {
38
+ if (!type.adapter) {
39
+ throw new Error('Export type is set to "custom", but adapter is not provided. Please add "adapter" option in config.');
40
+ }
41
+ return prepareTemplate({ adapter: type.adapter.toString() });
42
+ }
43
+ const { exportType = 'vanilla' } = type;
44
+ if (!exportTypes.includes(exportType)) {
45
+ throw new Error('Unknown export type, supported types: "custom", "react", "vanilla", "vue".');
46
+ }
47
+ return prepareTemplate({
48
+ adapterImport: `import { adapter } from 'vite-plugin-svg-sprite/runtime/adapters/${exportType}.js';`,
49
+ adapter: 'adapter(id, name)'
50
+ });
51
+ }
14
52
  export default (options) => {
15
53
  var _a;
16
54
  const match = (_a = options === null || options === void 0 ? void 0 : options.include) !== null && _a !== void 0 ? _a : '**.svg';
17
55
  const svgoOptions = options === null || options === void 0 ? void 0 : options.svgo;
56
+ const moduleTemplate = getModuleTemplate(options);
18
57
  const plugin = {
19
58
  name: 'svg-sprite',
20
59
  async transform(src, filepath) {
@@ -45,28 +84,8 @@ export default (options) => {
45
84
  throw new Error(`invalid svg file: ${filepath}`);
46
85
  }
47
86
  const { symbolXml, attributes } = symbolResults;
48
- let exportType = options === null || options === void 0 ? void 0 : options.exportType;
49
- if (!exportType || !exportTypes.includes(exportType)) {
50
- exportType = 'vanilla';
51
- }
52
- const codeToReturn = `
53
- import addSymbol from 'vite-plugin-svg-sprite/runtime/inject.js';
54
- import { adapter } from 'vite-plugin-svg-sprite/runtime/adapters/${exportType}.js';
55
- const id = ${stringify(symbolId)};
56
- const name = ${stringify(name)};
57
- const symbolXml = ${stringify(symbolXml)};
58
- const { dispose } = addSymbol(symbolXml, id);
59
-
60
- export default adapter(id, name);
61
- export const attributes = ${stringify(attributes)}
62
-
63
- if (import.meta.hot) {
64
- import.meta.hot.dispose(dispose);
65
- import.meta.hot.accept();
66
- }
67
- `;
68
87
  return {
69
- code: codeToReturn,
88
+ code: moduleTemplate({ id: symbolId, name, symbolXml, attributes }),
70
89
  moduleSideEffects: (_c = options === null || options === void 0 ? void 0 : options.moduleSideEffects) !== null && _c !== void 0 ? _c : true,
71
90
  map: { mappings: '' },
72
91
  };
@@ -5,7 +5,7 @@ export const adapter = (id, name) => {
5
5
  width: '1em',
6
6
  height: '1em',
7
7
  ...props,
8
- }, createElement('use', { xlinkHref: `#${id}` }))));
8
+ }, createElement('use', { href: `#${id}` }))));
9
9
  Icon.displayName = `Icon${capitalizeFirstLetter(name)}`;
10
10
  return Icon;
11
11
  };
@@ -8,7 +8,7 @@ export const adapter = (id, name) => defineComponent({
8
8
  height: '1em',
9
9
  ...attrs,
10
10
  }, [
11
- h('use', { 'xlink:href': `#${id}` }),
11
+ h('use', { href: `#${id}` }),
12
12
  ]);
13
13
  },
14
14
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vite-plugin-svg-sprite",
3
- "version": "0.6.2",
3
+ "version": "0.6.3",
4
4
  "description": "SVG sprite plugin for Vite",
5
5
  "type": "module",
6
6
  "exports": {
@@ -60,7 +60,7 @@
60
60
  },
61
61
  "peerDependencies": {
62
62
  "react": "17 || 18 || 19",
63
- "vite": "2 || 3 || 4 || 5 || 6",
63
+ "vite": "2 || 3 || 4 || 5 || 6 || 7",
64
64
  "vue": "3"
65
65
  },
66
66
  "peerDependenciesMeta": {