microsoft / microsoft/TypeScript

Invalid CommonJS output when transforming a single import declaration into multiple

オープン
#53,115 コメント 2 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

Experience Enhancement Suggestion
主要言語
Go
スター
111k
フォーク
14.4k
平均マージ
1日 19時間
マージ済み PR(30日)
117

説明

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

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

1つの import を複数の宣言に分割するカスタム transformer を再現し、その後、ts.setOriginalNode() が生成された require バインディングにどのような影響を与えるかを追跡しながら、CommonJS と ES module の出力を比較します。CommonJS の出力が個別の require 変数を使用し、各インポート参照を一貫して更新しつつ、ES module の出力を壊さなければ完了です。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
typescript
領域
compilers
issue の種類
バグ
難易度
4/5
見積もり時間
3〜5日
活発さ
停滞
明瞭さ
説明が足りない
初心者へのやさしさ
35/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。