-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
48 lines (41 loc) · 1.16 KB
/
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import fs from 'fs'
import path from 'path'
import { loadConfig, optimize } from 'svgo'
import { fileURLToPath } from 'url'
const defaultConfigFile = path.join(
fileURLToPath(import.meta.url),
'..',
'svgo.config.js'
)
function convert(svg, ts) {
const script = ts
? `<script lang="ts">
export let width: number | string | undefined = undefined;
export let height: number | string | undefined = undefined;
export let fill = 'currentColor';
</script>`
: `<script>
export let width;
export let height;
export let fill = 'currentColor';
</script>`
const p = svg.indexOf('>')
return `${script}
<!-- svelte-ignore a11y-no-static-element-interactions -->
${svg.slice(0, p)} {width} height={!width && !height ? "1em" : height} {fill} {...$$restProps} on:click on:keydown on:keyup>${svg.slice(p + 1)}`
}
export default async function (
input,
output,
ts,
configFile = defaultConfigFile
) {
const config = await loadConfig(configFile)
const data = fs.readFileSync(input, 'utf8')
const result = optimize(data, {
path: input,
...config
})
fs.writeFileSync(input, result.data)
fs.writeFileSync(output, convert(result.data, ts))
}