microsoft / microsoft/TypeScript

Compiler option to define output-to-input directory map for module resolution of package.json `"imports"`/`"exports"`

Aperta
#62,591 0 commenti 0 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
2g 4h
PR unite (30g)
132

Descrizione

🔍 Search Terms

"rootDir", "outDir", "paths", "map", "mapping", "imports", "exports", "moduleResolution", "module resolution"

✅ Viability Checklist
⭐ Suggestion

A TS project with moduleResolution: bundler (and others) uses the rootDir and outDir options to map the project's package.json "imports" and "exports" entries from outDir to rootDir directory. However, this only works for files inside rootDir.

A new option could be introduced for programs to define/inform that another local project compiles rootDir into outDir. This would be similar (indeed, identical in intent) to the paths option, but with more convenient support for package.json's "imports"/"exports" resolution, since only output-to-input directory mappings are required; with paths, one mapping would be required for every "imports" and "exports" entry.

📃 Motivating Example

Consider a project with the following structure:

├──📁 src
├──📁 test
├──📄 package.json
├──📄 tsconfig.json // references the other config files
├──📄 tsconfig.src.json
└──📄 tsconfig.test.json
// tsconfig.src.json
"compilerOptions": {
    "module": "esnext",
    "moduleResolution": "bundler",
    "rootDir": "src",
    "outDir": "dts",
},
"include": ["src"]
// package.json
"imports": {
    "#foo": {
        "types": "./dts/foo.d.ts"
    }
}

With the previous configuration, tsc is able to resolve #foo imports inside src/, even if dts/ has not yet been generated, because of rootDir and outDir.

The desired behavior is for tsconfig.test.json (for the test/ directory) to also be able to resolve #foo imports before dts/ is generated. That is, inform tsconfig.test.json that other program compiles src into dts. This could be achieved with a new compiler option:

// tsconfig.test.json
"<newOptionName>": {
    "dts": "src" // Inform tsc that other program compiles src into dts
}
💻 Use Cases

To resolve package.json's "imports" and "exports" output file paths by mapping them to corresponding input files. It may be seen in the previous example that this would be useful in the same way import specifier maps with paths is useful, but more convenient for "imports"/"exports" resolution.

Alternatives
1. paths

The desired behavior can be achieved with the paths option, but it is not as convenient since a map entry is required for every "imports" and "exports" entry, paths mappings are in a different file, and updating "imports"/"exports" requires a corresponding paths update.

// package.json
"imports": {
    "#foo": {
        "types": "./dts/foo.d.ts"
    },
    "#bar": {
        "types": "./dts/bar.d.ts"
    }
}
// tsconfig.json
"paths": {
    "#foo": ["./src/foo.ts"],
    "#bar": ["./src/bar.ts"]
}

Besides, it seems more appropriate for tsc to resolve "#" and self-reference import specifiers from paths defined by package.json "imports"/"exports" since that is, after all, their purpose.

2. customConditions

A second alternative is using customConditions, but drawbacks are somewhat similar. Although with a custom condition, all map entries can be defined in package.json, more boilerplate is required:

// package.json
"imports": {
    "#foo": {
        "types": {
            "dev": "./src/foo.ts",
            "default": "./dts/foo.d.ts"
        }
    },
    "#bar": {
        "types": {
            "dev": "./src/bar.ts",
            "default": "./dts/bar.d.ts"
        }
    }
}
// tsconfig.json
"customConditions": ["dev"]

This, however, better mirrors the desired behavior; the resolution process with this configuration is very similar to the outDir to rootDir mapping tsc performs (see package.json "imports" and self-name imports).

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

L’issue non nomina file sorgente né test del repository. Inizia riproducendo il comportamento di rootDir/outDir e di "imports" e "exports" di package.json con la configurazione mostrata di tsconfig.src.json e tsconfig.test.json, quindi traccia la gestione esistente di moduleResolution. Il lavoro è completato quando un’opzione del compilatore documentata associa nuovamente la directory di output di un altro progetto ai relativi file di input senza richiedere voci paths corrispondenti.

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

Valutazione

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

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.