From 3117cda0d71b36d2d5234b02c13c1b9da4145997 Mon Sep 17 00:00:00 2001 From: Anthony Fu Date: Fri, 3 Jun 2022 20:54:29 +0800 Subject: [PATCH 1/3] feat: support esm ssr build --- .gitignore | 1 + src/node/build/bundle.ts | 5 ++++- src/node/build/render.ts | 9 ++++++--- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index d73d7b2d1633..802a36270902 100644 --- a/.gitignore +++ b/.gitignore @@ -6,6 +6,7 @@ .idea .vite_opt_cache .vscode +.temp dist node_modules pnpm-global diff --git a/src/node/build/bundle.ts b/src/node/build/bundle.ts index ac8491129878..ae98f54daddd 100644 --- a/src/node/build/bundle.ts +++ b/src/node/build/bundle.ts @@ -54,7 +54,10 @@ export async function bundle( ), // @ts-ignore ssr: { - noExternal: ['vitepress'] + noExternal: [ + 'vitepress', + '@docsearch/css' + ] }, build: { ...options, diff --git a/src/node/build/render.ts b/src/node/build/render.ts index aa5f217e61e8..99b28c7c8dd3 100644 --- a/src/node/build/render.ts +++ b/src/node/build/render.ts @@ -20,9 +20,12 @@ 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.mjs`), + path.join(config.tempDir, `app.js`) + ].find((i) => fs.existsSync(i))! + + const { createApp } = await import(pathToFileURL(entryPath).toString()) const { app, router } = createApp() const routePath = `/${page.replace(/\.md$/, '')}` const siteData = resolveSiteDataByRoute(config.site, routePath) From 52499221874e23ec437c416f4fc5ef3edb771857 Mon Sep 17 00:00:00 2001 From: Anthony Fu Date: Fri, 3 Jun 2022 21:21:32 +0800 Subject: [PATCH 2/3] chore: update --- src/node/plugin.ts | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/node/plugin.ts b/src/node/plugin.ts index af91a3436341..05dc98085647 100644 --- a/src/node/plugin.ts +++ b/src/node/plugin.ts @@ -215,6 +215,17 @@ export async function createVitePressPlugin( delete bundle[name] } } + + // in Vite v3 + // we generate a fake package.json to make the output `.js` as ESM + // @ts-ignore + 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. From e0f30159b3141e48bfd3a4cc277880cdf486fabe Mon Sep 17 00:00:00 2001 From: Anthony Fu Date: Fri, 17 Jun 2022 15:45:18 +0800 Subject: [PATCH 3/3] fix: force use `.js` for SSR --- src/node/build/bundle.ts | 9 ++++++--- src/node/build/render.ts | 6 +----- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/src/node/build/bundle.ts b/src/node/build/bundle.ts index ae98f54daddd..fe3ecebdd7a7 100644 --- a/src/node/build/bundle.ts +++ b/src/node/build/bundle.ts @@ -52,11 +52,10 @@ export async function bundle( pageToHashMap, clientJSMap ), - // @ts-ignore ssr: { noExternal: [ 'vitepress', - '@docsearch/css' + '@docsearch/css', ] }, build: { @@ -74,7 +73,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 57a5bf4521c7..247d7b51ef7c 100644 --- a/src/node/build/render.ts +++ b/src/node/build/render.ts @@ -20,11 +20,7 @@ export async function renderPage( pageToHashMap: Record, hashMapString: string ) { - const entryPath = [ - path.join(config.tempDir, `app.mjs`), - path.join(config.tempDir, `app.js`) - ].find((i) => fs.existsSync(i))! - + const entryPath = path.join(config.tempDir, 'app.js') const { createApp } = await import(pathToFileURL(entryPath).toString()) const { app, router } = createApp() const routePath = `/${page.replace(/\.md$/, '')}`