feat: support esm ssr build (#707)

pull/855/head
Divyansh Singh 3 years ago
parent 75dcaa2344
commit c90baae34b

1
.gitignore vendored

@ -10,3 +10,4 @@ dist
node_modules
pnpm-global
TODOs.md
.temp

@ -187,7 +187,6 @@ export function useRouter(): Router {
if (!router) {
throw new Error('useRouter() is called without provider.')
}
// @ts-ignore
return router
}

@ -84,7 +84,7 @@ export async function build(
pageToHashMap
)
} finally {
await fs.remove(siteConfig.tempDir)
fs.rmSync(siteConfig.tempDir, { recursive: true, force: true })
}
console.log(`build complete in ${((Date.now() - start) / 1000).toFixed(2)}s.`)

@ -52,10 +52,13 @@ export async function bundle(
pageToHashMap,
clientJSMap
),
// @ts-ignore
ssr: {
noExternal: ['vitepress']
},
// TODO: remove this workaround
legacy: {
buildSsrCjsExternalHeuristics: true
},
build: {
...options,
emptyOutDir: true,
@ -71,7 +74,11 @@ export async function bundle(
output: {
...rollupOptions?.output,
...(ssr
? {}
? {
entryFileNames: `[name].js`,
chunkFileNames: `[name].[hash].js`,
assetFileNames: `[name].[ext]`
}
: {
chunkFileNames(chunk) {
// avoid ads chunk being intercepted by adblock

@ -20,9 +20,8 @@ export async function renderPage(
pageToHashMap: Record<string, string>,
hashMapString: string
) {
const { createApp } = await import(
pathToFileURL(path.join(config.tempDir, `app.js`)).toString()
)
const entryPath = path.join(config.tempDir, 'app.js')
const { createApp } = await import(pathToFileURL(entryPath).toString())
const { app, router } = createApp()
const routePath = `/${page.replace(/\.md$/, '')}`
const siteData = resolveSiteDataByRoute(config.site, routePath)

@ -216,6 +216,14 @@ export async function createVitePressPlugin(
delete bundle[name]
}
}
if (config.ssr?.format === 'esm') {
this.emitFile({
type: 'asset',
fileName: 'package.json',
source: '{ "private": true, "type": "module" }'
})
}
} else {
// client build:
// for each .md entry chunk, adjust its name to its correct path.

@ -13,7 +13,6 @@ export type {
export const EXTERNAL_URL_RE = /^https?:/i
export const APPEARANCE_KEY = 'vitepress-theme-appearance'
// @ts-ignore
export const inBrowser = typeof window !== 'undefined'
export const notFoundPageData: PageData = {

Loading…
Cancel
Save