deno: Import map not working with Worker

Steps to reproduce

  1. Create the following files under the same directory:

import_map.json

{
   "imports": {
      "fmt/": "https://deno.land/std@0.55.0/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

About this issue

  • Original URL
  • State: open
  • Created 4 years ago
  • Reactions: 5
  • Comments: 20 (12 by maintainers)

Most upvoted comments

@wongjiahau that would be something like:

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

I guess the spec will take a long time to decide a final solution, and import maps is something very popular and used right now in Deno, so I believe Deno should provide a way to handle this. The importMaps key under deno proposed in https://github.com/denoland/deno/issues/6675#issuecomment-657018192 seems the obvious choice.

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.

No, because we want to continue to align to the web standard. If the web standard is strongly considering establishing a proposal, then we might consider it. Best thing to do is continue to champion something with WHATWG.

Until there is an standardized way for import maps in Workers, ca we please have it under the deno options? ATM this issue throws a wrench into applications designed to work with import maps…

@wongjiahau that’s correct

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