Skip to content

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

Open
igordanchenko wants to merge 1 commit into
sxzz: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 into
sxzz: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
@igordanchenko igordanchenko force-pushed the fix/rewrite-relative-module-declarations branch from 96dbcc7 to 90b2fba Compare March 20, 2026 03:42
@igordanchenko igordanchenko force-pushed the fix/rewrite-relative-module-declarations branch from 90b2fba to ca48c6f Compare April 17, 2026 20:43
prisis added a commit to visulima/packem that referenced this pull request Apr 23, 2026
Port upstream v0.23.0..main improvements to avoid identifier collisions
and surface parser failures with actionable errors. Also adds the missing
test coverage for the already-applied relative `declare module` rewriter.

- Wrap parse() calls in fake-js.ts with try/catch so syntax errors in
  declaration files report the failing id instead of a bare babel stack
  (upstream sxzz/rolldown-plugin-dts@f8b8003).
- Prefix namespace import identifiers with `_$` to prevent collisions
  with real identifiers in the bundled .d.ts output
  (upstream sxzz/rolldown-plugin-dts@aeae198).
- Add fixtures + test for cross-chunk `declare module './foo.js'`
  specifier rewriting to exercise the placeholder mechanism already
  present locally (upstream sxzz/rolldown-plugin-dts#191).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
prisis pushed a commit to visulima/packem that referenced this pull request Apr 23, 2026
…n\n## @visulima/rollup-plugin-dts [1.0.0-alpha.12](https://github.com/visulima/packem/compare/@visulima/rollup-plugin-dts@1.0.0-alpha.11...@visulima/rollup-plugin-dts@1.0.0-alpha.12) (2026-04-23)

### Bug Fixes

* **rollup-plugin-dts:** sync fixes from upstream rolldown-plugin-dts ([449794f](449794f)), closes [sxzz/rolldown-plugin-dts#191](sxzz/rolldown-plugin-dts#191)

### Miscellaneous Chores

* **rollup-plugin-dts:** restore isolatedDeclarationTransformer in build config ([46d54f4](46d54f4))
@sxzz sxzz force-pushed the main branch 2 times, most recently from e355f38 to 6affa5a Compare May 1, 2026 11:38
@igordanchenko igordanchenko force-pushed the fix/rewrite-relative-module-declarations branch from ca48c6f to 0642ca6 Compare May 6, 2026 00:45
…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.
@igordanchenko igordanchenko force-pushed the fix/rewrite-relative-module-declarations branch from 0642ca6 to 6008746 Compare May 15, 2026 17:06
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