Skip to content

Conversation

@Flo0806
Copy link
Collaborator

@Flo0806 Flo0806 commented Oct 27, 2025

🔗 Linked issue

Fixes: #33582

Summary

Adds enforce: 'pre' to the EnvironmentsPlugin to ensure it runs before other Vite plugins, fixing issues where dependency optimization configuration was not correctly
applied.

Problem

After migrating to Vite's Environment API, certain packages (like ufo, @supabase/postgrest-js) failed to load with errors:

  • The entry point "ufo" cannot be marked as external
  • The requested module doesn't provide an export named: 'default'

This happened because the optimizeDeps configuration from clientEnvironment() was being overridden or not correctly merged by other plugins running later in the
pipeline.

Solution

By adding enforce: 'pre' to the EnvironmentsPlugin, we ensure:

  1. The plugin runs before all normal plugins
  2. The optimizeDeps configuration is correctly applied at the root level
  3. Vite properly optimizes CJS dependencies to ESM before other plugins can interfere

Test Plan

  • Tested with @nuxtjs/supabase module in playground
  • Verified no "ufo" external errors
  • Confirmed Supabase client initializes correctly
  • Checked that CJS modules are properly optimized

@Flo0806 Flo0806 requested a review from danielroe as a code owner October 27, 2025 12:19
@bolt-new-by-stackblitz
Copy link

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

@coderabbitai
Copy link

coderabbitai bot commented Oct 27, 2025

Walkthrough

The EnvironmentsPlugin in packages/vite/src/plugins/environments.ts was modified to include an enforce: 'pre' property in its returned plugin object. This configuration directive ensures the plugin executes earlier in Vite's plugin execution pipeline, before other plugins.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~3–5 minutes

  • Single file modification with minimal scope
  • One property addition to an existing object
  • No logic changes or method alterations
  • Straightforward configuration change

Potential areas requiring attention:

  • Verify that the enforce: 'pre' placement does not introduce unintended side effects with plugin ordering or dependencies
  • Confirm this is the correct enforcement level needed for the EnvironmentsPlugin's intended functionality

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 (4 passed)
Check name Status Explanation
Title Check ✅ Passed The pull request title "fix(vite): ensure optimizeDeps config is applied with environment api" is directly related to the main change in the changeset. The title clearly communicates that the fix addresses ensuring optimizeDeps configuration is properly applied when using Vite's environment API. The change (adding enforce: 'pre' to the EnvironmentsPlugin) is the mechanism by which this objective is achieved. The title is specific, concise, and provides meaningful context about what is being fixed without being vague or overly broad.
Linked Issues Check ✅ Passed The code change directly addresses the requirements from linked issue #33582. The issue reports that after updating to Nuxt 4.20, the browser receives CommonJS builds instead of ESM builds, causing "does not provide an export named 'default'" errors for packages like @supabase/postgrest-js. The root cause was that optimizeDeps configuration was being overridden or not correctly merged by plugins running later in the pipeline. The PR's solution of adding enforce: 'pre' to the EnvironmentsPlugin ensures the plugin runs first, allowing optimizeDeps configuration to be properly applied before other plugins can interfere. The PR's test plan confirms the fix resolves the issue by verifying no errors occur and CJS modules are properly optimized to ESM.
Out of Scope Changes Check ✅ Passed
Description Check ✅ Passed The pull request description is well-related to the changeset and provides comprehensive context about the problem, root cause, solution, and testing verification. It explains that the change adds enforce: 'pre' to the EnvironmentsPlugin to ensure it runs before other Vite plugins, which prevents optimizeDeps configuration from being overridden. The description includes specific details about the failures encountered (packages like ufo and @supabase/postgrest-js), the suspected root cause, and a concrete test plan with verification steps. The level of detail demonstrates clear understanding of the issue being addressed.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

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.

Copy link

@coderabbitai coderabbitai bot left a 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 (1)
packages/vite/src/plugins/environments.ts (1)

26-27: LGTM! This correctly fixes the plugin execution order issue.

The addition of enforce: 'pre' ensures that the EnvironmentsPlugin runs before other Vite plugins, preventing later plugins from overriding or interfering with the optimizeDeps configuration. This directly addresses the regression where CommonJS modules (like ufo and @supabase/postgrest-js) weren't being properly optimised to ESM.

The approach aligns with Vite's plugin architecture for infrastructure plugins that need to establish foundational configuration.

Optional: Consider enhancing the comment for better maintainability.

The comment could be more specific about why early execution is critical:

-    enforce: 'pre', // run before other plugins
+    enforce: 'pre', // run before other plugins to ensure optimizeDeps config is applied correctly

This would help future maintainers understand the specific reasoning behind the enforcement directive.

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 6f54e16 and d2cfac2.

📒 Files selected for processing (1)
  • packages/vite/src/plugins/environments.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/vite/src/plugins/environments.ts
⏰ 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). (2)
  • GitHub Check: build
  • GitHub Check: code

@pkg-pr-new
Copy link

pkg-pr-new bot commented Oct 27, 2025

Open in StackBlitz

@nuxt/kit

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

nuxt

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

@nuxt/rspack-builder

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

@nuxt/schema

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

@nuxt/vite-builder

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

@nuxt/webpack-builder

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

commit: d2cfac2

@codspeed-hq
Copy link

codspeed-hq bot commented Oct 27, 2025

CodSpeed Performance Report

Merging #33586 will not alter performance

Comparing Flo0806:fix/vite-environments-plugin-order (d2cfac2) with main (be83cda)1

Summary

✅ 10 untouched

Footnotes

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

@benjamincanac
Copy link
Member

benjamincanac commented Oct 27, 2025

I just made a pnpm patch of @nuxt/vite-builder to add enforce: 'pre' inside a project with the same error and it does fix the issue!

SyntaxError: The requested module '/_nuxt/@fs/Users/benjamincanac/GitHub/nuxt-ui-templates/chat/node_modules/.pnpm/extend@3.0.2/node_modules/extend/index.js?v=ee620684' does not provide an export named 'default'

nuxt-ui-templates/chat@24ddc46

@yassilah
Copy link
Contributor

I can confirm that this also fixes a related issue when testing using Vitest browser mode!

Copy link
Member

@danielroe danielroe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thank you 😊

@zealsprince
Copy link

Hello! Since this is a downstream breaking issue (specifically for the Nuxt Supabase plugin) with otherwise no side-effects from preliminary inspections and an approving review: when can we expect to see this merged and released? Wondering if I should hold off rolling back if the fix is imminent :)

@danielroe
Copy link
Member

fix should be imminent!

prplwtf added a commit to BlueprintFramework/web that referenced this pull request Nov 4, 2025
@prplwtf

This comment was marked as off-topic.

@danielroe danielroe merged commit 038f9d9 into nuxt:main Nov 5, 2025
100 of 101 checks passed
@github-actions github-actions bot mentioned this pull request Nov 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Nuxt 4.20 delivers CommonJS builds to browser

6 participants