-
Notifications
You must be signed in to change notification settings - Fork 0
/
vite.config.ts
48 lines (44 loc) · 1.49 KB
/
vite.config.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
import { sveltekit } from "@sveltejs/kit/vite"
import commonjs from "vite-plugin-commonjs"
import { defineConfig } from "vite"
import { execSync } from "node:child_process"
import MagicString from "magic-string"
const hash = execSync("git rev-parse --short HEAD").toString().trim()
export default defineConfig({
plugins: [sveltekit(), commonjs(), tablerOptimizer()],
define: {
__COMMIT_HASH__: JSON.stringify("_" + hash),
},
})
// taken from https://github.com/tabler/tabler-icons/issues/669#issuecomment-1993756128
// converts all named imports to direct imports during transforming
function tablerOptimizer(): import("vite").Plugin {
return {
name: "tabler-svelte optimizer",
transform(code, id) {
const ms = new MagicString(code, { filename: id })
ms.replace(
/([ \t]*)import\s+\{([^;]*?)\}\s+from\s+['"]@tabler\/icons-svelte['"];/g,
(match, whitespace: string, importNames: string) => {
const hasSemi = match.endsWith(";")
const imports = importNames
.split(",")
.map((v) => v.trim())
.map((name) => {
const path = name
return `${whitespace}import ${name} from '@tabler/icons-svelte/dist/svelte/icons/${path}.svelte'${
hasSemi ? ";" : ""
}`
})
return imports.join("\n")
}
)
if (ms.hasChanged()) {
return {
code: ms.toString(),
map: ms.generateMap(),
}
}
},
}
}