-
Notifications
You must be signed in to change notification settings - Fork 12
/
index.ts
65 lines (51 loc) · 1.93 KB
/
index.ts
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import { extname } from "node:path";
import { readFile } from "node:fs/promises";
import { optimize as optimise, Config as SVGOConfig } from "svgo";
import { createFilter } from "rollup-pluginutils";
import type { Plugin } from "./rollup";
const svgRegex = /(<svg.*?)(>)(.*)/s;
const svgheader = /^\<\?xml.+?\>/;
function recompose(source: string) {
const parts = svgRegex.exec(source);
if (!parts) throw new Error("Unable to parse as svg.");
const [, svgStart, end, svgBody] = parts;
return `${svgStart} role="img" {...$$props} ${end}<slot/>${svgBody}`;
}
const SVELTE_EXT = ".rollup-svg.svelte";
const SVG_SVELTE_EXT = ".svg" + SVELTE_EXT;
interface SvelteSVGOptions {
include?: string | RegExp | Array<string | RegExp>;
exclude?: string | RegExp | Array<string | RegExp>;
svgo?: SVGOConfig;
/**
* Only for Vite
*
* @see https://vitejs.dev/guide/api-plugin.html#plugin-ordering
* */
enforce?: "pre" | "post";
}
export function svelteSVG(options: SvelteSVGOptions = {}): Plugin {
const { svgo, enforce } = options;
const filter = createFilter(options.include, options.exclude);
return {
name: "rollup-plugin-svelte-svg",
// vite-only
...(enforce && { enforce }),
async resolveId(source, importer, options) {
if (!filter(source) || (extname(source) !== ".svg" && !source.endsWith(SVG_SVELTE_EXT)))
return null;
// vite is calling us with ".rollup-svg.svelte"
if (source.endsWith(SVG_SVELTE_EXT)) source = source.slice(0, -SVELTE_EXT.length);
const resolved = await this.resolve(source, importer, { ...options, skipSelf: true });
if (resolved?.id.endsWith(SVG_SVELTE_EXT)) return resolved.id;
return resolved && resolved.id + SVELTE_EXT;
},
load(id) {
if (!id.endsWith(SVG_SVELTE_EXT)) return null;
return readFile(id.slice(0, -SVELTE_EXT.length), "utf-8")
.then(file => (svgo ? optimise(file, svgo).data : file))
.then(file => file.replace(svgheader, "").trim())
.then(recompose);
},
};
}