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 { existsSync } from 'fs'
import { dirname } from 'path'
import { resolve, join } from 'pathe' import { resolve, join } from 'pathe'
import { createNitro, createDevServer, build, prepare, copyPublicAssets, writeTypes, scanHandlers, prerender } from 'nitropack' import { createNitro, createDevServer, build, prepare, copyPublicAssets, writeTypes, scanHandlers, prerender } from 'nitropack'
import type { NitroEventHandler, NitroDevEventHandler, NitroConfig } from 'nitropack' import type { NitroEventHandler, NitroDevEventHandler, NitroConfig } from 'nitropack'
@ -138,7 +137,6 @@ export async function initNitro (nuxt: Nuxt) {
// nuxt build/dev // nuxt build/dev
nuxt.hook('build:done', async () => { nuxt.hook('build:done', async () => {
await writeDocumentTemplate(nuxt)
if (nuxt.options.dev) { if (nuxt.options.dev) {
await build(nitro) await build(nitro)
} else { } else {
@ -192,17 +190,3 @@ async function resolveHandlers (nuxt: Nuxt) {
devHandlers 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 = { export const appViewTemplate = {
filename: 'views/app.template.html', filename: 'views/document.template.mjs',
write: true,
getContents () { getContents () {
return `<!DOCTYPE html> return `export default (params) => \`<!DOCTYPE html>
<html {{ HTML_ATTRS }}> <html \${params.HTML_ATTRS}>
<head {{ HEAD_ATTRS }}> <head \${params.HEAD_ATTRS}>
{{ HEAD }} \${params.HEAD}
</head> </head>
<body {{ BODY_ATTRS }}>{{ BODY_PREPEND }} <body \${params.BODY_ATTRS}>\${params.BODY_PREPEND}
{{ APP }} \${params.APP}
</body> </body>
</html> </html>\`
` `
} }
} }