refactor: vite ssr compat

pull/210/head
Evan You 4 years ago
parent 187815cc9a
commit b4bdaf648d

@ -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 {

@ -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]
}
}

@ -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

@ -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(

Loading…
Cancel
Save