Skip to content

Proposal: import.meta.resolve (was: import.meta.resolveURL) #3871

@guybedford

Description

@guybedford

Motivation

With import.meta.url support in Module Scripts, common asset loading workflows look something like the following:

const response = await fetch(new URL('../hamsters.jpg', import.meta.url).href);
const worker = new Worker(new URL('./worker.js', import.meta.url).href, { type: 'module' });

With an import.meta.resolveURL function, these workflows can be simplified to:

const response = await fetch(import.meta.resolveURL('../hamsters.jpg'));
const worker = new Worker(import.meta.resolveURL('./worker.js'), { type: 'module' });

much more clearly indicating the intent of these common loading scenarios.

The added benefit of the above is that we bring static analyzability back to these workflows, in that build tools can now statically determine where assets and workers are being resolved and handle rewriting of these kinds of expressions. In this way assets and workers can much more easily be updated to point to optimized resources during builds than requiring build tools to try and analyze custom URL manipulations, which may vary more than the initial examples above and lead to more unreliable results. This would, for example, be useful for us in Rollup to be able to provide these optimizations more easily to users.

Proposal

The proposal is a function of the following form:

resolveURL (url: string): string

Which returns the WhatWG URL normalization applied for url relative to import.meta.url, and returning the fully normalized URL string.

I'd be happy to assist with any spec work here as well.

Feedback welcome!

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