microsoft / microsoft/TypeScript
Invalid CommonJS output when transforming a single import declaration into multiple
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Go
- Sterne
- 111k
- Forks
- 14.3k
- Ø Merge
- 1 T. 19 Std.
- Gemergte PRs (30 T.)
- 117
Beschreibung
Bug Report
I've created a TypeScript transformer that transforms a single barrel import declaration into multiple deep import declarations.
Eg changing this:
import { A, B } from 'pkg';
console.log(A, B);
Into something like this:
import { A } from 'pkg/a';
import { B } from 'pkg/b';
console.log(A, B);
This works great when emitting ES modules but not when emitting CommonJS.
🔎 Search Terms
Transform, import, esm, cjs
🕗 Version & Regression Information
Tested with 4.9.5 but I suspect that most versions have this behaviour.
⏯ Playground Link
Since Playground doesn't have support for transforms I don't have a playground link.
💻 Code
Not sure what to post here, any custom transform that replaces a single import declaration into multiple behave this way.
🙁 Actual behavior
When calling ts.setOriginalNode(newNode, node) for all new import declarations when emitted CJS code uses the same variable for all statements like this:
var pkg_1 = require('pkg/a');
var pkg_1 = require('pkg/b');
console.log(pkg_1.A, pkg_1.B);
That is, multiple new nodes having the same, single node as their original node.
When not calling ts.setOriginalNode() at all the ES output is fine but the CJS output is even more broken with something like:
var a_1 = require('pkg/a');
var b_1 = require('pkg/b');
console.log(pkg_1.A, pkg_1.B);
🙂 Expected behavior
I'm not sure how this should be handled but I would have liked to be able to connect multiple new nodes to a single original node and have the CommonJS transform see that and use different require variables. With output that looks something like this:
var pkg_1 = require('pkg/a');
var pkg_2 = require('pkg/b');
console.log(pkg_1.A, pkg_2.B);
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Reproduziere den benutzerdefinierten Transformer, der einen Import in mehrere Deklarationen aufteilt, und vergleiche anschließend die Ausgabe für CommonJS und ES-Module, während du nachverfolgst, wie ts.setOriginalNode() die generierten require-Bindings beeinflusst. Fertig ist die Aufgabe, wenn die CommonJS-Ausgabe unterschiedliche require-Variablen verwendet und jede importierte Referenz konsistent aktualisiert, ohne die Ausgabe für ES-Module zu beeinträchtigen.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- typescript
- Bereich
- compilers
- Issue-Typ
- Bug
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Muss geklärt werden
- Anfängerfreundlichkeit
- 35/100