Skip to content

Fix TypeScript native module transforms - #23

Merged
biw merged 1 commit into
mainfrom
fix/transform-order-typescript
Aug 10, 2026
Merged

Fix TypeScript native module transforms#23
biw merged 1 commit into
mainfrom
fix/transform-order-typescript

Conversation

@biw

@biw biw commented Aug 10, 2026

Copy link
Copy Markdown
Owner

Summary

Fix TypeScript native-wrapper transforms without regressing direct .node ESM imports. The resolver still runs before Vite's asset handling, while the source transform runs after Vite transpiles TypeScript.

What This PR Does

  • Makes only resolveId pre-ordered instead of pre-ordering the entire plugin.
  • Adds generic Vite 7 and Vite 8 integration fixtures for TypeScript modules that load an addon.node file through require and createRequire.
  • Bumps the package version to 2.4.1.

What This Improves

  • Native wrapper code is parsed as JavaScript after TypeScript syntax has been removed.
  • Direct .node ESM imports continue to resolve before Vite classifies them as generic assets.

What This Does Not Do

  • Does not change native-binary discovery, hashing, loader generation, or runtime-sidecar handling.

Important Context

  • Plugin-wide enforce: "pre" caused the native source transform to parse raw TypeScript.
  • Vite and Rollup support per-hook ordering, so only the resolver needs to run early.

Code To Review Closely

src/index.ts:1934

The resolver retains pre-ordering to intercept direct native ESM imports, while transform keeps Vite's normal ordering.

const resolveId = plugin.resolveId;
if (typeof resolveId === "function") {
  plugin.resolveId = {
    call(context: unknown, ...args: unknown[]) {
      return Reflect.apply(resolveId, context, args);
    },
    handler: resolveId,
    order: "pre",
  } as Plugin["resolveId"];
}

Gotchas, Risks, And Edge Cases

  • The integration fixtures use fake binary contents and validate bundling behavior, not loading a compiled native addon at runtime.
  • Existing direct native ESM-import coverage remains in the same Vite 7/Vite 8 matrix.

Tests And Regression Coverage

Validation Run

  • pnpm test: passed (204 tests).
  • pnpm lint: passed (format, lint, and type checks).
  • pnpm build: passed.
  • pnpm precommit: unavailable; this package does not define that script.

Coverage Assessment

Sufficient. The new fixtures exercise both conventional and createRequire TypeScript native wrappers with Vite 7 and Vite 8, and assert that the hashed native asset is emitted and referenced.

Regressions These Tests Should Catch

  • Applying pre-ordering to transform and parsing TypeScript before Vite transpiles it.
  • Failing to emit or reference a native asset loaded from TypeScript wrapper code.
  • Losing direct .node ESM import compatibility on Vite 7 or Vite 8.

Gaps And Recommended Follow-Up Tests

  • Consider a runtime smoke test with a compiled addon when a cross-platform fixture becomes available.

Follow-Up Work

  • Not applicable.

@biw
biw merged commit 62fab2f into main Aug 10, 2026
2 checks passed
@biw
biw deleted the fix/transform-order-typescript branch August 10, 2026 23:05
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