Skip to content

Commit

Permalink
refactor(bundler-vite): split vite plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
meteorlxy committed Sep 10, 2024
1 parent 7dcdadc commit 8a90ce9
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 69 deletions.
4 changes: 3 additions & 1 deletion packages/bundler-vite/src/plugins/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export * from './vuepressMainPlugin.js'
export * from './vuepressBuildPlugin.js'
export * from './vuepressConfigPlugin.js'
export * from './vuepressDevPlugin.js'
export * from './vuepressUserConfigPlugin.js'
export * from './vuepressVuePlugin.js'
23 changes: 23 additions & 0 deletions packages/bundler-vite/src/plugins/vuepressBuildPlugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import type { Plugin } from 'vite'

/**
* Configure build command for vuepress
*/
export const vuepressBuildPlugin = ({
isServer,
}: {
isServer: boolean
}): Plugin => ({
name: 'vuepress:build',

generateBundle(_, bundle) {
// delete all asset outputs in server build
if (isServer) {
Object.keys(bundle).forEach((key) => {
if (bundle[key].type === 'asset') {
delete bundle[key]
}
})
}
},
})
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import type { App } from '@vuepress/core'
import { fs, sanitizeFileName } from '@vuepress/utils'
import autoprefixer from 'autoprefixer'
import history from 'connect-history-api-fallback'
import type { AcceptedPlugin } from 'postcss'
import postcssrc from 'postcss-load-config'
import type { AliasOptions, Connect, Plugin, UserConfig } from 'vite'
import type { AliasOptions, Plugin, UserConfig } from 'vite'

/**
* Resolve vite config `resolve.alias`
Expand Down Expand Up @@ -93,9 +92,9 @@ const resolveDefine = async ({
}

/**
* The main plugin to compat vuepress with vite
* Resolve and setup vite config
*/
export const vuepressMainPlugin = ({
export const vuepressConfigPlugin = ({
app,
isBuild,
isServer,
Expand All @@ -104,9 +103,11 @@ export const vuepressMainPlugin = ({
isBuild: boolean
isServer: boolean
}): Plugin => ({
name: 'vuepress:main',
name: 'vuepress:config',

config: async () => {
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
Expand Down Expand Up @@ -188,58 +189,4 @@ export const vuepressMainPlugin = ({
},
}
},

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,
)

// 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)
})
})
}
},
})
57 changes: 57 additions & 0 deletions packages/bundler-vite/src/plugins/vuepressDevPlugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import type { App } from '@vuepress/core'
import { fs } from '@vuepress/utils'
import history from 'connect-history-api-fallback'
import type { Connect, Plugin } from 'vite'

/**
* Configure dev command for vuepress
*/
export const vuepressDevPlugin = ({ app }: { app: App }): Plugin => ({
name: 'vuepress:dev',

async configureServer(server) {
// inject vuepress client app script into dev template
const templateDevContent = await fs.readFile(app.options.templateDev, {
encoding: 'utf-8',
})
const indexHtml = templateDevContent.replace(
/<\/body>/,
`\
<script type="module">
import 'vuepress/client-app'
</script>
</body>`,
)

return () => {
server.middlewares
// fallback all `.html` requests to `/index.html`
.use(
history({
rewrites: [
{
from: /\.html$/,
to: '/index.html',
},
],
}) as Connect.NextHandleFunction,
)
// serve virtual `/index.html`
.use((req, res, next) => {
if (!req.url?.endsWith('.html')) {
next()
return
}

res.statusCode = 200
res.setHeader('Content-Type', 'text/html')

void server
.transformIndexHtml(req.url, indexHtml, req.originalUrl)
.then((result) => {
res.end(result)
})
})
}
},
})
11 changes: 8 additions & 3 deletions packages/bundler-vite/src/plugins/vuepressUserConfigPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,14 @@ import type { ViteBundlerOptions } from '../types.js'
/**
* A plugin to allow user config to override vite config
*/
export const vuepressUserConfigPlugin = (
options: ViteBundlerOptions,
): Plugin => ({
export const vuepressUserConfigPlugin = ({
options,
}: {
options: ViteBundlerOptions
}): Plugin => ({
name: 'vuepress:user-config',

enforce: 'post',

config: () => options.viteOptions ?? {},
})
6 changes: 5 additions & 1 deletion packages/bundler-vite/src/plugins/vuepressVuePlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ import type { ViteBundlerOptions } from '../types.js'
/**
* Wrapper of the official vue plugin
*/
export const vuepressVuePlugin = (options: ViteBundlerOptions): Plugin =>
export const vuepressVuePlugin = ({
options,
}: {
options: ViteBundlerOptions
}): Plugin =>
vuePlugin({
...options.vuePluginOptions,
})
12 changes: 8 additions & 4 deletions packages/bundler-vite/src/resolveViteConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import type { App } from '@vuepress/core'
import type { InlineConfig } from 'vite'
import { mergeConfig } from 'vite'
import {
vuepressMainPlugin,
vuepressBuildPlugin,
vuepressConfigPlugin,
vuepressDevPlugin,
vuepressUserConfigPlugin,
vuepressVuePlugin,
} from './plugins/index.js'
Expand All @@ -28,9 +30,11 @@ export const resolveViteConfig = ({
charset: 'utf8',
},
plugins: [
vuepressMainPlugin({ app, isBuild, isServer }),
vuepressVuePlugin(options),
vuepressUserConfigPlugin(options),
vuepressConfigPlugin({ app, isBuild, isServer }),
vuepressDevPlugin({ app }),
vuepressBuildPlugin({ isServer }),
vuepressVuePlugin({ options }),
vuepressUserConfigPlugin({ 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 8a90ce9

Please sign in to comment.