Skip to content

fix(webpack,rspack): resolve deep imports in virtual files - #33927

Merged
danielroe merged 7 commits into
mainfrom
fix/webpack-deep-imports
Dec 21, 2025
Merged

danielroe merged 7 commits into
mainfrom
fix/webpack-deep-imports

Conversation

@danielroe

@danielroe danielroe commented Dec 21, 2025 •

Copy link
Copy Markdown
Member

🔗 Linked issue

extracted from #33005

📚 Description

previously we were relying on nitro resolving these files, which happened to work but nitro is making its module resolution algorithm more performant in v3

👉 Next steps

  • move deep imports plugin -> vite
  • add resolved externals plugin -> webpack

@bolt-new-by-stackblitz

Copy link
Copy Markdown

Review PR in StackBlitz Codeflow Run & review this pull request in StackBlitz Codeflow.

@github-actions github-actions Bot added 4.x 🐛 bug Something isn't working as expected labels Dec 21, 2025
@coderabbitai

coderabbitai Bot commented Dec 21, 2025 •

Copy link
Copy Markdown
Contributor

Walkthrough

This PR expands server externals resolution and Vite plugin wiring. It adds path/URL utilities, a VIRTUAL_RE for virtual-module detection, and a resolver conditions array. The server standalone externals flow is made async and now attempts module resolution via resolveModulePath, suppressing externalisation when a local absolute resolution is found. Nitro/nuxt runtimeDependencies are integrated from nuxt/meta, runtime-aware externals sets are updated, and ResolveDeepImports/ResolveExternals plugins are added and wired via unified build-plugin registrations.

Possibly related PRs

  • nuxt/nuxt PR 31235: Modifies the same externals resolution flow and adds ResolveDeepImportsPlugin / ResolveExternalsPlugin wiring across builders.
  • nuxt/nuxt PR 31436: Adds resolveModulePath-based resolution and adjusts runtimeDependencies sourcing used by externalisation logic.
  • nuxt/nuxt PR 33403: Alters deep-import and resolved-externals logic, including environment-specific resolve conditions and Nitro runtime dependency handling.

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately describes the main change: implementing deep imports resolution for webpack and rspack in virtual files, which is the core focus of this pull request.
Description check ✅ Passed The description is directly related to the changeset, explaining the context (Nitro v3 changes), motivation, and completed tasks that align with the file modifications shown.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch fix/webpack-deep-imports

📜 Recent review details

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 7fc5ce1 and 61fe9c6.

⛔ Files ignored due to path filters (3)
  • packages/rspack/package.json is excluded by !**/package.json
  • packages/webpack/package.json is excluded by !**/package.json
  • pnpm-lock.yaml is excluded by !**/pnpm-lock.yaml, !pnpm-lock.yaml
📒 Files selected for processing (1)
  • packages/nuxt/src/core/nuxt.ts (3 hunks)
🧰 Additional context used
📓 Path-based instructions (2)
**/*.{ts,tsx,vue}

📄 CodeRabbit inference engine (.github/copilot-instructions.md)

Follow standard TypeScript conventions and best practices

Files:

  • packages/nuxt/src/core/nuxt.ts
**/*.{ts,tsx,js,jsx,vue}

📄 CodeRabbit inference engine (.github/copilot-instructions.md)

