Skip to content

fix: rewrite local declare module specifiers to output chunk paths#191

Open
igordanchenko wants to merge 1 commit intosxzz:mainfrom
igordanchenko:fix/rewrite-relative-module-declarations
Open

fix: rewrite local declare module specifiers to output chunk paths#191
igordanchenko wants to merge 1 commit intosxzz:mainfrom
igordanchenko:fix/rewrite-relative-module-declarations

Conversation

@igordanchenko
Copy link
Copy Markdown

  • This PR contains AI-generated code, but I have carefully reviewed it myself. Otherwise, my PR may be closed.
    • I understand that my PR is more likely to be rejected or requested for changes if it contains AI-generated code that I do not fully understand.

Description

When declare module './foo.js' or declare module '@/foo.js' references a local file, the specifier was preserved as-is in the bundled .d.ts output. This made the augmentation unreachable for consumers since the internal path no longer resolves after bundling.

Now resolves local declare module targets and rewrites their specifiers to the relative path of the output chunk containing the target module.

How it works

  1. transform - resolves all declare module string literal specifiers using this.resolve(). If the target is a local (non-external) module, stores the resolved module ID in the declaration metadata.
  2. renderChunk - replaces resolved specifiers with an internal placeholder.
  3. generateBundle - builds a module-to-chunk mapping and rewrites placeholders to the correct relative path between output chunks.

This approach works for both same-chunk (self-referencing) and cross-chunk augmentations, as well as tsconfig path aliases like @/foo.js.

Test plan

  • New test case with cross-chunk rewrites, subfolder resolution, and same-chunk self-references
  • Existing declare module test (ambient declare module 'virtual') still passes unchanged
  • Full test suite (194 tests), lint, typecheck all pass

Linked Issues

Fixes #134

Additional context

While the original ticket #134 mentions same-chunk references being replaced with declare module '.', I couldn't get that working with TypeScript. This PR rewrites same-chunk specifiers as declare module './current-chunk.js' instead.

@igordanchenko igordanchenko force-pushed the fix/rewrite-relative-module-declarations branch 2 times, most recently from 188c4a4 to 96dbcc7 Compare March 6, 2026 17:16
…xzz#134)

When `declare module './foo.js'` references a local file, the specifier
was preserved as-is in bundled output, making it unreachable for consumers.

Now resolves local module targets and rewrites specifiers to point to
the correct output chunk.
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.

declare module with local file path does not bundle correctly

1 participant