Add NAPI-RS pattern support for native module loaders#2
Merged
Conversation
Implements test-driven development support for NAPI-RS auto-generated
loaders used by packages like libsql-js. Adds detection and rewriting of:
- join(__dirname, 'xxx.node') path patterns (Pattern 6/6b)
- require('@scope/package-name') for platform-specific npm packages (Pattern 7)
Includes 13 comprehensive test cases covering real-world scenarios.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
The test was hardcoding platform-specific filenames that didn't match the actual platform on Linux CI. Now uses dynamic platform/arch values. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
…kages
This adds support for NAPI-RS/neon-rs style native module loaders that use
template literal requires like `require(`@libsql/${target}`)`.
Changes:
- Add findPlatformSpecificNativePackage helper to scan scope directories
for platform-matching native packages (e.g., @libsql/darwin-arm64)
- Add Pattern 8 to detect template literal requires and resolve them to
the current platform's native module package
- Update early bailout check to include template literal native package
patterns so the transform hook processes these files
- Add 3 new tests for the template literal pattern
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Rollup's getAugmentedNamespace helper has a bug where if the default export is not a function (like native .node module exports), it creates an empty object and only copies keys from the namespace (which only has 'default'), NOT from the default export itself. This caused `databaseOpen is not a function` errors because the destructured properties weren't available on the wrapped namespace. The fix adds `__esModule = true` marker to the native exports, which tells getAugmentedNamespace to return the object as-is without creating an empty wrapper. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
The previous fix attempted to set __esModule on nativeExports, but Rollup
still wrapped it in { default: nativeExports }, so getAugmentedNamespace
never saw the marker.
This fix exploits Rollup's special handling for function defaults:
- Export a function wrapper with native module properties attached
- getAugmentedNamespace creates a callable wrapper preserving all properties
- Code can destructure databaseOpen, etc. from the result
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
When filenameFormat is "preserve" (default), native module filenames now include the package name prefix for better identification: - @libsql/darwin-arm64/index.node → @libsql-darwin-arm64-index-ABC123.node - sql/native.node → sql-native-ABC123.node Scoped package names have / converted to - for file safety. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
…ct return
- Remove @ prefix from package names in extractPackageName to make them path-safe
(e.g., @libsql/darwin-arm64 → libsql-darwin-arm64)
- Update test files to handle resolveId returning { id, syntheticNamedExports }
object instead of just a string ID
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
…ssue) These tests cover the fix for the "databaseOpen is not a function" error that occurred when Rollup's getAugmentedNamespace wrapped native modules. The fix uses syntheticNamedExports: true in resolveId to tell Rollup to resolve named exports from the default export's properties. Tests added: - Verify resolveId returns syntheticNamedExports: true for hashed .node files - Verify ES module load hook output enables destructuring - Verify libsql-style destructuring pattern works with syntheticNamedExports 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
- Add registerNativeFile() helper to centralize hash generation and storage - Add detectModuleTypeWithContext() helper for module type detection - Update resolveId, Pattern 5-8, and processNodeFile to use registerNativeFile - Combine Pattern 6 and 6b into unified path.join/__dirname handler - Net reduction of ~250 lines by eliminating duplicate code 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Implements test-driven development support for NAPI-RS auto-generated loaders used by packages like libsql-js. Adds detection and rewriting of
join(__dirname, 'xxx.node')patterns andrequire('@scope/package')platform-specific npm packages.Changes
join(__dirname, 'xxx.node')path expressions in existsSync calls.nodefiles and rewrite requiresTest Results
All 147 tests passing (134 existing + 13 new)
🤖 Generated with Claude Code