Skip to content

Commit fcab1ed

Browse files
committed
refactor(bundler-vite): remove vite-root temp dir and clean up stale fix for vue plugin
1 parent 64f0215 commit fcab1ed

File tree

3 files changed

+30
-67
lines changed

3 files changed

+30
-67
lines changed

packages/bundler-vite/src/plugins/vuepressMainPlugin.ts

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -107,24 +107,6 @@ export const vuepressMainPlugin = ({
107107
name: 'vuepress:main',
108108

109109
config: async () => {
110-
// create a temp index.html as dev entry point
111-
if (!isBuild) {
112-
await app.writeTemp(
113-
'vite-root/index.html',
114-
fs
115-
.readFileSync(app.options.templateDev)
116-
.toString()
117-
.replace(
118-
/<\/body>/,
119-
`\
120-
<script type="module">
121-
import 'vuepress/client-app'
122-
</script>
123-
</body>`,
124-
),
125-
)
126-
}
127-
128110
// vuepress related packages that include pure esm client code,
129111
// which should not be optimized in dev mode, and should not be
130112
// externalized in build ssr mode
@@ -147,7 +129,7 @@ import 'vuepress/client-app'
147129
}
148130

149131
return {
150-
root: app.dir.temp('vite-root'),
132+
root: app.dir.source(),
151133
base: app.options.base,
152134
mode: !isBuild || app.env.isDebug ? 'development' : 'production',
153135
define: await resolveDefine({ app, isBuild, isServer }),
@@ -231,6 +213,33 @@ import 'vuepress/client-app'
231213
],
232214
}) as Connect.NextHandleFunction,
233215
)
216+
217+
// serve the dev template as `/index.html`
218+
server.middlewares.use((req, res, next) => {
219+
if (!req.url?.endsWith('.html')) {
220+
next()
221+
return
222+
}
223+
224+
res.statusCode = 200
225+
res.setHeader('Content-Type', 'text/html')
226+
const indexHtml = fs
227+
.readFileSync(app.options.templateDev)
228+
.toString()
229+
.replace(
230+
/<\/body>/,
231+
`\
232+
<script type="module">
233+
import 'vuepress/client-app'
234+
</script>
235+
</body>`,
236+
)
237+
void server
238+
.transformIndexHtml(req.url, indexHtml, req.originalUrl)
239+
.then((result) => {
240+
res.end(result)
241+
})
242+
})
234243
}
235244
},
236245
})
Lines changed: 1 addition & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,11 @@
11
import vuePlugin from '@vitejs/plugin-vue'
22
import type { Plugin } from 'vite'
3-
import type { AssetURLOptions, AssetURLTagConfig } from 'vue/compiler-sfc'
43
import type { ViteBundlerOptions } from '../types.js'
54

65
/**
7-
* Determine if the given `transformAssetUrls` option is `AssetURLTagConfig`
8-
*/
9-
const isAssetURLTagConfig = (
10-
transformAssetUrls: AssetURLOptions | AssetURLTagConfig,
11-
): transformAssetUrls is AssetURLTagConfig =>
12-
Object.values(transformAssetUrls).some((val) => Array.isArray(val))
13-
14-
/**
15-
* Resolve `template.transformAssetUrls` option from user config
16-
*/
17-
const resolveTransformAssetUrls = (
18-
options: ViteBundlerOptions,
19-
): AssetURLOptions => {
20-
// default transformAssetUrls option
21-
const defaultTransformAssetUrls = { includeAbsolute: true }
22-
23-
// user provided transformAssetUrls option
24-
const { transformAssetUrls: userTransformAssetUrls } =
25-
options.vuePluginOptions?.template ?? {}
26-
27-
// if user does not provide an object as transformAssetUrls
28-
if (typeof userTransformAssetUrls !== 'object') {
29-
return defaultTransformAssetUrls
30-
}
31-
32-
// AssetURLTagConfig
33-
if (isAssetURLTagConfig(userTransformAssetUrls)) {
34-
return {
35-
...defaultTransformAssetUrls,
36-
tags: userTransformAssetUrls,
37-
}
38-
}
39-
40-
// AssetURLOptions
41-
return {
42-
...defaultTransformAssetUrls,
43-
...userTransformAssetUrls,
44-
}
45-
}
46-
47-
/**
48-
* Wrapper of official vue plugin
6+
* Wrapper of the official vue plugin
497
*/
508
export const vuepressVuePlugin = (options: ViteBundlerOptions): Plugin =>
519
vuePlugin({
5210
...options.vuePluginOptions,
53-
template: {
54-
...options.vuePluginOptions?.template,
55-
transformAssetUrls: resolveTransformAssetUrls(options),
56-
},
5711
})

packages/bundler-vite/src/resolveViteConfig.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ export const resolveViteConfig = ({
2828
charset: 'utf8',
2929
},
3030
plugins: [
31-
vuepressVuePlugin(options),
3231
vuepressMainPlugin({ app, isBuild, isServer }),
3332
vuepressUserConfigPlugin(options),
33+
vuepressVuePlugin(options),
3434
],
3535
},
3636
// some vite options would not take effect inside a plugin, so we still need to merge them here in addition to userConfigPlugin

0 commit comments

Comments
 (0)