Skip to content

Commit

Permalink
fix(dev): add polyfill for writeTypes (#158)
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 authored Sep 8, 2023
1 parent 4dee2dc commit 7e488c9
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 15 deletions.
18 changes: 5 additions & 13 deletions src/commands/build.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import { relative, resolve } from 'pathe'
import { consola } from 'consola'
import type { Nitro } from 'nitropack'
// we are deliberately inlining this code as a backup in case user has `@nuxt/schema<3.7`
import { writeTypes as writeTypesLegacy } from '@nuxt/kit'

import { loadKit } from '../utils/kit'
import { clearBuildDir } from '../utils/fs'
import { overrideEnv } from '../utils/env'
Expand Down Expand Up @@ -35,14 +32,9 @@ export default defineCommand({

showVersions(cwd)

const {
loadNuxt,
buildNuxt,
useNitro,
writeTypes = writeTypesLegacy,
} = await loadKit(cwd)
const kit = await loadKit(cwd)

const nuxt = await loadNuxt({
const nuxt = await kit.loadNuxt({
rootDir: cwd,
dotenv: {
cwd,
Expand All @@ -61,21 +53,21 @@ export default defineCommand({
// In Bridge, if nitro is not enabled, useNitro will throw an error
try {
// Use ? for backward compatibility for Nuxt <= RC.10
nitro = useNitro?.()
nitro = kit.useNitro?.()
} catch {
//
}

await clearBuildDir(nuxt.options.buildDir)

await writeTypes(nuxt)
await kit.writeTypes(nuxt)

nuxt.hook('build:error', (err) => {
consola.error('Nuxt Build Error:', err)
process.exit(1)
})

await buildNuxt(nuxt)
await kit.buildNuxt(nuxt)

if (ctx.args.prerender) {
if (!nuxt.options.ssr) {
Expand Down
12 changes: 10 additions & 2 deletions src/utils/kit.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { importModule, tryResolveModule } from './esm'

// we are deliberately inlining this code as a backup in case user has `@nuxt/schema<3.7`
import { writeTypes as writeTypesLegacy } from '@nuxt/kit'

export const loadKit = async (
rootDir: string,
): Promise<typeof import('@nuxt/kit')> => {
Expand All @@ -8,10 +11,15 @@ export const loadKit = async (
const localKit = await tryResolveModule('@nuxt/kit', rootDir)
// Otherwise, we resolve Nuxt _first_ as it is Nuxt's kit dependency that will be used
const rootURL = localKit ? rootDir : (await tryResolveNuxt()) || rootDir
return (await importModule(
let kit: typeof import('@nuxt/kit') = await importModule(
'@nuxt/kit',
rootURL,
)) as typeof import('@nuxt/kit')
)
if (!kit.writeTypes) {
// Polyfills for schema < 3.7
kit = { ...kit, writeTypes: writeTypesLegacy }
}
return kit
} catch (e: any) {
if (e.toString().includes("Cannot find module '@nuxt/kit'")) {
throw new Error(
Expand Down

0 comments on commit 7e488c9

Please sign in to comment.