Skip to content

Commit

Permalink
refactor(bundler-vite): remove vite-root temp dir and clean up stale …
Browse files Browse the repository at this point in the history
…fix for vue plugin
  • Loading branch information
meteorlxy committed Sep 9, 2024
1 parent 64f0215 commit fcab1ed
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 67 deletions.
47 changes: 28 additions & 19 deletions packages/bundler-vite/src/plugins/vuepressMainPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,24 +107,6 @@ export const vuepressMainPlugin = ({
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
Expand All @@ -147,7 +129,7 @@ import 'vuepress/client-app'
}

return {
root: app.dir.temp('vite-root'),
root: app.dir.source(),
base: app.options.base,
mode: !isBuild || app.env.isDebug ? 'development' : 'production',
define: await resolveDefine({ app, isBuild, isServer }),
Expand Down Expand Up @@ -231,6 +213,33 @@ import 'vuepress/client-app'
],
}) as Connect.NextHandleFunction,
)

// serve the dev template as `/index.html`
server.middlewares.use((req, res, next) => {
if (!req.url?.endsWith('.html')) {
next()
return
}

res.statusCode = 200
res.setHeader('Content-Type', 'text/html')
const indexHtml = fs
.readFileSync(app.options.templateDev)
.toString()
.replace(
/<\/body>/,
`\
<script type="module">
import 'vuepress/client-app'
</script>
</body>`,
)
void server
.transformIndexHtml(req.url, indexHtml, req.originalUrl)
.then((result) => {
res.end(result)
})
})
}
},
})
48 changes: 1 addition & 47 deletions packages/bundler-vite/src/plugins/vuepressVuePlugin.ts
Original file line number Diff line number Diff line change
@@ -1,57 +1,11 @@
import vuePlugin from '@vitejs/plugin-vue'
import type { Plugin } from 'vite'
import type { AssetURLOptions, AssetURLTagConfig } from 'vue/compiler-sfc'
import type { ViteBundlerOptions } from '../types.js'

/**
* Determine if the given `transformAssetUrls` option is `AssetURLTagConfig`
*/
const isAssetURLTagConfig = (
transformAssetUrls: AssetURLOptions | AssetURLTagConfig,
): transformAssetUrls is AssetURLTagConfig =>
Object.values(transformAssetUrls).some((val) => Array.isArray(val))

/**
* Resolve `template.transformAssetUrls` option from user config
*/
const resolveTransformAssetUrls = (
options: ViteBundlerOptions,
): AssetURLOptions => {
// default transformAssetUrls option
const defaultTransformAssetUrls = { includeAbsolute: true }

// user provided transformAssetUrls option
const { transformAssetUrls: userTransformAssetUrls } =
options.vuePluginOptions?.template ?? {}

// if user does not provide an object as transformAssetUrls
if (typeof userTransformAssetUrls !== 'object') {
return defaultTransformAssetUrls
}

// AssetURLTagConfig
if (isAssetURLTagConfig(userTransformAssetUrls)) {
return {
...defaultTransformAssetUrls,
tags: userTransformAssetUrls,
}
}

// AssetURLOptions
return {
...defaultTransformAssetUrls,
...userTransformAssetUrls,
}
}

/**
* Wrapper of official vue plugin
* Wrapper of the official vue plugin
*/
export const vuepressVuePlugin = (options: ViteBundlerOptions): Plugin =>
vuePlugin({
...options.vuePluginOptions,
template: {
...options.vuePluginOptions?.template,
transformAssetUrls: resolveTransformAssetUrls(options),
},
})
2 changes: 1 addition & 1 deletion packages/bundler-vite/src/resolveViteConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ export const resolveViteConfig = ({
charset: 'utf8',
},
plugins: [
vuepressVuePlugin(options),
vuepressMainPlugin({ app, isBuild, isServer }),
vuepressUserConfigPlugin(options),
vuepressVuePlugin(options),
],
},
// some vite options would not take effect inside a plugin, so we still need to merge them here in addition to userConfigPlugin
Expand Down

0 comments on commit fcab1ed

Please sign in to comment.