Deno: Import map not working with Worker

Created on 7 Jul 2020  Â·  10Comments  Â·  Source: denoland/deno

Steps to reproduce

  1. Create the following files under the same directory:

import_map.json

{
   "imports": {
      "fmt/": "https://deno.land/[email protected]/fmt/"
   }
}

color.ts

import { red } from "fmt/colors.ts";

console.log(red("hello world"));

main.ts

new Worker(new URL('./color.ts', import.meta.url).href, {type: 'module'})
  1. Run this command:
deno run --allow-read --importmap=import_map.json --unstable main.ts

Expected output

hello world

Actual output

error: Uncaught Error: relative import path "fmt/colors.ts" not prefixed with / or ./ or ../ Imported from "file:///color.ts"

Environment

deno 1.1.3
cli feat

Most helpful comment

According to import maps spec they are not affecting workers. So rather than a bug this would be a feature and would be Deno specific. It could be added to non-standard deno option in Worker constructor.

All 10 comments

@ry If I want to fix this which files should I look at?

According to import maps spec they are not affecting workers. So rather than a bug this would be a feature and would be Deno specific. It could be added to non-standard deno option in Worker constructor.

@bartlomieju I see, so is it something like ‘importMap: URL’ option for Worker?

Having import map loading different context of modules could introduce interesting use cases for stuff like A/B testing

@wongjiahau that would be something like:

declare class Worker extends EventTarget {
    constructor(
      specifier: string,
      options?: {
        type?: "classic" | "module";
        name?: string;
        deno?: {
            importMap?: string
        };
      }
    );
  }

@bartlomieju Can the import map represents a remote file? for example

https://deno.land/x/import_map.json

@wongjiahau not at the moment, there is a PR for it (#5849), but I haven't reviewed it yet

@bartlomieju Ok, so importMap can only be either relative or absolute file path right?

@wongjiahau that's correct

@bartlomieju Thanks I will create a PR based on the direction you provided

Was this page helpful?
0 / 5 - 0 ratings

Related issues

xueqingxiao picture xueqingxiao  Â·  3Comments

doutchnugget picture doutchnugget  Â·  3Comments

ry picture ry  Â·  3Comments

kitsonk picture kitsonk  Â·  3Comments

justjavac picture justjavac  Â·  3Comments