**/*.{ts,tsx,js,jsx,vue}: Use clear, descriptive variable and function names
Add comments only to explain complex logic or non-obvious implementations
Keep functions focused and manageable (generally under 50 lines), and extract complex logic into separate domain-specific files
Remove code that is not used or needed
Use error handling patterns consistently

Files:

  • packages/nuxt/src/core/nuxt.ts
🧠 Learnings (3)
📓 Common learnings
Learnt from: GalacticHypernova
Repo: nuxt/nuxt PR: 26468
File: packages/nuxt/src/components/plugins/loader.ts:24-24
Timestamp: 2024-11-05T15:22:54.759Z
Learning: In `packages/nuxt/src/components/plugins/loader.ts`, the references to `resolve` and `distDir` are legacy code from before Nuxt used the new unplugin VFS and will be removed.
📚 Learning: 2024-11-05T15:22:54.759Z
Learnt from: GalacticHypernova
Repo: nuxt/nuxt PR: 26468
File: packages/nuxt/src/components/plugins/loader.ts:24-24
Timestamp: 2024-11-05T15:22:54.759Z
Learning: In `packages/nuxt/src/components/plugins/loader.ts`, the references to `resolve` and `distDir` are legacy code from before Nuxt used the new unplugin VFS and will be removed.

Applied to files:

  • packages/nuxt/src/core/nuxt.ts
📚 Learning: 2024-12-12T12:36:34.871Z
Learnt from: huang-julien
Repo: nuxt/nuxt PR: 29366
File: packages/nuxt/src/app/components/nuxt-root.vue:16-19
Timestamp: 2024-12-12T12:36:34.871Z
Learning: In `packages/nuxt/src/app/components/nuxt-root.vue`, when optimizing bundle size by conditionally importing components based on route metadata, prefer using inline conditional imports like:

```js
const IsolatedPage = route?.meta?.isolate ? defineAsyncComponent(() => import('#build/isolated-page.mjs')) : null
```

instead of wrapping the import in a computed property or importing the component unconditionally.

Applied to files:

  • packages/nuxt/src/core/nuxt.ts
🧬 Code graph analysis (1)
packages/nuxt/src/core/nuxt.ts (2)
packages/kit/src/build.ts (1)
  • addBuildPlugin (217-229)
packages/kit/src/index.ts (1)
  • addBuildPlugin (22-22)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: code
🔇 Additional comments (3)
packages/nuxt/src/core/nuxt.ts (3)

10-10: LGTM: Import addition for unified build plugin API.

The addition of addBuildPlugin import supports the new unified plugin registration pattern used below at lines 400 and 413.


413-419: LGTM: Intentional hybrid approach for Nuxt app protection.

The use of addBuildPlugin for webpack/rspack whilst retaining separate addVitePlugin calls for Vite is deliberate and well-documented. The TODO comment clearly explains that Vite requires different error handling for client vs server builds and will be unified in Nuxt v5 when the Vite env API is available.


400-404: LGTM: Clean migration to unified build plugin API.

The migration from separate plugin registrations to addBuildPlugin is well-structured. The ImpoundPlugin (imported from the 'impound' library) exports the required static methods (vite(), webpack(), rspack()), each receiving the same protection configuration. The client-only restriction is maintained via { server: false }.


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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@pkg-pr-new

pkg-pr-new Bot commented Dec 21, 2025 •

Copy link
Copy Markdown

Open in StackBlitz

@nuxt/kit

npm i https://pkg.pr.new/@nuxt/kit@33927

@nuxt/nitro-server

npm i https://pkg.pr.new/@nuxt/nitro-server@33927

nuxt

npm i https://pkg.pr.new/nuxt@33927

@nuxt/rspack-builder

npm i https://pkg.pr.new/@nuxt/rspack-builder@33927

@nuxt/schema

npm i https://pkg.pr.new/@nuxt/schema@33927

@nuxt/vite-builder

npm i https://pkg.pr.new/@nuxt/vite-builder@33927

@nuxt/webpack-builder

npm i https://pkg.pr.new/@nuxt/webpack-builder@33927

commit: 2f1173c

@codspeed

codspeed Bot commented Dec 21, 2025 •

Copy link
Copy Markdown

CodSpeed Performance Report

Merging #33927 will not alter performance

Comparing fix/webpack-deep-imports (2f1173c) with main (f9c092c)1

Summary

✅ 10 untouched

Footnotes

  1. No successful run was found on main (fbb9929) during the generation of this report, so f9c092c was used instead as the comparison base. There might be some changes unrelated to this pull request in this report. ↩

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

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 (2)
packages/webpack/src/configs/server.ts (2)

13-13: Consider extracting VIRTUAL_RE to a shared utility.

This pattern is duplicated in packages/vite/src/plugins/resolve-deep-imports.ts. Extracting it to @nuxt/kit or a shared constants module would improve maintainability.


119-137: Core fix for virtual file resolution looks correct.

The logic properly attempts local resolution before externalising, which is essential for deep imports in virtual files.

However, the empty catch block silently swallows all errors. Consider logging at debug level to aid troubleshooting, consistent with the Vite plugin's logger.debug('Could not resolve id', ...) pattern.

🔎 Suggested improvement for debugging
       } catch {
-        // Ignore resolution errors, fall through to externalize
+        // Ignore resolution errors, fall through to externalize
+        logger.debug('Could not resolve', request, 'from', context)
       }
📜 Review details

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 9c1319f and 7fc5ce1.

⛔ Files ignored due to path filters (1)
  • packages/nuxt/package.json is excluded by !**/package.json
📒 Files selected for processing (7)
  • eslint.config.mjs (1 hunks)
  • packages/nuxt/src/core/nuxt.ts (0 hunks)
  • packages/vite/src/plugins/resolve-deep-imports.ts (2 hunks)
  • packages/vite/src/plugins/resolved-externals.ts (1 hunks)
  • packages/vite/src/vite.ts (2 hunks)
  • packages/webpack/src/configs/server.ts (5 hunks)
  • tsconfig.json (1 hunks)
💤 Files with no reviewable changes (1)
  • packages/nuxt/src/core/nuxt.ts
✅ Files skipped from review due to trivial changes (1)
  • tsconfig.json
🧰 Additional context used
📓 Path-based instructions (2)
**/*.{ts,tsx,vue}

📄 CodeRabbit inference engine (.github/copilot-instructions.md)

Follow standard TypeScript conventions and best practices

Files:

  • packages/vite/src/plugins/resolve-deep-imports.ts
  • packages/vite/src/vite.ts
  • packages/webpack/src/configs/server.ts
  • packages/vite/src/plugins/resolved-externals.ts
**/*.{ts,tsx,js,jsx,vue}

📄 CodeRabbit inference engine (.github/copilot-instructions.md)

