-
Notifications
You must be signed in to change notification settings - Fork 923
/
Copy pathmainPlugin.ts
212 lines (197 loc) · 5.13 KB
/
mainPlugin.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
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import type { App } from '@vuepress/core'
import { fs } from '@vuepress/utils'
import autoprefixer from 'autoprefixer'
import history from 'connect-history-api-fallback'
import type { AliasOptions, Connect, Plugin, UserConfig } from 'vite'
/**
* The main plugin to compat vuepress with vite
*/
export const mainPlugin = ({
app,
isBuild,
isServer,
}: {
app: App
isBuild: boolean
isServer: boolean
}): Plugin => ({
name: 'vuepress:main',
config: async () => {
// create a temp index.html as dev entry point
if (!isBuild) {
await app.writeTemp(
'vite-root/index.html',
fs
.readFileSync(app.options.templateDev)
.toString()
.replace(
/<\/body>/,
`\
<script type="module">
import '@vuepress/client/app'
</script>
</body>`
)
)
}
// 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 = [
'@vuepress/client',
...app.pluginApi.plugins.map(({ name }) => name),
]
return {
root: app.dir.temp('vite-root'),
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 ? [] : [autoprefixer],
},
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,
rollupOptions: {
input: app.dir.client(
fs.readJsonSync(app.dir.client('package.json')).exports['./app']
),
output: {
...(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,
},
}
},
generateBundle(_, bundle) {
// delete all asset outputs in server build
if (isServer) {
Object.keys(bundle).forEach((key) => {
if (bundle[key].type === 'asset') {
delete bundle[key]
}
})
}
},
configureServer(server) {
return () => {
// fallback all `.html` requests to `/index.html`
server.middlewares.use(
history({
rewrites: [
{
from: /\.html$/,
to: '/index.html',
},
],
}) as Connect.NextHandleFunction
)
}
},
})
/**
* 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.entries(aliasObject).forEach(([key, value]) => {
alias[key] = value
})
)
return [
...Object.keys(alias).map((item) => ({
find: item,
replacement: alias[item],
})),
...(isServer
? []
: [
{
find: /^vue$/,
replacement: 'vue/dist/vue.runtime.esm-bundler.js',
},
{
find: /^vue-router$/,
replacement: 'vue-router/dist/vue-router.esm-bundler.js',
},
]),
]
}
/**
* 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_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),
}
// 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
}