From c90baae34b4106824a070fc1a34b035e4f92be64 Mon Sep 17 00:00:00 2001 From: Divyansh Singh <40380293+brc-dd@users.noreply.github.com> Date: Thu, 23 Jun 2022 17:19:54 +0530 Subject: [PATCH] feat: support esm ssr build (#707) --- .gitignore | 1 + src/client/app/router.ts | 1 - src/node/build/build.ts | 2 +- src/node/build/bundle.ts | 11 +++++++++-- src/node/build/render.ts | 5 ++--- src/node/plugin.ts | 8 ++++++++ src/shared/shared.ts | 1 - 7 files changed, 21 insertions(+), 8 deletions(-) diff --git a/.gitignore b/.gitignore index d73d7b2d1633..d31abfd94ea0 100644 --- a/.gitignore +++ b/.gitignore @@ -10,3 +10,4 @@ dist node_modules pnpm-global TODOs.md +.temp diff --git a/src/client/app/router.ts b/src/client/app/router.ts index 394185595ed8..a36c94745c42 100644 --- a/src/client/app/router.ts +++ b/src/client/app/router.ts @@ -187,7 +187,6 @@ export function useRouter(): Router { if (!router) { throw new Error('useRouter() is called without provider.') } - // @ts-ignore return router } diff --git a/src/node/build/build.ts b/src/node/build/build.ts index d8c34f3f7259..1a318a185c4b 100644 --- a/src/node/build/build.ts +++ b/src/node/build/build.ts @@ -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.`) diff --git a/src/node/build/bundle.ts b/src/node/build/bundle.ts index ac8491129878..bdb69a13deb2 100644 --- a/src/node/build/bundle.ts +++ b/src/node/build/bundle.ts @@ -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 diff --git a/src/node/build/render.ts b/src/node/build/render.ts index c4330e3e63aa..247d7b51ef7c 100644 --- a/src/node/build/render.ts +++ b/src/node/build/render.ts @@ -20,9 +20,8 @@ export async function renderPage( pageToHashMap: Record, 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) diff --git a/src/node/plugin.ts b/src/node/plugin.ts index e3f141313ef9..990a61dd2ca7 100644 --- a/src/node/plugin.ts +++ b/src/node/plugin.ts @@ -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. diff --git a/src/shared/shared.ts b/src/shared/shared.ts index 7db6114e07a8..a47b033ff25c 100644 --- a/src/shared/shared.ts +++ b/src/shared/shared.ts @@ -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 = {