Skip to content

Fix direct native ESM imports on Vite 7 and 8 - #20

Merged
biw merged 4 commits into
mainfrom
biw/fix-direct-native-imports
Aug 8, 2026
Merged

Fix direct native ESM imports on Vite 7 and 8#20
biw merged 4 commits into
mainfrom
biw/fix-direct-native-imports

Conversation

@biw

@biw biw commented Aug 8, 2026

Copy link
Copy Markdown
Owner

Summary

Direct ESM imports of .node files could be claimed by Vite's generic asset handling before this plugin resolved them, causing the binary to be parsed as JavaScript. This child PR resolves native imports first and adds regression coverage on Vite 7.3.6 and 8.2.1.

What This PR Does

  • Runs native module resolution before Vite's asset resolver.
  • Runs native asset emission after Vite's output hooks so tree-shaking continues to emit used addons.
  • Adds isolated Vite 7 and Vite 8 aliases and verifies a normal direct .node ESM import builds in both.

What This Improves

  • import addon from "./addon.node" now bundles successfully with the plugin's normal configuration on Vite 7 and Vite 8.

What This Does Not Do

  • Does not change native wrapper generation, package publishing, or the supported peer dependency range.

Important Context

Code To Review Closely

src/index.ts:740

The resolver must run before Vite classifies the .node import as a generic asset. The generated asset hook is separately ordered last at src/index.ts:1704 so native files remain visible after tree-shaking.

name,

enforce: "pre",

async resolveId(source, importer) {
  // Check if enabled
  const enabled = options.forced ?? command === "build";

Gotchas, Risks, And Edge Cases

  • Resolution ordering changes only the point at which qualifying native files are intercepted; missing and non-native imports still return null.
  • The post-order output hook retains a callable bridge for the existing direct-hook unit tests while using Rollup's object-hook ordering.

Tests And Regression Coverage

Validation Run

  • vp check: passed.
  • vp run test: passed, 197 tests.
  • vp pack: passed.

Coverage Assessment

Sufficient for the reported bundling regression: the new test creates a direct static .node ESM import and builds it through the normal plugin configuration against both Vite 7.3.6 and Vite 8.2.1. Existing tree-shaking coverage verifies that used native assets are still emitted.

Regressions These Tests Should Catch

  • Vite asset handling preempting the plugin and attempting to parse a native binary.
  • Native asset emission occurring before final tree-shaking output is available.

Gaps And Recommended Follow-Up Tests

  • The fixture uses fake native bytes and validates bundling, not runtime loading of a compiled addon.

Follow-Up Work

  • Not applicable.

@biw biw changed the title biw/fix direct native imports Fix direct native ESM imports on Vite 7 and 8 Aug 8, 2026
Base automatically changed from biw/migrate-to-vite-plus to main August 8, 2026 00:16
@biw
biw force-pushed the biw/fix-direct-native-imports branch from 1746688 to 18e513c Compare August 8, 2026 00:16
@biw
biw merged commit 6e5417c into main Aug 8, 2026
2 checks passed
@biw
biw deleted the biw/fix-direct-native-imports branch August 8, 2026 00:16
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