From b4bdaf648d4506fc3a75251cdeea41f631f815a3 Mon Sep 17 00:00:00 2001 From: Evan You Date: Sun, 17 Jan 2021 23:49:13 -0500 Subject: [PATCH] refactor: vite ssr compat --- src/node/alias.ts | 1 - src/node/build/bundle.ts | 36 ++++++------------------------------ src/node/config.ts | 2 +- src/node/plugin.ts | 6 ++---- 4 files changed, 9 insertions(+), 36 deletions(-) diff --git a/src/node/alias.ts b/src/node/alias.ts index 15bc8290..bb2314c2 100644 --- a/src/node/alias.ts +++ b/src/node/alias.ts @@ -18,7 +18,6 @@ export const SITE_DATA_ID = '@siteData' export const SITE_DATA_REQUEST_PATH = '/' + SITE_DATA_ID export function resolveAliases( - root: string, themeDir: string, userConfig: UserConfig ): AliasOptions { diff --git a/src/node/build/bundle.ts b/src/node/build/bundle.ts index 9489364c..7f499958 100644 --- a/src/node/build/bundle.ts +++ b/src/node/build/bundle.ts @@ -3,7 +3,7 @@ import path from 'path' import slash from 'slash' import { APP_PATH } from '../alias' import { SiteConfig } from '../config' -import { RollupOutput, ExternalOption } from 'rollup' +import { RollupOutput } from 'rollup' import { build, BuildOptions, UserConfig as ViteUserConfig } from 'vite' import { createVitePressPlugin } from '../plugin' @@ -38,8 +38,12 @@ export async function bundle( root, logLevel: 'warn', plugins: createVitePressPlugin(root, config, ssr, pageToHashMap), + ssr: { + noExternal: ['vitepress'] + }, build: { ...options, + emptyOutDir: true, ssr, base: config.site.base, outDir: ssr ? config.tempDir : config.outDir, @@ -47,20 +51,13 @@ export async function bundle( rollupOptions: { ...rollupOptions, input, - external: ssr - ? resolveExternal(rollupOptions?.external) - : rollupOptions?.external, // important so that each page chunk and the index export things for each // other preserveEntrySignatures: 'allow-extension', output: { ...rollupOptions?.output, ...(ssr - ? { - format: 'cjs', - exports: 'named', - entryFileNames: '[name].js' - } + ? {} : { chunkFileNames(chunk): string { if (!chunk.isEntry && /runtime/.test(chunk.name)) { @@ -97,24 +94,3 @@ export async function bundle( return [clientResult, serverResult, pageToHashMap] } - -function resolveExternal( - userExternal: ExternalOption | undefined -): ExternalOption { - const required = ['vue', /^@vue\//] - if (!userExternal) { - return required - } - if (Array.isArray(userExternal)) { - return [...required, ...userExternal] - } else if (typeof userExternal === 'function') { - return (src, importer, isResolved) => { - if (src === 'vue' || /^@vue\//.test(src)) { - return true - } - return userExternal(src, importer, isResolved) - } - } else { - return [...required, userExternal] - } -} diff --git a/src/node/config.ts b/src/node/config.ts index f5625643..d5af0e59 100644 --- a/src/node/config.ts +++ b/src/node/config.ts @@ -59,7 +59,7 @@ export async function resolveConfig( outDir: resolve(root, 'dist'), tempDir: path.resolve(APP_PATH, 'temp'), markdown: userConfig.markdown, - alias: resolveAliases(root, themeDir, userConfig) + alias: resolveAliases(themeDir, userConfig) } return config diff --git a/src/node/plugin.ts b/src/node/plugin.ts index 3dcd70f8..1cebd379 100644 --- a/src/node/plugin.ts +++ b/src/node/plugin.ts @@ -31,8 +31,7 @@ export function createVitePressPlugin( const markdownToVue = createMarkdownToVueRenderFn(root, markdown) const vuePlugin = createVuePlugin({ - include: [/\.vue$/, /\.md$/], - ssr + include: [/\.vue$/, /\.md$/] }) let siteData = site @@ -73,8 +72,7 @@ export function createVitePressPlugin( configureServer(server) { // serve our index.html after vite history fallback return () => { - // @ts-ignore - server.app.use((req, res, next) => { + server.middlewares.use((req, res, next) => { if (req.url!.endsWith('.html')) { res.statusCode = 200 res.end(