Skip to content

Commit

Permalink
feat: support esm ssr build (#707)
Browse files Browse the repository at this point in the history
  • Loading branch information
brc-dd committed Jun 23, 2022
1 parent 75dcaa2 commit c90baae
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 8 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -10,3 +10,4 @@ dist
node_modules
pnpm-global
TODOs.md
.temp
1 change: 0 additions & 1 deletion src/client/app/router.ts
Expand Up @@ -187,7 +187,6 @@ export function useRouter(): Router {
if (!router) {
throw new Error('useRouter() is called without provider.')
}
// @ts-ignore
return router
}

Expand Down
2 changes: 1 addition & 1 deletion src/node/build/build.ts
Expand Up @@ -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.`)
Expand Down
11 changes: 9 additions & 2 deletions src/node/build/bundle.ts
Expand Up @@ -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,
Expand All @@ -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
Expand Down
5 changes: 2 additions & 3 deletions src/node/build/render.ts
Expand Up @@ -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)
Expand Down
8 changes: 8 additions & 0 deletions src/node/plugin.ts
Expand Up @@ -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.
Expand Down
1 change: 0 additions & 1 deletion src/shared/shared.ts
Expand Up @@ -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 = {
Expand Down

0 comments on commit c90baae

Please sign in to comment.