fix(nuxt3): directly emit `document.template.mjs` (#4360)

This commit is contained in:
Daniel Roe 2022-04-15 08:39:16 +01:00 committed by GitHub
parent 20e09d6203
commit 14d9e8e4ae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 25 deletions

View File

@ -1,5 +1,4 @@
import { existsSync, promises as fsp } from 'fs'
import { dirname } from 'path'
import { existsSync } from 'fs'
import { resolve, join } from 'pathe'
import { createNitro, createDevServer, build, prepare, copyPublicAssets, writeTypes, scanHandlers, prerender } from 'nitropack'
import type { NitroEventHandler, NitroDevEventHandler, NitroConfig } from 'nitropack'
@ -138,7 +137,6 @@ export async function initNitro (nuxt: Nuxt) {
// nuxt build/dev
nuxt.hook('build:done', async () => {
await writeDocumentTemplate(nuxt)
if (nuxt.options.dev) {
await build(nitro)
} else {
@ -192,17 +190,3 @@ async function resolveHandlers (nuxt: Nuxt) {
devHandlers
}
}
async function writeDocumentTemplate (nuxt: Nuxt) {
// Compile html template
const src = resolve(nuxt.options.buildDir, 'views/app.template.html')
const dst = src.replace(/.html$/, '.mjs').replace('app.template.mjs', 'document.template.mjs')
const contents = nuxt.vfs[src] || await fsp.readFile(src, 'utf-8').catch(() => '')
if (contents) {
const compiled = 'export default ' +
// eslint-disable-next-line no-template-curly-in-string
`(params) => \`${contents.replace(/{{ (\w+) }}/g, '${params.$1}')}\``
await fsp.mkdir(dirname(dst), { recursive: true })
await fsp.writeFile(dst, compiled, 'utf8')
}
}

View File

@ -71,20 +71,21 @@ export const serverPluginTemplate = {
}
export const appViewTemplate = {
filename: 'views/app.template.html',
filename: 'views/document.template.mjs',
write: true,
getContents () {
return `<!DOCTYPE html>
<html {{ HTML_ATTRS }}>
return `export default (params) => \`<!DOCTYPE html>
<html \${params.HTML_ATTRS}>
<head {{ HEAD_ATTRS }}>
{{ HEAD }}
<head \${params.HEAD_ATTRS}>
\${params.HEAD}
</head>
<body {{ BODY_ATTRS }}>{{ BODY_PREPEND }}
{{ APP }}
<body \${params.BODY_ATTRS}>\${params.BODY_PREPEND}
\${params.APP}
</body>
</html>
</html>\`
`
}
}