Skip to content

fix(build): emit import attributes with the with key - #4520

Open
agantelin wants to merge 1 commit into
nitrojs:mainfrom
agantelin:fix/rollup-import-attributes-key
Open

fix(build): emit import attributes with the with key#4520
agantelin wants to merge 1 commit into
nitrojs:mainfrom
agantelin:fix/rollup-import-attributes-key

Conversation

@agantelin

@agantelin agantelin commented Aug 9, 2026

Copy link
Copy Markdown

🔗 Linked issue

#4518

❓ Type of change

  • 🐞 Bug fix (a non-breaking change that fixes an issue)

📚 Description

Rollup 4 re-prints the import attributes of external modules with its default key, assert, which
Node.js removed in 22.0.0 (nodejs/node#52104). Rollup itself flips the default in v5
(rollup/rollup#6248, merged to rollup-5), and documents output.importAttributesKey as the
workaround until then; no 5.x is published.

This only bites the rollup paths — the default rolldown builder emits with already — so it's
builder: "rollup" and the rollup branch of the vite builder:

  • src/build/rollup/config.ts — the default in the builder's output config; it sits in the object
    defu merges under nitro.options.rollupConfig, so user config still wins.
  • src/build/vite/bundler.ts — same default, applied with ??= after the merge, because in that
    branch commonConfig is merged last and a literal would outrank user config.

Symptom: the build is green, the server starts, and the route that imports the module answers 500
with TypeError [ERR_IMPORT_ATTRIBUTE_MISSING]. I hit it through @nuxt/icon's
serverBundle.externalizeIconsJson, which generates a correct with { type: "json" } import.
Setting the key costs nothing on the supported matrix: the runtimes that take assert but reject
with are all below engines.node.

📝 Checklist

Verification

  • test/unit/import-attributes.test.ts covers three things, each checked by reverting the
    corresponding line one at a time: the rollup builder's default, the vite builder's rollup branch,
    and that an explicit rollupConfig.output.importAttributesKey from user config survives both.
  • End-to-end on nitro@3.0.260610-beta with builder: "rollup": assert → HTTP 500
    ERR_IMPORT_ATTRIBUTE_MISSING, with → 200. Case 4 in
    https://github.com/agantelin/nitro-import-attributes-repro
  • A test/fixture/ regression route can't express this: the case needs an external JSON import,
    which the worker and edge presets can't load, so the fixture suite would fail for other reasons.
  • What I could not check end to end is a real vite buildbuilder: "vite" doesn't get that far
    on Windows for me (ERR_UNSUPPORTED_ESM_URL_SCHEME … Received protocol 'c:', unrelated to this
    change, but you may want to know). The emitted config is pinned by the test above.

@agantelin
agantelin requested a review from pi0 as a code owner August 9, 2026 00:00
@vercel

vercel Bot commented Aug 9, 2026

Copy link
Copy Markdown

@agantelin is attempting to deploy a commit to the Nitro Team on Vercel.

A member of the Team first needs to authorize it.

@coderabbitai

coderabbitai Bot commented Aug 9, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

Rollup output configuration now sets importAttributesKey to "with" by default. Tests cover direct Rollup and Vite builder output, including explicit "assert" configuration.

Changes

Import attributes configuration

Layer / File(s) Summary
Rollup output configuration
src/build/rollup/config.ts, src/build/vite/bundler.ts
Rollup uses "with" for import attributes. The Vite bundler applies "with" when no value is configured.
Configuration validation
test/unit/import-attributes.test.ts
Tests verify default "with" output and preserve explicit "assert" configuration for both output paths.

Estimated code review effort: 2 (Simple) | ~10 minutes

Possibly related issues

Possibly related PRs

  • nitrojs/nitro#4431 — It adds related import-attributes handling in the build pipeline.
🚥 Pre-merge checks | ✅ 3 | ❌ 2

❌ Failed checks (2 warnings)

Check name Status Explanation Resolution
Linked Issues check ⚠️ Warning The changes do not address linked issue #39, which requires adding a Layer0 preset and related build outputs. Link the correct issue for the import-attribute fix, or implement the Layer0 preset requirements from #39.
Out of Scope Changes check ⚠️ Warning The import-attribute changes are unrelated to linked issue #39, whose scope is the Layer0 preset. Use a linked issue covering the Rollup import-attribute fix, or limit this pull request to Layer0 preset changes.
✅ Passed checks (3 passed)
Check name Status Explanation
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Title check ✅ Passed The title follows Conventional Commits format and clearly describes the import-attribute output fix.
Description check ✅ Passed The description directly explains the Rollup fix, affected paths, user overrides, tests, and verification results.
✨ Finishing Touches 💡 1
🛠️ Fix failing CI checks 💡
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with 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.

❤️ Share

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

@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.

🧹 Nitpick comments (2)
src/build/rollup/config.ts (1)

61-62: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Remove the added explanatory source comments.

Both changed source segments explain compatibility details for a configuration line. Remove these comments or move the rationale to documentation.

  • src/build/rollup/config.ts#L61-L62: remove the two-line comment.
  • src/build/vite/bundler.ts#L100-L100: remove the duplicate comment.

As per coding guidelines: “Do not add comments explaining what the line does unless prompted.”

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/build/rollup/config.ts` around lines 61 - 62, Remove the two-line
explanatory comment at src/build/rollup/config.ts lines 61-62 and the duplicate
comment at src/build/vite/bundler.ts line 100, leaving the associated
configuration lines unchanged.

Source: Coding guidelines

test/unit/import-attributes.test.ts (1)

9-14: 🗄️ Data Integrity & Integration | 🔵 Trivial | ⚡ Quick win

Cover both configuration paths and the override contract.

This test invokes getRollupConfig only. It does not exercise the Rollup branch of src/build/vite/bundler.ts (Line 70-106) or verify that an explicit importAttributesKey: "assert" remains unchanged. Add default and override cases for both builders.

Based on PR objectives: the change covers both the Rollup builder and the Vite Rollup branch while preserving user overrides.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@test/unit/import-attributes.test.ts` around lines 9 - 14, Expand the
import-attributes tests around getRollupConfig and the Rollup branch in
bundler.ts to cover both builders: verify each defaults importAttributesKey to
"with", and verify an explicit "assert" configuration remains unchanged for each
path. Reuse the existing Nitro/Vite setup and preserve the current default
assertion.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@src/build/rollup/config.ts`:
- Around line 61-62: Remove the two-line explanatory comment at
src/build/rollup/config.ts lines 61-62 and the duplicate comment at
src/build/vite/bundler.ts line 100, leaving the associated configuration lines
unchanged.

In `@test/unit/import-attributes.test.ts`:
- Around line 9-14: Expand the import-attributes tests around getRollupConfig
and the Rollup branch in bundler.ts to cover both builders: verify each defaults
importAttributesKey to "with", and verify an explicit "assert" configuration
remains unchanged for each path. Reuse the existing Nitro/Vite setup and
preserve the current default assertion.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: d68b8388-75d2-46b7-96cf-1df50ca62b85

📥 Commits

Reviewing files that changed from the base of the PR and between 52abde8 and fb8c898.

📒 Files selected for processing (3)
  • src/build/rollup/config.ts
  • src/build/vite/bundler.ts
  • test/unit/import-attributes.test.ts

@agantelin
agantelin force-pushed the fix/rollup-import-attributes-key branch from fb8c898 to e4c32a5 Compare August 9, 2026 13:07
@agantelin

Copy link
Copy Markdown
Author

Pushed an update (force-push, same two source lines):

  • The test now also covers the vite builder's rollup branch and the user-override contract, so both
    changed lines are pinned rather than just the rollup one. Each of the three assertions was checked
    by reverting its line individually.
  • Trimmed the rationale comments to one line each.

Nothing else changed. Still unverified on my side: an actual vite build, which doesn't start on
Windows (ERR_UNSUPPORTED_ESM_URL_SCHEME, unrelated to this PR).

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant