Allow `.js` file generated from `.tsx` file to be runnable in browser with less configuration

未关闭
#59,879 5 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
35/100
Issue 类型
功能
描述清晰度
描述清楚
活跃度
停滞

调研方向

使用所示的 tsconfig.json 以及 Foo.tsx、subfolder/Bar.tsx 和 myCustomJSX/jsx-dev-runtime.ts 布局重现该问题。先从 tsc 的 JSX 导入生成和模块解析行为入手,然后验证生成的导入是否为两个输出文件都包含 .js 扩展名和正确的相对路径。

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

描述

Awaiting More Feedback Suggestion
🔍 Search Terms
  • jsx-dev-runtime.js
  • jsx-runtime.js
✅ Viability Checklist
⭐ Suggestion

The .js file generated from .tsx file should have file extension in the injected import statement.
Also, if the value of jsxImportSource is relative (for e.g. ./myCustomJSX instead of myCustomJSX), the import statement should have correct relative path to JSX runtime files.

📃 Motivating Example

Suppose, a web project has following files and folders:

  • /tsconfig.json
  • /myCustomJSX/jsx-dev-runtime.ts
  • /Foo.tsx
  • /subfolder/Bar.tsx
  • /index.html

And, tsconfig.json looks like:

{
  "compilerOptions": {
    "jsx": "react-jsxdev",
    "jsxImportSource": "./myCustomJSX",
  }
}

After running npx tsc, all generated .js files (/Foo.js, /subfolder/Bar.js) have following code at the top:

import { jsxDEV as _jsxDEV, Fragment as _Fragment } from "./myCustomJSX/jsx-dev-runtime";

There are three problems with this:

  1. The .js extension is missing in the import statement.
  2. The path of the module is not accurate for /subfolder/Bar.js
  3. Also, tsc shows following error for /subfolder/Bar.tsx:
    subfolder/Bar.tsx:13:3 - error TS2307: Cannot find module './myCustomJSX/jsx-dev-runtime' or its corresponding type declarations.
    

Expected behavior:

/Foo.js:

import { jsxDEV as _jsxDEV, Fragment as _Fragment } from "./myCustomJSX/jsx-dev-runtime.js";

/subfolder/Bar.js:

import { jsxDEV as _jsxDEV, Fragment as _Fragment } from "../myCustomJSX/jsx-dev-runtime.js";
💻 Use Cases
  1. What do you want to use this for?
    I want to use this for web project that utilizes JSX syntax and uses only TypeScript as build tool.

  2. What shortcomings exist with current approaches?
    The current approach requires too much extra steps when using the generated .js file in web project without additional build steps, especially when using relative path in jsxImportSource.

  3. What workarounds are you using in the meantime?
    I am providing following in importmap in /index.html file:

    <script type="importmap">
    {
      "imports": {
        "./myCustomJSX/jsx-dev-runtime": "./myCustomJSX/jsx-dev-runtime.js"
        "./subfolder/myCustomJSX/jsx-dev-runtime": "./myCustomJSX/jsx-dev-runtime.js"
      }
    }
    </script>
    

    However, the importmap can grow when there are .tsx files in many depths in file system.
    If this issue is solved, there won't be any need for importmap when using relative path in jsxImportSource. However, for bare module path, 1 entry in importmap is still required.

主要语言
Go
星标
111k
派生
14.4k
平均合并
1 天 15 小时
30 天内合并 PR
106

贡献指南

打开贡献指南

从这里开始

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

microsoft/TypeScript 的其他 Issue

查看 microsoft/TypeScript 的全部 Issue

相似的 Issue

更多 Go Issue

把新 issue 发到你的邮箱

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