From 14d9e8e4ae16c07a813b0fd73b4b1c1bff1542ad Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Fri, 15 Apr 2022 08:39:16 +0100 Subject: [PATCH] fix(nuxt3): directly emit `document.template.mjs` (#4360) --- packages/nuxt3/src/core/nitro.ts | 18 +----------------- packages/nuxt3/src/core/templates.ts | 17 +++++++++-------- 2 files changed, 10 insertions(+), 25 deletions(-) diff --git a/packages/nuxt3/src/core/nitro.ts b/packages/nuxt3/src/core/nitro.ts index cb69bbd27c..12693042ee 100644 --- a/packages/nuxt3/src/core/nitro.ts +++ b/packages/nuxt3/src/core/nitro.ts @@ -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') - } -} diff --git a/packages/nuxt3/src/core/templates.ts b/packages/nuxt3/src/core/templates.ts index d80fe18faa..2f536df8a7 100644 --- a/packages/nuxt3/src/core/templates.ts +++ b/packages/nuxt3/src/core/templates.ts @@ -71,20 +71,21 @@ export const serverPluginTemplate = { } export const appViewTemplate = { - filename: 'views/app.template.html', + filename: 'views/document.template.mjs', + write: true, getContents () { - return ` - + return `export default (params) => \` + - - {{ HEAD }} + + \${params.HEAD} -{{ BODY_PREPEND }} - {{ APP }} +\${params.BODY_PREPEND} + \${params.APP} - +\` ` } }