microsoft / microsoft/TypeScript

Explicit module resolution

Abierto
#33,437 3 comentarios 63 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

In Discussion Suggestion
Lenguaje dominante
Go
Estrellas
111k
Forks
14.4k
Merge medio
1 d 19 h
PR fusionados (30 d)
117

Descripción

Search Terms

explicit module resolution

Semi-Related: #11979

Suggestion

Have the ability to specify a pragma/directive that provides a definitive resolution to a module to use at compiler time in place of one that is specified at runtime. During transpilation, the directive could be elided, leaving the module specifier it was overriding untouched.

Use Cases

Specifically in Deno, we don't use any form of "magic" resolution (like Node.js resolution logic), and we include a fully qualified module specifier. This information would change the resolved module for the purposes of the compiler, but not change the runtime module specifier. The logic applied to the module specifier in the pragma/directive could be applied "as normal", it simply would indicate there is a compile time only substitution to be made.

Examples

Currently in Deno we utilise a special directive to accomplish this: @deno-types="...". We parse a source file as the module is being resolved by the TypeScript compiler, parse out any instances of the directive and apply that substitution to any import/export between that point and the next directive or the end of the file.

So to load prettier, we would do something like this:

// @deno-types="./prettier/standalone.d.ts"
import "./prettier/standalone.js";

Or if we load lo-dash:

// @deno-types="https://unpkg.com/@types/lodash@^4.0.0/index.d.ts"
import * as _ from 'https://cdn.pika.dev/lodash-es/v4';

So while we have something that works for us, we think that if solving this problem was done in the TypeScript compiler, it would be something that could benefit a wider community as we go more to situations people might not rely upon Node.js module resolution to resolve the compile time and runtime paths and expect the compiler to magically know what their intent is, but also it would be part of the parse of a source file and therefore TypeScript's understanding of the file, instead of having to "trick" the compiler.

There maybe other ways to solve this than some sort of positional directive, so thoughts, feedback are more than welcome. The intent is "the module specifier says X, which is important at runtime, but TypeScript needs to be told to resolve to Y instead".

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.

Guía de contribución

Abrir la guía de contribución

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Línea de trabajo

Comienza revisando las rutas de resolución de módulos y análisis del código fuente de TypeScript, y compáralas después con el comportamiento existente de la directiva @deno-types descrito en el issue. Define cómo interactuaría una sustitución realizada únicamente en tiempo de compilación con los especificadores de importación y exportación, y establece pruebas que cubran el alcance de la directiva y el especificador de módulo sin cambios en tiempo de ejecución.

Escrito por el modelo de indexación a partir del texto del issue.

Evaluación

Stack tecnológico
deno, typescript
Área
compilers
Tipo de issue
Nueva funcionalidad
Dificultad
5/5
Tiempo estimado
Más de una semana
Estado de actividad
Estancado
Claridad
Necesita aclaración
Aptitud para principiantes
25/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.