microsoft / microsoft/TypeScript
Invalid CommonJS output when transforming a single import declaration into multiple
Nadie ha tomado este issue todavía.
- Lenguaje dominante
- Go
- Estrellas
- 111k
- Forks
- 14.4k
- Merge medio
- 1 d 19 h
- PR fusionados (30 d)
- 117
Descripción
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);
Guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Línea de trabajo
Reproduce el transformer personalizado que divide un import en varias declaraciones y, después, compara la emisión de CommonJS y ES module mientras rastreas cómo ts.setOriginalNode() afecta a los bindings de require generados. La tarea está terminada cuando la salida de CommonJS usa variables require distintas y actualiza cada referencia importada de forma coherente, sin romper la salida de ES module.
Escrito por el modelo de indexación a partir del texto del issue.
Evaluación
- Stack tecnológico
- typescript
- Área
- compilers
- Tipo de issue
- Error
- Dificultad
- 4/5
- Tiempo estimado
- 3-5 días
- Estado de actividad
- Estancado
- Claridad
- Necesita aclaración
- Aptitud para principiantes
- 35/100