question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Declaration maps do not always work

See original GitHub issue
  • Version: 1.3.4
  • Rollup Version: 2.26.10
  • Operating System and version (if applicable): macOS
  • Node Version (if applicable): 14.9.0
  • Does it work with tsc (if applicable): yes

I might be missing something obvious but unfortunately, declaration maps stopped working for me when I switched to this plugin.

Reproduction

Check out https://github.com/vega/vega-tooltip/tree/fee07c92ce6b1914650a0daf881ef403249e6374 and run yarn && yarn build. Then link from another js module and run

import * as tooltip from 'vega-tooltip';

tooltip.formatValue;

Then use VSCode to follow formatValue.

Expected Behavior

I would expect to end up in https://github.com/vega/vega-tooltip/blob/fee07c92ce6b1914650a0daf881ef403249e6374/src/formatValue.ts#L11

Actual Behavior

VSCode opens up the .d.ts file.

I used to compile declarations with tsc and declaration maps worked fine.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
wessbergcommented, Sep 10, 2020

I’ve been thinking about that too, yes. I’m open to the idea of providing an option to opt out of bundling and just fall back to TypeScripts normal declaration mechanism. There are some major gotchas with that approach though!

This plugin not only bundles declarations, but also fully supports code splitting of the declarations and more generally will respect the output structure and filenames produced by Rollup.

TypeScripts normal declaration system expects the source structure to be equivalent to the output structure, and so it is quite easy to end up in a situation where the definitions are placed in a different file than the actual value and then you have the problem of TypeScript suggesting importing from the wrong files and so on. For example, you may instruct Rollup to produce an output structure based on two entry chunks: a (pointing to src/foo.ts) and b (pointing to src/bar.ts). Rollup will then generate two bundles named a and b and split their common dependencies up into shared chunks. Now TypeScript, on the other hand, won’t know anything about neither files called a and b nor any shared chunks, so it will complain when clients of that library attempts to import from either a or b.

So ideally, if we were to allow falling back to the built-in declarations system, we would have to do some work to at the very least rewrite import- and export specifiers to align at least the entry chunks with Rollup.

1reaction
wessbergcommented, Sep 9, 2020

Hey there. As you may know, rollup-plugin-ts bundles declarations, and it does this as a TypeScript Custom Transformer that visits SourceFiles through multiple steps (the major ones being bundling/merging, deconflicting, and tree-shaking).

TypeScript itself then takes care of diffing the source and output ASTs for each SourceFile and applies transformations to the declaration maps that will be emitted.

It is not that declaration maps doesn’t work generally - they do, and we have several tests for them - but unfortunately it is true that they can break quite easily as soon as symbols from various SourceFiles are merged together in one

The primary reason behind it is that for TypeScript to be able to properly trace, say, a Declaration (so it can apply transformations to the declaration maps), it must be able to see a connection between the Declaration that ends up in the merged declaration file all the way back to the place and position where it was originally declared. And, when merging/bundling Nodes from other files, we can’t just reference them, we have to clone them (which is why we use @wessberg/ts-clone-node for that. More info here), so this is where that connection is broken, and TypeScript loses track of that Node.

I would love to find a way to work around this, and intuitively I think a solution will require manually generating declaration maps and ignoring those generated by TypeScript, but that won’t do since other Custom Transformers may be provided that applies further transformations to the bundled declarations, and these changes too should carry over to the declaration maps. I’ll keep this issue open for future reference. And I’m open to suggestions as to how this might be resolved.

For the time being, I recommend disabling declaration maps. Based on what I can see, declaration maps are generally not supported by other declaration bundlers either.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How accurate are US Topo maps, and why don't they have an ...
US Topo maps are as accurate as the data sources used to make them, but because these sources are many and varied, it...
Read more >
Change settings for spoken directions in Maps on iPhone
Turn spoken directions on or off​​ ), then choose an option. All directions are spoken. Only driving alerts are spoken. No directions are...
Read more >
TSConfig Reference - Docs on every TSConfig option
A series of entries which re-map imports to lookup locations relative to the baseUrl . There is a larger coverage of paths in...
Read more >
Map - JavaScript - MDN Web Docs - Mozilla
The keys in Map are ordered in a simple, straightforward way: A Map object iterates entries, keys, and values in the order of...
Read more >
Go maps in action - The Go Programming Language
In this statement, the first value ( i ) is assigned the value stored under the key "route" . If that key doesn't...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found