-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
fix(nuxt): move key imports logic after all modules run #33214
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
Conversation
|
|
|
Warning Rate limit exceeded@danielroe has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 22 minutes and 19 seconds before requesting another review. ⌛ How to resolve this issue?After the wait time has elapsed, a review can be triggered using the We recommend that you space out your commits to avoid hitting the rate limit. 🚦 How do rate limits work?CodeRabbit enforces hourly rate limits for each developer per organization. Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout. Please see our FAQ for further information. 📒 Files selected for processing (1)
Walkthrough
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Pre-merge checks (4 passed, 1 warning)❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing touches
🧪 Generate unit tests
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
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.
Actionable comments posted: 0
🧹 Nitpick comments (4)
packages/nuxt/src/imports/transform.ts (1)
13-13: Type narrowing looks good; consider normalising id earlier for consistency.The switch to
Pick<Unimport, 'injectImports'>is sound and tightens the surface area. One small consistency win: normaliseidintransformIncludebefore running include/exclude regexes (you already normalise intransform). This avoids platform-specific path mismatches.Apply:
export const TransformPlugin = ({ ctx, options, sourcemap }: { ctx: Pick<Unimport, 'injectImports'>, options: Partial<ImportsOptions>, sourcemap?: boolean }) => createUnplugin(() => { return { name: 'nuxt:imports-transform', enforce: 'post', transformInclude (id) { + id = normalize(id) // Included if (options.transform?.include?.some(pattern => pattern.test(id))) { return true } // Excluded if (options.transform?.exclude?.some(pattern => pattern.test(id))) { return false }packages/nuxt/src/imports/module.ts (3)
74-77: Deferring imports:dirs and path normalisation is correct; also normalise the watcher path.You normalise
composablesDirsonmodules:done. For symmetric comparisons in the watcher, normalisepathtoo to avoid separator issues on Windows.Apply:
- const path = resolve(nuxt.options.srcDir, relativePath) + const path = normalize(resolve(nuxt.options.srcDir, relativePath))
91-116: Initialising ctx on modules:done — please verify no early access; optional hardening with a ctxReady gate.Everything that uses
ctxappears to execute aftermodules:done, but there’s an implicit ordering assumption (e.g. template getters and the transform plugin wrapper). To be belt-and-braces, gate access behind a promise so accidental early calls don’t throw if ordering changes.Minimal hardening:
- let ctx: Unimport + let ctx: Unimport + let resolveCtx!: (u: Unimport) => void + const ctxReady = new Promise<Unimport>(r => { resolveCtx = r }) // initialise unimport only after all modules // have had a chance to register their hooks nuxt.hook('modules:done', async () => { await nuxt.callHook('imports:sources', presets) const { addons: inlineAddons, ...rest } = options const [addons, addonsOptions] = Array.isArray(inlineAddons) ? [inlineAddons] : [[], inlineAddons] // Create a context to share state between module internals ctx = createUnimport({ injectAtEnd: true, ...rest, addons: { addons, vueTemplate: options.autoImport, vueDirectives: options.autoImport === false ? undefined : true, ...addonsOptions, }, presets, }) await nuxt.callHook('imports:context', ctx) + resolveCtx(ctx) })And adapt the two main access sites:
- addTemplate({ + addTemplate({ filename: 'imports.mjs', - getContents: async () => toExports(await ctx.getImports()) + '\nif (import.meta.dev) { console.warn("[nuxt] `#imports` should be transformed with real imports. There seems to be something wrong with the imports plugin.") }', + getContents: async () => { + const i = await ctxReady + return toExports(await i.getImports()) + '\nif (import.meta.dev) { console.warn("[nuxt] `#imports` should be transformed with real imports. There seems to be something wrong with the imports plugin.") }' + }, })- addBuildPlugin(TransformPlugin({ - ctx: { injectImports: (code, id, options) => ctx.injectImports(code, id, options) }, + addBuildPlugin(TransformPlugin({ + ctx: { injectImports: async (code, id, options) => (await ctxReady).injectImports(code, id, options) }, options, sourcemap: !!nuxt.options.sourcemap.server || !!nuxt.options.sourcemap.client, }))Optionally, mirror this pattern for
addDeclarationTemplateswrappers for complete consistency.
139-140: Compute nuxtImports after imports:sources mutates presets.
presetscan be extended inimports:sources. ComputingnuxtImportsbefore that may miss newly contributed internal imports, weakening the override warning.Apply:
- const nuxtImportSources = new Set(allNuxtPresets.flatMap(i => i.from)) - const nuxtImports = new Set(presets.flatMap(p => nuxtImportSources.has(p.from) ? p.imports : [])) + const nuxtImportSources = new Set(allNuxtPresets.flatMap(i => i.from)) + let nuxtImports = new Set<string>()Then after
await nuxt.callHook('imports:sources', presets):+ // Recompute internal-import names after sources have been extended + nuxtImports = new Set(presets.flatMap(p => nuxtImportSources.has(p.from) ? p.imports : []))
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
packages/nuxt/src/imports/module.ts(5 hunks)packages/nuxt/src/imports/transform.ts(1 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
**/*.{ts,tsx}
📄 CodeRabbit inference engine (.github/copilot-instructions.md)
Follow standard TypeScript conventions and best practices
Files:
packages/nuxt/src/imports/module.tspackages/nuxt/src/imports/transform.ts
🧬 Code graph analysis (2)
packages/nuxt/src/imports/module.ts (2)
packages/nuxt/src/imports/transform.ts (1)
TransformPlugin(13-58)packages/schema/src/types/imports.ts (1)
ImportsOptions(3-36)
packages/nuxt/src/imports/transform.ts (1)
packages/nuxt/src/components/plugins/transform.ts (1)
TransformPlugin(22-140)
🔇 Additional comments (5)
packages/nuxt/src/imports/module.ts (5)
68-70: Guarding against falsey custom dirs — good catch.Prevents pushing
undefined/empty entries when resolving user-specified import dirs from layers.
125-128: Plugin wiring with narrowed ctx is clean.The lazy wrapper focuses exposure to
injectImportsand keeps the transform isolated from the full context.
176-176: Regenerating imports on modules:done is aligned with the sequencing goal.Ensures scanning and dynamic imports reflect all registered modules.
179-183: Narrowed ctx for declarations is appropriate.Limiting to
getImports/generateTypeDeclarationsclarifies intent and reduces coupling.
202-262: Signature change for addDeclarationTemplates is clear and contained.The inner usage matches the narrowed ctx; no issues spotted.
@nuxt/kit
nuxt
@nuxt/rspack-builder
@nuxt/schema
@nuxt/vite-builder
@nuxt/webpack-builder
commit: |
CodSpeed Performance ReportMerging #33214 will not alter performanceComparing Summary
Footnotes |
🔗 Linked issue
resolves #33196
📚 Description
with
moduleDependencies, it was possible for modules to be added after core nuxt modules like the imports module.this PR adapts the logic of the imports module to move the creation of the unimport context after all modules have run.