**/*.{ts,tsx,js,jsx,vue}: Use clear, descriptive variable and function names
Add comments only to explain complex logic or non-obvious implementations
Keep functions focused and manageable (generally under 50 lines), and extract complex logic into separate domain-specific files
Remove code that is not used or needed
Use error handling patterns consistently

Files:

  • packages/vite/src/plugins/resolve-deep-imports.ts
  • packages/vite/src/vite.ts
  • packages/webpack/src/configs/server.ts
  • packages/vite/src/plugins/resolved-externals.ts
🧠 Learnings (2)
📚 Learning: 2024-11-05T15:22:54.759Z
Learnt from: GalacticHypernova
Repo: nuxt/nuxt PR: 26468
File: packages/nuxt/src/components/plugins/loader.ts:24-24
Timestamp: 2024-11-05T15:22:54.759Z
Learning: In `packages/nuxt/src/components/plugins/loader.ts`, the references to `resolve` and `distDir` are legacy code from before Nuxt used the new unplugin VFS and will be removed.

Applied to files:

  • packages/vite/src/plugins/resolve-deep-imports.ts
  • packages/vite/src/vite.ts
  • packages/webpack/src/configs/server.ts
  • packages/vite/src/plugins/resolved-externals.ts
  • eslint.config.mjs
📚 Learning: 2024-12-12T12:36:34.871Z
Learnt from: huang-julien
Repo: nuxt/nuxt PR: 29366
File: packages/nuxt/src/app/components/nuxt-root.vue:16-19
Timestamp: 2024-12-12T12:36:34.871Z
Learning: In `packages/nuxt/src/app/components/nuxt-root.vue`, when optimizing bundle size by conditionally importing components based on route metadata, prefer using inline conditional imports like:

```js
const IsolatedPage = route?.meta?.isolate ? defineAsyncComponent(() => import('#build/isolated-page.mjs')) : null
```

instead of wrapping the import in a computed property or importing the component unconditionally.

Applied to files:

  • packages/vite/src/plugins/resolve-deep-imports.ts
  • packages/vite/src/vite.ts
  • packages/webpack/src/configs/server.ts
  • packages/vite/src/plugins/resolved-externals.ts
  • eslint.config.mjs
🧬 Code graph analysis (2)
packages/vite/src/vite.ts (2)
packages/vite/src/plugins/resolve-deep-imports.ts (1)
  • ResolveDeepImportsPlugin (11-88)
packages/vite/src/plugins/resolved-externals.ts (1)
  • ResolveExternalsPlugin (8-66)
packages/webpack/src/configs/server.ts (3)
packages/webpack/src/utils/config.ts (1)
  • WebpackConfigContext (6-17)
packages/kit/src/index.ts (3)
  • tryImportModule (45-45)
  • directoryToURL (45-45)
  • resolveAlias (37-37)
packages/kit/src/resolve.ts (1)
  • resolveAlias (92-95)
🔇 Additional comments (10)
packages/vite/src/plugins/resolve-deep-imports.ts (2)

5-5: LGTM!

Consolidating imports from @nuxt/kit is cleaner and ensures consistent utility usage across the codebase.


63-63: Fallback to appDir is appropriate for Nuxt template resolution.

Using nuxt.options.appDir as the fallback directory aligns with how Nuxt templates are resolved, as they are typically relative to the application context rather than the package directory.

packages/vite/src/vite.ts (2)

41-42: LGTM!

Import additions for the new deep imports and externals resolution plugins.


193-195: Plugin registration order is correct.

ResolveDeepImportsPlugin uses enforce: 'post' and ResolveExternalsPlugin uses enforce: 'pre', ensuring externals are marked before other resolution attempts, whilst deep imports are resolved as a final fallback. The descriptive comment clarifies the purpose.

packages/webpack/src/configs/server.ts (4)

1-5: LGTM!

Imports are appropriately grouped and provide necessary utilities for the enhanced resolution logic.


44-44: LGTM!

The async conversion and dynamic import pattern for Nitro runtime meta aligns with the Vite implementation in resolved-externals.ts, ensuring consistent runtime dependency handling across build tools.

Also applies to: 60-62


85-90: LGTM!

The conditions array correctly prioritises node for server builds and includes both import and require for CommonJS interoperability.


96-107: LGTM!

The resolution attempt for known externals enables Nitro to handle version resolution correctly. Returning the absolute path when available, with a fallback to simple externalisation, is a sound approach.

packages/vite/src/plugins/resolved-externals.ts (1)

6-6: Import path change to nuxt/meta requires verification of module export availability.

The switch from a relative path to nuxt/meta cannot be confirmed as a valid public export. The runtimeDependencies export is found imported from './packages/nuxt/src/meta.mjs' in the Nuxt repository, but no evidence exists that nuxt/meta is a public API. Verify that this module export is available in Nuxt 4 before merging this change.

eslint.config.mjs (1)

10-10: No action required. The import has been verified to resolve correctly. The nuxt/meta export is properly configured in the package.json as "./meta": "./meta.mjs", and the meta.mjs file exists with the correct exports. The import in eslint.config.mjs will resolve without issues during linting.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

4.x 🐛 bug Something isn't working as expected

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant