-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vite.config.js
118 lines (107 loc) · 2.65 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
113
114
115
116
117
118
import viteConf from "@3-/vite-conf"
import vbD from "@3-/vb/vbD.js"
import read from "@3-/read"
import rJson from "@3-/read/rJson.js"
import write from "@3-/write"
import { readFileSync } from "node:fs"
import utf8d from "@3-/utf8/utf8d.js"
import bintxt from "@3-/bintxt"
import { copyFileSync } from "node:fs"
import { join, dirname } from "node:path"
import merge from "lodash-es/merge"
import Yml from "@3-/yml/Yml.js"
const ROOT = import.meta.dirname
const conf = await viteConf(ROOT)
const env = process.env
const IS_DEV = env.NODE_ENV != "production"
merge(conf, {
define: {
I18N_VER: JSON.stringify(
"@" +
(IS_DEV
? "latest"
: rJson(join(ROOT, "conf/i18n.package.json")).version) +
"/",
),
},
})
const MD = join(dirname(ROOT), "md")
const MD_HTM_CONF = env.MD_HTM_CONF || "dev"
const MD_HTM = Yml(join(MD, ".i18n/htm"))[MD_HTM_CONF]
const MD_V = join(MD, "out", MD_HTM_CONF, "v")
if (IS_DEV) {
merge(conf, {
plugins: [
{
name: "initHtm",
enforce: "pre",
transformIndexHtml: (htm) => {
const head = "<head>"
const [B, P] = read(join(MD_V, ".v"))
.split(">")
.map((ver, pos) => read(join(MD_V, ver, "BP"[pos] + ".js")))
return htm.replace(
head,
`${head}<script>const _V='${MD_HTM.v}',_P=${P};(${B})('//127.0.0.1:7775/x/')()</script>`,
)
},
},
],
})
}
const ignoreImport = (conf, external) => {
// external = external.map((i) => (i.endsWith("@") ? i + "latest/" : i))
// console.log({ external })
const prefix = "//.+"
const alias = {}
external.push("x/")
external.forEach((i) => {
alias[i] = prefix + i
})
merge(conf, {
resolve: { alias },
build: {
rollupOptions: { external },
},
optimizeDeps: {
exclude: external,
},
plugins: [
{
name: "ignoreImport",
enforce: "pre",
// 2. push a plugin to rewrite the 'vite:import-analysis' prefix
configResolved(resolvedConfig) {
resolvedConfig.plugins.push({
name: "replacePrefix",
transform: (code) => {
for (const [k, v] of Object.entries(alias)) {
code = code.replaceAll(v, k)
}
// for (let i of code.split("\n")) {
// if (i.includes("x/")) {
// console.log(i)
// }
// }
return code
},
})
},
// 3. rewrite the id before 'vite:resolve' plugin transform to 'node_modules/...'
resolveId: (id) => {
if (id.startsWith(prefix)) {
if (!IS_DEV) {
id = id.slice(prefix.length)
}
return { id, external: true }
}
},
},
],
})
}
ignoreImport(conf, Object.keys(MD_HTM.importmap))
for (let [k, v] of Object.entries(conf.define)) {
console.log(k, v)
}
export default conf