Typescript: Add support for URI style import

Created on 18 Dec 2019  ·  4Comments  ·  Source: microsoft/TypeScript

Search Terms

browser es module import url

Suggestion

Related issues: #28985, #19942

In browser and deno, import from a "package" is different from the node style.

// it's valid in browser but not in TypeScript
import lodash from "https://unpkg.com/[email protected]/lodash.js";

// it's valid in deno but not in TypeScript
import { serve } from "https://deno.land/[email protected]/http/server.ts";

Currently there is no way to let TypeScript automatically map the URI to another place like @types/* or $DENO_DIR/deps/https/deno.land/*

The current path can map a simple pattern of import module specifier to another place, but in the URI style import, a more flexible way to map the URI is required.

Proposal

(maybe add a new moduleResolution: browser)
Add a new uriPaths that allows to map from a RegExp to a local path. It will NOT effect the emitted code. Just a way to find the type definition for those URIs.

I'm willing to implement this feature but I'm not sure if TypeScript will accept this.

Use Cases

For TypeScript to find definition file for imports like https://unpkg.com/lodash

Examples

{
  "compilerOptions": {
    "uriPaths": {
      "https://unpkg.com/(.+?)@.+?/.+": "./node_modules/@types/$1",
      "https://deno.land/(.+?)@v.+?/(.+?)/(.+)": "$DENO_DIR/deps/https/deno.land/$1/$2/$3",
      "std:(.+)": "./node_modules/builtin-module-types/$1"
    }
  }
}

This rule map https://unpkg.com/[email protected]/lodash.js to @types/lodash-es

Map https://deno.land/[email protected]/http/server.ts to $DENO_DIR/deps/https/deno.land/std/http/server.ts.

$DENO_DIR is an environment variable.
By default, on Windows, it's ~\AppData\Local\deno\deps\https\deno.land\std\http\server.ts.
By default on Linux, it is ~/.deno/deps/https/deno.land/std/http/server.ts.

Checklist

My suggestion meets these guidelines:

  • [x] This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • [x] This wouldn't change the runtime behavior of existing JavaScript code
  • [x] This could be implemented without emitting different JS based on the types of the expressions
  • [x] This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • [x] This feature would agree with the rest of TypeScript's Design Goals.
In Discussion Suggestion

Most helpful comment

Please, don't let this issue die.

VSCode depends on TS to resolve JS modules definitions.
The ability to import a package from a remote server and still have access to intellisense is essential to modern day web development.

All 4 comments

Related: https://github.com/microsoft/TypeScript/issues/35589

If you find yourself trying to write a valid runtime path, and can't get TypeScript to understand how to resolve that path to the target module, tell us about it.
-- From @RyanCavanaugh


Hi @xlaywan, @fatcerberus, @MicahZoltu, @andrewbranch
Please move from #35163 to here!


Hi @vladima you implemented the current compilerOptions.paths feature in the TypeScript, how do you think about this issue?

Please, don't let this issue die.

VSCode depends on TS to resolve JS modules definitions.
The ability to import a package from a remote server and still have access to intellisense is essential to modern day web development.

This issue seems to focus on loading / using HTTP URLs. I created a more targeted issue to just support import by URL in general, without supporting module locations beyond what TS already supports today (https://github.com/microsoft/TypeScript/issues/41730).

Also, Node v14.13.1+ supports the node: prefix for importing built‑in modules.

Was this page helpful?
0 / 5 - 0 ratings