-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvite.config.js
112 lines (103 loc) · 3.69 KB
/
vite.config.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import path from "path"
import fs from "fs/promises"
import autoprefixer from "autoprefixer"
import autoPreprocess from "svelte-preprocess"
import alias from "@rollup/plugin-alias"
import FullReload from "vite-plugin-full-reload"
import { defineConfig } from "vitest/config"
import { visualizer } from "rollup-plugin-visualizer"
import { svelte } from "@sveltejs/vite-plugin-svelte"
import { Jsonjsdb_watcher, jsonjsdb_add_config } from "jsonjsdb_editor"
const bundle_view = false
const out_dir = "app"
const mermaid_node_path = "node_modules/mermaid/dist/mermaid.min.js"
const mermaid_public_path = "public/assets/external/mermaid.min.js"
const jsonjsdb_config = "public/data/jsonjsdb_config.html"
const app_version = JSON.parse(await fs.readFile("package.json")).version
await Jsonjsdb_watcher.set_db("public/data/db")
await Jsonjsdb_watcher.watch("public/data/db_source")
await Jsonjsdb_watcher.update_preview("preview", "public/data/dataset")
await Jsonjsdb_watcher.update_md_files("md_doc", "public/data/md")
await fs.copyFile(mermaid_node_path, mermaid_public_path)
async function get_aliases(from) {
const jsconfig = JSON.parse(await fs.readFile(from)).compilerOptions
return Object.fromEntries(
Object.entries(jsconfig.paths).map(([find, [replacement]]) => [
find.replace("/*", ""),
path.resolve(replacement.replace("/*", "")),
])
)
}
function html_replace(replacements) {
return {
name: `html_replace`,
transformIndexHtml: {
handler: html => {
for (const replacement of replacements) {
html = html.replaceAll(replacement[0], replacement[1])
}
return html
},
},
}
}
function after_build(callback) {
return { name: "after_build", apply: "build", closeBundle: callback }
}
function update_router_index(file, page_dir_from_router_index) {
return {
name: "update_router_index",
async buildStart() {
let imports = ""
let content = `\nexport default {`
const files = await fs.readdir("src/page")
for (const file of files) {
if (!file.endsWith(".svelte")) continue
const filename = file.replace(".svelte", "")
const module_name = filename.split("[")[0]
const route_name =
module_name.charAt(0).toLowerCase() + module_name.slice(1)
let param = false
if (filename.includes("["))
param = `"${filename.split("[")[1].split("]")[0]}"`
const module_path = `${page_dir_from_router_index}/${filename}.svelte`
imports += `import ${module_name} from "${module_path}"\n`
content += `\n ${route_name}: { component: ${module_name}, param: ${param} },`
}
content += "\n}\n"
await fs.writeFile(file, imports + content, "utf8")
},
}
}
export default defineConfig({
base: "",
server: { port: 8080, origin: "", open: true },
test: { include: ["test/**/*.test.js"] },
build: {
outDir: out_dir,
chunkSizeWarningLimit: 1000,
rollupOptions: {
plugins: [
bundle_view && visualizer({ open: true, filename: "bundle_view.html" }),
],
},
},
plugins: [
FullReload(Jsonjsdb_watcher.get_db_meta_file_path()),
jsonjsdb_add_config(jsonjsdb_config),
update_router_index("src/.generated/router_index.js", "../page"),
alias({ entries: await get_aliases("jsconfig.json") }),
svelte({
preprocess: autoPreprocess({ postcss: { plugins: [autoprefixer] } }),
}),
html_replace([
["{{app_version}}", app_version],
[" crossorigin ", " "],
[` type="module" src="./`, ` defer src="./`],
]),
after_build(async () => {
await fs.copyFile("LICENSE.md", out_dir + "/LICENSE.md")
await fs.copyFile("CHANGELOG.md", out_dir + "/CHANGELOG.md")
}),
],
})