From 03cc1913a357b11428b7636757a679fed27b9cf0 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Mon, 24 Jan 2022 13:28:47 +0000 Subject: [PATCH] fix(nuxi,nuxt3,bridge): generate all templates with `nuxi prepare` (#2409) --- packages/bridge/src/app.ts | 7 +++++++ packages/bridge/src/nitro.ts | 4 +++- packages/nuxi/src/commands/prepare.ts | 6 +++++- packages/nuxi/src/utils/prepare.ts | 1 - packages/nuxt3/src/core/builder.ts | 6 ++++-- 5 files changed, 19 insertions(+), 5 deletions(-) diff --git a/packages/bridge/src/app.ts b/packages/bridge/src/app.ts index f97c6a5e13..25744700a0 100644 --- a/packages/bridge/src/app.ts +++ b/packages/bridge/src/app.ts @@ -10,6 +10,13 @@ export function setupAppBridge (_options: any) { nuxt.options.alias['#app'] = resolve(distDir, 'runtime/index.mjs') nuxt.options.alias['#build'] = nuxt.options.buildDir + // Mock `bundleBuilder.build` to support `nuxi prepare` + if (nuxt.options._prepare) { + nuxt.hook('builder:prepared', (builder) => { + builder.bundleBuilder.build = () => Promise.resolve(builder.bundleBuilder) + }) + } + // Resolve vue2 builds nuxt.options.alias.vue2 = resolveModule('vue/dist/vue.runtime.esm.js', { paths: nuxt.options.modulesDir }) nuxt.options.build.transpile.push('vue') diff --git a/packages/bridge/src/nitro.ts b/packages/bridge/src/nitro.ts index 325979978d..7e54224a8d 100644 --- a/packages/bridge/src/nitro.ts +++ b/packages/bridge/src/nitro.ts @@ -114,6 +114,7 @@ export function setupNitroBridge () { // Generate mjs resources nuxt.hook('build:compiled', async ({ name }) => { + if (nuxt.options._prepare) { return } if (name === 'server') { const jsServerEntry = resolve(nuxt.options.buildDir, 'dist/server/server.js') await fsp.writeFile(jsServerEntry.replace(/.js$/, '.cjs'), 'module.exports = require("./server.js")', 'utf8') @@ -145,7 +146,7 @@ export function setupNitroBridge () { }) // nuxt prepare - nuxt.hook('builder:generateApp', async () => { + nuxt.hook('build:done', async () => { nitroDevContext.scannedMiddleware = await scanMiddleware(nitroDevContext._nuxt.serverDir) await writeTypes(nitroDevContext) }) @@ -155,6 +156,7 @@ export function setupNitroBridge () { nuxt.options.build._minifyServer = false nuxt.options.build.standalone = false nuxt.hook('build:done', async () => { + if (nuxt.options._prepare) { return } if (nuxt.options.dev) { await build(nitroDevContext) } else if (!nitroContext._nuxt.isStatic) { diff --git a/packages/nuxi/src/commands/prepare.ts b/packages/nuxi/src/commands/prepare.ts index 17d2d75ae2..86ec283757 100644 --- a/packages/nuxi/src/commands/prepare.ts +++ b/packages/nuxi/src/commands/prepare.ts @@ -1,4 +1,6 @@ -import { resolve } from 'pathe' +import { buildNuxt } from '@nuxt/kit' +import { relative, resolve } from 'pathe' +import consola from 'consola' import { clearDir } from '../utils/fs' import { loadKit } from '../utils/kit' import { writeTypes } from '../utils/prepare' @@ -18,6 +20,8 @@ export default defineNuxtCommand({ const nuxt = await loadNuxt({ rootDir, config: { _prepare: true } }) await clearDir(nuxt.options.buildDir) + await buildNuxt(nuxt) await writeTypes(nuxt) + consola.success('Types generated in', relative(process.cwd(), nuxt.options.buildDir)) } }) diff --git a/packages/nuxi/src/utils/prepare.ts b/packages/nuxi/src/utils/prepare.ts index 5145f43887..28c3b0ba38 100644 --- a/packages/nuxi/src/utils/prepare.ts +++ b/packages/nuxi/src/utils/prepare.ts @@ -67,7 +67,6 @@ export const writeTypes = async (nuxt: Nuxt) => { const declarations: string[] = [] - await nuxt.callHook('builder:generateApp') await nuxt.callHook('prepare:types', { references, declarations, tsConfig }) const declaration = [ diff --git a/packages/nuxt3/src/core/builder.ts b/packages/nuxt3/src/core/builder.ts index a6c763bf21..679e343e5f 100644 --- a/packages/nuxt3/src/core/builder.ts +++ b/packages/nuxt3/src/core/builder.ts @@ -20,8 +20,10 @@ export async function build (nuxt: Nuxt) { } await nuxt.callHook('build:before', { nuxt }, nuxt.options.build) - await bundle(nuxt) - await nuxt.callHook('build:done', { nuxt }) + if (!nuxt.options._prepare) { + await bundle(nuxt) + await nuxt.callHook('build:done', { nuxt }) + } if (!nuxt.options.dev) { await nuxt.callHook('close', nuxt)