microsoft / microsoft/TypeScript

Allow paths to be masked and captured by regex

Offen
#29,245 5 Kommentare 47 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

Suggestion

Sometimes if we use a monorepo we will construct our monopackages like this:

packages
 - a
  - src/
    - index.ts
  - tsconfig.json
  - package.json
 - b
  - src/
    - index.ts
  - tsconfig.json
  - package.json
 - c
  - src/
    - index.ts
    - hello/
      - world.ts
  - tsconfig.json
  - package.json

So when we need to eject it as a git repo it is trivially done. However, I noticed that TypeScript path resolution is pretty limited to just simple replacement, how it is not working will be talked about later below. And I believe my examples are easier than my explanations :(

Scenario

Using babel-plugin-module-resolver, it was easy as pie:

const moduleResolver = [
  'babel-plugin-module-resolver', {
    'root': ['.'],
    'alias': {
      '^@app\/([^\/]+)\/?(.+)?': './packages/\\1/src/\\2'
    }
  }
]

But with the paths option of typescript, which only does simple substitution and is very useless that it could only accommodate one pattern star, is obviously much weaker in expressing power and cannot resolve context dependent subdirectories/submodules.

Examples

Consider this tsconfig

// Trying to use a common pattern to match "@app/{a,b,c}":
"paths": {
  "@app/*": ["packages/*/src"]
}
// Fine, what about "@app/c/hello/world"? It is actually substituted as packages/c/hello/world/src! 
// Doing a brute-force construction:
"paths": {
  "@app/a*": ["packages/a/src", "packages/a/src*"],
  "@app/b*": ["packages/b/src", "packages/b/src*"],
  "@app/c*": ["packages/c/src", "packages/c/src*"],
}
// It works but it wasn't elegant...consider if you have hundreds of monopackages and this would be hell
// Only if we could using a regex like pattern in babel-plugin-module-resolver:
"paths": {
  "^@app\/([^\/]+)\/?(.+)?": ["./packages/\\1/src/\\2"]
}
// Ah, much easier

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.

Similar issues

https://github.com/Microsoft/TypeScript/issues/27298
https://github.com/Microsoft/TypeScript/issues/26787

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

Beginne mit den Beispielen für tsconfig paths im Issue und vergleiche die angeforderten Regex-Captures mit dem aktuellen Verhalten der Pfadsubstitution von TypeScript. Lege fest, wie Regex-Muster und erfasste Pfade mit bestehenden paths-Regeln interagieren sollen, und überprüfe anschließend, dass Monorepo-Imports wie @app/c/hello/world zum vorgesehenen Quellpfad des Pakets aufgelöst werden.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
typescript
Bereich
tooling
Issue-Typ
Feature
Schwierigkeit
5/5
Geschätzter Aufwand
Über eine Woche
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

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