Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support esm ssr build #707

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -6,6 +6,7 @@
.idea
.vite_opt_cache
.vscode
.temp
antfu marked this conversation as resolved.
Show resolved Hide resolved
dist
node_modules
pnpm-global
Expand Down
12 changes: 9 additions & 3 deletions src/node/build/bundle.ts
Expand Up @@ -52,9 +52,11 @@ export async function bundle(
pageToHashMap,
clientJSMap
),
// @ts-ignore
ssr: {
noExternal: ['vitepress']
noExternal: [
'vitepress',
'@docsearch/css',
]
},
build: {
...options,
Expand All @@ -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
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
11 changes: 11 additions & 0 deletions src/node/plugin.ts
Expand Up @@ -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') {
brc-dd marked this conversation as resolved.
Show resolved Hide resolved
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