microsoft / microsoft/TypeScript

Explicit module resolution

オープン
#33,437 コメント 3 件 リアクション 63 件 担当者 0 名 GitHub で見る

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

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

説明

Search Terms

explicit module resolution

Semi-Related: #11979

Suggestion

Have the ability to specify a pragma/directive that provides a definitive resolution to a module to use at compiler time in place of one that is specified at runtime. During transpilation, the directive could be elided, leaving the module specifier it was overriding untouched.

Use Cases

Specifically in Deno, we don't use any form of "magic" resolution (like Node.js resolution logic), and we include a fully qualified module specifier. This information would change the resolved module for the purposes of the compiler, but not change the runtime module specifier. The logic applied to the module specifier in the pragma/directive could be applied "as normal", it simply would indicate there is a compile time only substitution to be made.

Examples

Currently in Deno we utilise a special directive to accomplish this: @deno-types="...". We parse a source file as the module is being resolved by the TypeScript compiler, parse out any instances of the directive and apply that substitution to any import/export between that point and the next directive or the end of the file.

So to load prettier, we would do something like this:

// @deno-types="./prettier/standalone.d.ts"
import "./prettier/standalone.js";

Or if we load lo-dash:

// @deno-types="https://unpkg.com/@types/lodash@^4.0.0/index.d.ts"
import * as _ from 'https://cdn.pika.dev/lodash-es/v4';

So while we have something that works for us, we think that if solving this problem was done in the TypeScript compiler, it would be something that could benefit a wider community as we go more to situations people might not rely upon Node.js module resolution to resolve the compile time and runtime paths and expect the compiler to magically know what their intent is, but also it would be part of the parse of a source file and therefore TypeScript's understanding of the file, instead of having to "trick" the compiler.

There maybe other ways to solve this than some sort of positional directive, so thoughts, feedback are more than welcome. The intent is "the module specifier says X, which is important at runtime, but TypeScript needs to be told to resolve to Y instead".

Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

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

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

はじめの一歩

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

調査の方向性

まず TypeScript のモジュール解決とソース解析のパスを確認し、次に issue に記載されている既存の @deno-types ディレクティブの動作と比較します。コンパイル時にのみ行われる置換が import および export の指定子とどのように連携するかを定義し、ディレクティブのスコープと、実行時には変更されないモジュール指定子を対象とするテストを整備します。

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

評価

技術スタック
deno, typescript
領域
compilers
issue の種類
機能追加
難易度
5/5
見積もり時間
1週間以上
活発さ
停滞
明瞭さ
説明が足りない
初心者へのやさしさ
25/100

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

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