-
Notifications
You must be signed in to change notification settings - Fork 109
perf(nuxi): skip loading Nuxt in preview when default output dir exists #1274
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -37,37 +37,49 @@ const command = defineCommand({ | |
|
|
||
| const cwd = resolve(ctx.args.cwd || ctx.args.rootDir) | ||
|
|
||
| const { loadNuxt } = await loadKit(cwd) | ||
| const defaultOutput = resolve(cwd, '.output', 'nitro.json') | ||
|
|
||
| const resolvedOutputDir = await new Promise<string>((res) => { | ||
| loadNuxt({ | ||
| cwd, | ||
| dotenv: { | ||
| cwd, | ||
| fileName: ctx.args.dotenv, | ||
| }, | ||
| envName: ctx.args.envName, // c12 will fall back to NODE_ENV | ||
| ready: true, | ||
| overrides: { | ||
| ...(ctx.args.extends && { extends: ctx.args.extends }), | ||
| modules: [ | ||
| function (_, nuxt) { | ||
| nuxt.hook('nitro:init', (nitro) => { | ||
| res(resolve(nuxt.options.srcDir || cwd, nitro.options.output.dir || '.output', 'nitro.json')) | ||
| }) | ||
| }, | ||
| ], | ||
| }, | ||
| }).then(nuxt => nuxt.close()).catch(() => '') | ||
| }) | ||
| let nitroJSONPath: string | undefined | ||
|
|
||
| const defaultOutput = resolve(cwd, '.output', 'nitro.json') // for backwards compatibility | ||
| // Try the default output path first to avoid loading Nuxt (which runs | ||
| // module setup and may emit warnings about missing env vars that are | ||
| // already baked into the build output). | ||
| if (existsSync(defaultOutput)) { | ||
| nitroJSONPath = defaultOutput | ||
| } | ||
| else { | ||
| const { loadNuxt } = await loadKit(cwd) | ||
|
|
||
| const resolvedOutputDir = await new Promise<string>((res) => { | ||
| loadNuxt({ | ||
| cwd, | ||
| dotenv: { | ||
| cwd, | ||
| fileName: ctx.args.dotenv, | ||
| }, | ||
| envName: ctx.args.envName, // c12 will fall back to NODE_ENV | ||
| ready: true, | ||
| overrides: { | ||
| ...(ctx.args.extends && { extends: ctx.args.extends }), | ||
| modules: [ | ||
| function (_, nuxt) { | ||
| nuxt.hook('nitro:init', (nitro) => { | ||
| res(resolve(nuxt.options.srcDir || cwd, nitro.options.output.dir || '.output', 'nitro.json')) | ||
| }) | ||
| }, | ||
| ], | ||
| }, | ||
| }).then(nuxt => nuxt.close()).catch(() => '') | ||
| }) | ||
|
|
||
| if (resolvedOutputDir && existsSync(resolvedOutputDir)) { | ||
| nitroJSONPath = resolvedOutputDir | ||
| } | ||
| } | ||
|
|
||
| const nitroJSONPaths = [resolvedOutputDir, defaultOutput].filter(Boolean) | ||
| const nitroJSONPath = nitroJSONPaths.find(p => existsSync(p)) | ||
| if (!nitroJSONPath) { | ||
| logger.error( | ||
| `Cannot find ${colors.cyan('nitro.json')}. Did you run ${colors.cyan('nuxi build')} first? Search path:\n${nitroJSONPaths.join('\n')}`, | ||
| `Cannot find ${colors.cyan('nitro.json')}. Did you run ${colors.cyan('nuxi build')} first? Search path:\n${defaultOutput}`, | ||
| ) | ||
|
Comment on lines
80
to
83
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Report the path that was actually checked. Once the fallback runs, a miss may come from a non-default 🤖 Prompt for AI Agents |
||
| process.exit(1) | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Preserve
--extendson the.outputfast path.Line 47 short-circuits before the explicit override on Line 63 is ever applied. If
cwdalready has a.output/nitro.json,nuxi preview --extends ...can boot that artifact instead of the layered build the caller asked for.💡 One minimal way to keep the optimization for the common case
Also applies to: 63-64
🤖 Prompt for AI Agents