microsoft / microsoft/TypeScript

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

Offen
#59,879 5 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Awaiting More Feedback Suggestion
Vorherrschende Sprache
Go
Sterne
111k
Forks
14.4k
Ø Merge
1 T. 19 Std.
Gemergte PRs (30 T.)
117

Beschreibung

🔍 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.

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Reproduziere das Problem mit der gezeigten tsconfig.json und dem Layout aus Foo.tsx, subfolder/Bar.tsx und myCustomJSX/jsx-dev-runtime.ts. Beginne mit der JSX-Importausgabe und dem Modulauflösungsverhalten von tsc und überprüfe anschließend, dass die generierten Importe für beide Ausgabedateien die Erweiterung .js und die korrekten relativen Pfade enthalten.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
javascript, typescript
Bereich
build-system, compilers
Issue-Typ
Feature
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Klar beschrieben
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.