-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
34 lines (29 loc) · 902 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import { extname } from 'node:path';
import { createFilter } from '@rollup/pluginutils';
const injectNode = (svg) => `
export default function() {
return (new DOMParser().parseFromString(${svg}, 'image/svg+xml')).firstChild;
};
`;
const injectString = (svg) => `export default ${svg};`;
/**
* @param options
* @param options.include
* @param options.exclude
* @param options.stringify - if true returns String, otherwise returns DOM Node
* @returns {import('rollup').Plugin}
*/
export default function svgImportPlugin(options = {}) {
const filter = createFilter(options.include, options.exclude);
return {
name: 'svg-import',
transform: (code, id) => {
if (!filter(id) || extname(id) !== '.svg') return null;
const content = JSON.stringify(code);
return {
code: options.stringify ? injectString(content) : injectNode(content),
map: { mappings: '' },
};
},
};
}