Skip to content

Feature request: import(url, {importmap: {...}}) #8877

@szmarczak

Description

@szmarczak
// hello.js
export const hello = (name) => console.log(`Hello ${name}!`);
// test.js
import hello from './hello.js';

hello('world');
// loader.js
const response = await fetch('./test.js');
let payload = await response.text();

// modify payload as necessary

const blob = new Blob([payload], {type: 'application/javascript'});
const url = URL.createObjectURL(blob); // blob:uuid

const module = await import(blob);

URL.revokeObjectURL(url);

globalThis.loaded = module;

I would like to patch a script (e.g. add exports that expose unexported local variables) before actually importing it. Currently it requires the server to handle the patching. Alternatively the loader can replace leading ., .., / in static imports - this wouldn't work for dynamic imports though.

Instead, the line const module = await import(blob); could be replaced with:

const module = await import(blob, {
	importmap: {
		'./': new URL('./', location.href).href
	}
});

I'm not sure if this is the correct repo to report this issue - in that case please instruct me where I should report it instead.

Edit: I just found WICG/import-maps#92 so this issue potentially is a duplicate.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions