Deno: Dynamic imports should accept dynamic URLs

Created on 3 May 2020  路  8Comments  路  Source: denoland/deno

Dynamic imports is a decent feature for what it is capable of, however, it currently seems to lack the ability to load dynamic URLs. As an example, we cannot import a module from the current working directory in CLI tools:

import(`${Deno.cwd()}/some_file.ts`).then(console.log);

For regular dynamic imports of relative modules, a similar approach with a static string representing a module in a relative directory works:

import("./relative.ts").then(console.log);

Is there something I'm missing?

bug cli

All 8 comments

Dynamic imports is a decent feature for what it is capable of, however, it currently seems to lack the ability to load dynamic URLs.

What error are you getting?

thread 'main' panicked at 'internal error: entered unreachable code', cli\state.rs:452:12
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

@undefinedbuddy This one, I assume? Looks like on Windows it interprets the drive letter as the protocol, whereas on unix it just gets a regular looking absolute path and that works.

@kitsonk Like @nayeemrmn mentioned.

How do I fix this issue in a cross-platform way?

@undefinedbuddy It's an internal bug. You can prepend file:/// to the import specifier for now, seems like it works.

Great! Does it work for all platforms? Can I simply use:

import(`file:///${Deno.cwd()}/some_file.ts`).then(console.log);

On Windows, this would most likely concatenate it with the drive letter as file:///C:\.

5048 is a bug and we could handle it better, but supporting import / actually feels like a side effect.

@undefinedbuddy technically you need to change the \ to / in URIs (https://en.wikipedia.org/wiki/File_URI_scheme#Windows_2). I believe there are some functions in std/path/mod.ts that help with all of this.

Alright, I'll certainly give that a shot!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ry picture ry  路  3Comments

ry picture ry  路  3Comments

zugende picture zugende  路  3Comments

kitsonk picture kitsonk  路  3Comments

motss picture motss  路  3Comments