microsoft / microsoft/TypeScript

Provide a new, simple 'browser' moduleResolution mode for Browser, ESM-based, non-bundled projects.

未关闭
#62,905 2 条评论 2 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

Awaiting More Feedback Suggestion
主要语言
Go
星标
111k
派生
14.3k
平均合并
2 天 4 小时
30 天内合并 PR
132

描述

🔍 Search Terms

Module resolution, ESM, ES modules, Browser, avoid resolution heuristics.

✅ Viability Checklist
⭐ Suggestion

TLDR: Provide a simple resolution mode that resolves only relative paths and absolute paths explicitly mapped in the tsconfig paths attribute, everyhing else should result in an error, to be used for Browser ESM-based projects.

This is a follow-up to the discussion at https://github.com/microsoft/TypeScript/issues/62206#issuecomment-3657546540 where the resolutionMode='classic' deprecation is discussed.

There is currently no appropriatte moduleResolution mode offered for TypeScript projects that:

  1. Target the Browser as the running environment (meaning not NodeJS) AND
  2. Use ES modules (ESM) AND
  3. Don't use bundling, instead deploy the transpiled ES modules directly.

NodeNext and Bundler are both NodeJS-centric resolution modes, which perform a bunch of NodeJS heuristics to resolve any non-relative paths (for example auto-discovering node_modules folders and package.json files by walking up the folder hierarchy). Moreover, per 'Bundler' mode docs, it resolves import URLs that aren't valid URL in a Browser context (for example when omitting the .js file extension).

The proposed Browser mode should follow as closely as possible the logic used by the Browser to resolve modules at runtime, where it resolves relative/absolute paths, or bare paths specified in Import Maps.

None of the existing resolution modes are appropriate for projects that meet criteria 1-3 listed above. (For reference the Chromium codebase has lot of such TS projects in it, more context here). This seems like a significant gap in TypeScript compiler's offerings since Browser ESM-based projects are very common, at least enough to justify having an appropriate resolution mode, instead of trying to accomodate such projects with Bundler or NodeNext, which seems more of a workaround than a proper solution.

While classic mode was also a bit odd (since it also attempted several heuristics when trying to resolve absolute paths), it kind of worked for this type of projects, and with its deprecation the gap described above is more prominent.

📃 Motivating Example

Implementing this feature would close a gap in TypeScript's moduleResolution oferrings which are currently heavily NodeJS-centric, somewhat under-representing the amount of code that is written for a Browser as the runtime environment where all the resolution heuristics of NodeJS don't apply.

💻 Use Cases
  1. What do you want to use this for?
    Browser, ESM, non-bundled web projects.

  2. What shortcomings exist with current approaches?
    NodeNext and Bundler are not appropriate for projects targeting the Browser as the runtime environment and don't use any bundling.

  3. What workarounds are you using in the meantime?
    Still using Classic mode, while investigating alternatives, see https://issues.chromium.org/issues/423789047 for more context, where we are trying to unblock migrating the Chromium codebase from updating to the upcoming TypeScirpt v6 and v7 versions. Might also look for possibly implementing a custom resolution mode using the programmatic compiler API from https://github.com/microsoft/TypeScript/wiki/Using-the-Compiler-API#customizing-module-resolution, depending on how the effort to unblock Chromium's migration to v6,v7 goes.

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

首先查看现有的 moduleResolution 模式以及 TypeScript 编译器 API 关于自定义模块解析的指南。将 NodeNext、Bundler 和 Classic 的行为与所要求的相对路径、绝对路径和 tsconfig 路径解析规则进行比较。当受支持的 Browser 模式能够在不使用 Node.js 解析启发式的情况下处理所描述的浏览器 ESM、非捆绑使用场景时,即视为完成。

由索引模型根据 Issue 内容生成。

评估

技术栈
typescript
领域
compilers
Issue 类型
功能
难度
5/5
预计耗时
一周以上
活跃度
冷清
描述清晰度
基本清楚
新手友好度
35/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。