feat: support esm ssr build (#707)

pull/855/head
Divyansh Singh 3 years ago
parent 75dcaa2344
commit c90baae34b

1
.gitignore vendored

@ -10,3 +10,4 @@ dist
node_modules node_modules
pnpm-global pnpm-global
TODOs.md TODOs.md
.temp

@ -187,7 +187,6 @@ export function useRouter(): Router {
if (!router) { if (!router) {
throw new Error('useRouter() is called without provider.') throw new Error('useRouter() is called without provider.')
} }
// @ts-ignore
return router return router
} }

@ -84,7 +84,7 @@ export async function build(
pageToHashMap pageToHashMap
) )
} finally { } 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.`) console.log(`build complete in ${((Date.now() - start) / 1000).toFixed(2)}s.`)

@ -52,10 +52,13 @@ export async function bundle(
pageToHashMap, pageToHashMap,
clientJSMap clientJSMap
), ),
// @ts-ignore
ssr: { ssr: {
noExternal: ['vitepress'] noExternal: ['vitepress']
}, },
// TODO: remove this workaround
legacy: {
buildSsrCjsExternalHeuristics: true
},
build: { build: {
...options, ...options,
emptyOutDir: true, emptyOutDir: true,
@ -71,7 +74,11 @@ export async function bundle(
output: { output: {
...rollupOptions?.output, ...rollupOptions?.output,
...(ssr ...(ssr
? {} ? {
entryFileNames: `[name].js`,
chunkFileNames: `[name].[hash].js`,
assetFileNames: `[name].[ext]`
}
: { : {
chunkFileNames(chunk) { chunkFileNames(chunk) {
// avoid ads chunk being intercepted by adblock // avoid ads chunk being intercepted by adblock

@ -20,9 +20,8 @@ export async function renderPage(
pageToHashMap: Record<string, string>, pageToHashMap: Record<string, string>,
hashMapString: string hashMapString: string
) { ) {
const { createApp } = await import( const entryPath = path.join(config.tempDir, 'app.js')
pathToFileURL(path.join(config.tempDir, `app.js`)).toString() const { createApp } = await import(pathToFileURL(entryPath).toString())
)
const { app, router } = createApp() const { app, router } = createApp()
const routePath = `/${page.replace(/\.md$/, '')}` const routePath = `/${page.replace(/\.md$/, '')}`
const siteData = resolveSiteDataByRoute(config.site, routePath) const siteData = resolveSiteDataByRoute(config.site, routePath)

@ -216,6 +216,14 @@ export async function createVitePressPlugin(
delete bundle[name] delete bundle[name]
} }
} }
if (config.ssr?.format === 'esm') {
this.emitFile({
type: 'asset',
fileName: 'package.json',
source: '{ "private": true, "type": "module" }'
})
}
} else { } else {
// client build: // client build:
// for each .md entry chunk, adjust its name to its correct path. // for each .md entry chunk, adjust its name to its correct path.

@ -13,7 +13,6 @@ export type {
export const EXTERNAL_URL_RE = /^https?:/i export const EXTERNAL_URL_RE = /^https?:/i
export const APPEARANCE_KEY = 'vitepress-theme-appearance' export const APPEARANCE_KEY = 'vitepress-theme-appearance'
// @ts-ignore
export const inBrowser = typeof window !== 'undefined' export const inBrowser = typeof window !== 'undefined'
export const notFoundPageData: PageData = { export const notFoundPageData: PageData = {

Loading…
Cancel
Save