refactor(nitro): app template 2-3 normalization (#177)

This commit is contained in:
Daniel Roe 2021-06-03 14:12:05 +01:00 committed by GitHub
parent f91f822ed4
commit 1bbd754f29
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 19 additions and 9 deletions

View File

@ -5,9 +5,9 @@
</head>
<body {{ BODY_ATTRS }}>
{{ BODY_SCRIPTS_PREPEND }}
<div id="__nuxt">{{ APP }}</div>
{{ APP }}
{{ BODY_SCRIPTS }}
<% if (nuxt.options.vite && nuxt.options.dev) { %><script type="module" src="/@vite/client"></script>
<script type="module" src="/entry.mjs"></script><% } %>
{{ BODY_SCRIPTS }}
</body>
</html>

View File

@ -57,6 +57,9 @@ export interface NuxtHooks {
// 'watch:fileChanged': (builder: Builder, fileName: string) => HookResult
'builder:watch': (event: WatchEvent, path: string) => HookResult
// @nuxt/nitro
'nitro:template': (template: { src: string, contents: string }) => HookResult
// @nuxt/cli
'cli:buildError': (error: unknown) => HookResult
'generate:cache:ignore': (ignore: string[]) => HookResult

View File

@ -49,8 +49,8 @@ export async function build (nitroContext: NitroContext) {
const htmlTemplate = { src: htmlSrc, contents: '', dst: '', compiled: '' }
htmlTemplate.dst = htmlTemplate.src.replace(/.html$/, '.js').replace('app.', 'document.')
htmlTemplate.contents = await readFile(htmlTemplate.src, 'utf-8')
htmlTemplate.compiled = 'module.exports = ' + serializeTemplate(htmlTemplate.contents)
await nitroContext._internal.hooks.callHook('nitro:template:document', htmlTemplate)
htmlTemplate.compiled = 'module.exports = ' + serializeTemplate(htmlTemplate.contents)
await writeFile(htmlTemplate.dst, htmlTemplate.compiled)
nitroContext.rollupConfig = getRollupConfig(nitroContext)

View File

@ -6,7 +6,6 @@ import { getNitroContext, NitroContext } from './context'
import { createDevServer } from './server/dev'
import { wpfs } from './utils/wpfs'
import { resolveMiddleware } from './server/middleware'
import { serializeTemplate } from './utils'
export default function nuxt2CompatModule () {
const { nuxt } = this
@ -34,6 +33,8 @@ export default function nuxt2CompatModule () {
nitroDevContext._internal.hooks.hook('renderLoading',
(req, res) => nuxt.callHook('server:nuxt:renderLoading', req, res))
nitroContext._internal.hooks.hook('nitro:template', template => nuxt.callHook('nitro:template', template))
// Expose process.env.NITRO_PRESET
nuxt.options.env.NITRO_PRESET = nitroContext.preset
@ -57,7 +58,6 @@ export default function nuxt2CompatModule () {
if (!htmlTemplate.contents.includes('BODY_SCRIPTS_PREPEND')) {
const fullTemplate = ['{{ BODY_SCRIPTS_PREPEND }}', '{{ APP }}', '{{ BODY_SCRIPTS }}'].join('\n ')
htmlTemplate.contents = htmlTemplate.contents.replace('{{ APP }}', fullTemplate)
htmlTemplate.compiled = 'module.exports = ' + serializeTemplate(htmlTemplate.contents)
}
})

View File

@ -103,9 +103,10 @@ function renderHTML (payload, rendered, ssrContext) {
meta.htmlAttrs += _meta.htmlAttrs.text()
meta.headAttrs += _meta.headAttrs.text()
meta.headTags +=
_meta.title.text() + _meta.meta.text() +
_meta.link.text() + _meta.style.text() +
_meta.script.text() + _meta.noscript.text()
_meta.title.text() + _meta.base.text() +
_meta.meta.text() + _meta.link.text() +
_meta.style.text() + _meta.script.text() +
_meta.noscript.text()
meta.bodyAttrs += _meta.bodyAttrs.text()
meta.bodyScriptsPrepend =
_meta.meta.text({ pbody: true }) + _meta.link.text({ pbody: true }) +

View File

@ -1,2 +1,6 @@
// @ts-ignore
export { renderToString } from '@vue/server-renderer'
import { renderToString as render } from '@vue/server-renderer'
export const renderToString: typeof render = (...args) => {
return render(...args).then(result => `<div id="__nuxt">${result}</div>`)
}

View File

@ -21,6 +21,8 @@ export function initNitro (nuxt: Nuxt) {
// Expose process.env.NITRO_PRESET
nuxt.options.env.NITRO_PRESET = nitroContext.preset
nitroContext._internal.hooks.hook('nitro:template', template => nuxt.callHook('nitro:template', template))
// Wait for all modules to be ready
nuxt.hook('modules:done', async () => {
// Extend nitro with modules