microsoft / microsoft/TypeScript

Allow paths to be masked and captured by regex

Aperta
#29,245 5 commenti 47 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Awaiting More Feedback Suggestion
Lingua principale
Go
Stelle
111k
Fork
14.3k
Merge medio
1g 19h
PR unite (30g)
117

Descrizione

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

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia dagli esempi di tsconfig paths dell'issue e confronta i capture regex richiesti con il comportamento attuale della sostituzione dei percorsi di TypeScript. Definisci come i pattern regex e i percorsi catturati debbano interagire con le regole paths esistenti, quindi verifica che gli import del monorepo come @app/c/hello/world vengano risolti nel percorso del codice sorgente previsto del package.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
typescript
Ambito
tooling
Tipo di issue
Funzionalità
Difficoltà
5/5
Tempo stimato
Più di una settimana
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.