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..fe3ecebdd7a7 100644 --- a/src/node/build/bundle.ts +++ b/src/node/build/bundle.ts @@ -52,9 +52,11 @@ export async function bundle( pageToHashMap, clientJSMap ), - // @ts-ignore ssr: { - noExternal: ['vitepress'] + noExternal: [ + 'vitepress', + '@docsearch/css', + ] }, build: { ...options, @@ -71,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 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..3d8325cfb38a 100644 --- a/src/node/plugin.ts +++ b/src/node/plugin.ts @@ -216,6 +216,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.