question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add support for URI style import

See original GitHub issue

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/lodash-es@4.17.15/lodash.js";

// it's valid in deno but not in TypeScript
import { serve } from "https://deno.land/std@v0.24.0/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/lodash-es@4.17.15/lodash.js to @types/lodash-es

Map https://deno.land/std@v0.24.0/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:

  • 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.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:96
  • Comments:10 (3 by maintainers)

github_iconTop GitHub Comments

51reactions
NemoSteincommented, Aug 17, 2020

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.

30reactions
niieanicommented, Feb 3, 2022

Node 18 is coming out with support for https:// imports, making this even more pressing.

Read more comments on GitHub >

github_iconTop Results From Across the Web

CSS @import Rule - W3Schools
The @import rule allows you to import a style sheet into another style sheet. The @import rule must be at the top of...
Read more >
import - CSS: Cascading Style Sheets - MDN Web Docs
The @import CSS at-rule is used to import style rules from other valid stylesheets. An @import rule must be defined at the top...
Read more >
Uri | Android Developers
Added in API level 1 ... java.lang.Object. ↳, android.net.Uri ... A URI reference includes a URI and a fragment, the component of the...
Read more >
How to load up CSS files using Javascript? - Stack Overflow
This example checks if the CSS was already added so it adds it only once. ... else { var styles = "@import url('...
Read more >
Style Sheets in HTML documents - W3C
Introduction to style sheets; Adding style to HTML ... HTML 4 provides support for the following style sheet features: ... The value of...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found