-
Notifications
You must be signed in to change notification settings - Fork 906
Expand file tree
/
Copy pathvuepressConfigPlugin.ts
More file actions
179 lines (165 loc) · 4.74 KB
/
vuepressConfigPlugin.ts
File metadata and controls
179 lines (165 loc) · 4.74 KB
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import type { App } from '@vuepress/core'
import { fs, sanitizeFileName } from '@vuepress/utils'
import autoprefixer from 'autoprefixer'
import type { AcceptedPlugin } from 'postcss'
import postcssrc from 'postcss-load-config'
import type { AliasOptions, Plugin, UserConfig } from 'vite'
/**
* Resolve vite config `resolve.alias`
*/
const resolveAlias = async ({
app,
isServer,
}: {
app: App
isServer: boolean
}): Promise<AliasOptions> => {
const alias: AliasOptions = {
'@internal': app.dir.temp('internal'),
'@temp': app.dir.temp(),
'@source': app.dir.source(),
}
// plugin hook: alias
const aliasResult = await app.pluginApi.hooks.alias.process(app, isServer)
aliasResult.forEach((aliasObject) => {
Object.assign(alias, aliasObject)
})
return (
Object.keys(alias)
// sort alias by length in descending order to ensure longer alias is handled first
.sort((a, b) => b.length - a.length)
.map((item) => ({
find: item,
replacement: alias[item],
}))
)
}
/**
* Resolve vite config `define`
*/
const resolveDefine = async ({
app,
isBuild,
isServer,
}: {
app: App
isBuild: boolean
isServer: boolean
}): Promise<UserConfig['define']> => {
const define: UserConfig['define'] = {
__VUEPRESS_VERSION__: JSON.stringify(app.version),
__VUEPRESS_BASE__: JSON.stringify(app.options.base),
__VUEPRESS_DEV__: JSON.stringify(!isBuild),
__VUEPRESS_SSR__: JSON.stringify(isServer),
// @see http://link.vuejs.org/feature-flags
// enable options API by default
__VUE_OPTIONS_API__: JSON.stringify(true),
__VUE_PROD_DEVTOOLS__: JSON.stringify(app.env.isDebug),
__VUE_PROD_HYDRATION_MISMATCH_DETAILS__: JSON.stringify(app.env.isDebug),
}
// override vite built-in define config in debug mode
if (app.env.isDebug) {
define['process.env.NODE_ENV'] = JSON.stringify('development')
}
// plugin hook: define
const defineResult = await app.pluginApi.hooks.define.process(app, isServer)
defineResult.forEach((defineObject) => {
Object.entries(defineObject).forEach(([key, value]) => {
define[key] = JSON.stringify(value)
})
})
return define
}
/**
* Resolve and setup vite config
*/
export const vuepressConfigPlugin = ({
app,
isBuild,
isServer,
}: {
app: App
isBuild: boolean
isServer: boolean
}): Plugin => ({
name: 'vuepress:config',
enforce: 'pre',
async config() {
// vuepress related packages that include pure esm client code,
// which should not be optimized in dev mode, and should not be
// externalized in build ssr mode
const clientPackages = [
// although discouraged, but users may still use `@vuepress/client` directly
'@vuepress/client',
'vuepress',
...app.pluginApi.plugins
// the 'user-config' plugin is created by cli internally
.filter(({ name }) => name !== 'user-config')
.map(({ name }) => name),
]
let postcssPlugins: AcceptedPlugin[]
try {
const postcssConfigResult = await postcssrc()
postcssPlugins = postcssConfigResult.plugins
} catch {
postcssPlugins = [autoprefixer]
}
return {
root: app.dir.source(),
base: app.options.base,
mode: !isBuild || app.env.isDebug ? 'development' : 'production',
define: await resolveDefine({ app, isBuild, isServer }),
publicDir: app.dir.public(),
cacheDir: app.dir.cache(),
resolve: {
alias: await resolveAlias({ app, isServer }),
},
css: {
postcss: {
plugins: isServer ? [] : postcssPlugins,
},
preprocessorOptions: {
scss: { charset: false },
},
},
server: {
host: app.options.host,
port: app.options.port,
open: app.options.open,
},
build: {
ssr: isServer,
outDir: isServer ? app.dir.temp('.server') : app.dir.dest(),
emptyOutDir: false,
cssCodeSplit: false,
rolldownOptions: {
input: app.dir.client(
(
fs.readJsonSync(app.dir.client('package.json')) as {
exports: { './app': string }
}
).exports['./app'],
),
output: {
sanitizeFileName,
...(isServer
? {
// also add hash to ssr entry file, so that users could build multiple sites in a single process
entryFileNames: `[name].[hash].mjs`,
}
: {}),
},
preserveEntrySignatures: 'allow-extension',
},
minify: isServer ? false : !app.env.isDebug,
},
optimizeDeps: {
exclude: clientPackages,
},
ssr: {
format: 'esm',
noExternal: clientPackages,
},
}
},
})