Skip to content

Commit

Permalink
fix(nuxi,nuxt3,bridge): generate all templates with nuxi prepare (n…
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe authored Jan 24, 2022
1 parent 7553849 commit 03cc191
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 5 deletions.
7 changes: 7 additions & 0 deletions packages/bridge/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
4 changes: 3 additions & 1 deletion packages/bridge/src/nitro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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)
})
Expand All @@ -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) {
Expand Down
6 changes: 5 additions & 1 deletion packages/nuxi/src/commands/prepare.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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))
}
})
1 change: 0 additions & 1 deletion packages/nuxi/src/utils/prepare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down
6 changes: 4 additions & 2 deletions packages/nuxt3/src/core/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 03cc191

Please sign in to comment.