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.
ProposalThe 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!
Rich-Harris, shellscape, dcleao, matthewp, elektronik2k5 and 4 more
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4