-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
/
define.ts
98 lines (86 loc) · 2.56 KB
/
define.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
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
import MagicString from 'magic-string'
import { TransformResult } from 'rollup'
import { ResolvedConfig } from '../config'
import { Plugin } from '../plugin'
import { isCSSRequest } from './css'
export function definePlugin(config: ResolvedConfig): Plugin {
const isBuild = config.command === 'build'
const userDefine: Record<string, string> = {}
for (const key in config.define) {
const val = config.define[key]
userDefine[key] = typeof val === 'string' ? val : JSON.stringify(val)
}
// during dev, import.meta properties are handled by importAnalysis plugin
const importMetaKeys: Record<string, string> = {}
if (isBuild) {
const env: Record<string, any> = {
...config.env,
SSR: !!config.build.ssr
}
for (const key in env) {
importMetaKeys[`import.meta.env.${key}`] = JSON.stringify(env[key])
}
Object.assign(importMetaKeys, {
'import.meta.env.': `({}).`,
'import.meta.env': JSON.stringify(config.env),
'import.meta.hot': `false`
})
}
const replacements: Record<string, string | undefined> = {
'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV || config.mode),
...userDefine,
...importMetaKeys,
'process.env.': `({}).`
}
const pattern = new RegExp(
'\\b(' +
Object.keys(replacements)
.map((str) => {
return str.replace(/[-[\]/{}()*+?.\\^$|]/g, '\\$&')
})
.join('|') +
')\\b',
'g'
)
return {
name: 'vite:define',
transform(code, id, ssr) {
if (!ssr && !isBuild) {
// for dev we inject actual global defines in the vite client to
// avoid the transform cost.
return
}
if (
// exclude css and static assets for performance
isCSSRequest(id) ||
config.assetsInclude(id)
) {
return
}
if (ssr && !isBuild) {
// ssr + dev, simple replace
return code.replace(pattern, (_, match) => {
return '' + replacements[match]
})
}
const s = new MagicString(code)
let hasReplaced = false
let match
while ((match = pattern.exec(code))) {
hasReplaced = true
const start = match.index
const end = start + match[0].length
const replacement = '' + replacements[match[1]]
s.overwrite(start, end, replacement)
}
if (!hasReplaced) {
return null
}
const result: TransformResult = { code: s.toString() }
if (config.build.sourcemap) {
result.map = s.generateMap({ hires: true })
}
return result
}
}
}