microsoft / microsoft/TypeScript

Invalid CommonJS output when transforming a single import declaration into multiple

Aperta
#53,115 2 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Experience Enhancement Suggestion
Lingua principale
Go
Stelle
111k
Fork
14.3k
Merge medio
1g 19h
PR unite (30g)
117

Descrizione

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);

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Riproduci il transformer personalizzato che suddivide un import in più dichiarazioni, quindi confronta l’emissione di CommonJS e degli ES module mentre tracci il modo in cui ts.setOriginalNode() influisce sui binding require generati. Il lavoro è completato quando l’output CommonJS usa variabili require distinte e aggiorna in modo coerente ogni riferimento importato, senza compromettere l’output degli ES module.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
typescript
Ambito
compilers
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Da chiarire
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.