Skip to content
This repository has been archived by the owner on May 1, 2020. It is now read-only.

Proposal: import.meta.urlBase #16

Closed
guybedford opened this issue Mar 28, 2019 · 8 comments
Closed

Proposal: import.meta.urlBase #16

guybedford opened this issue Mar 28, 2019 · 8 comments

Comments

@guybedford
Copy link

Many use cases around import.meta.url are based on relative loading of sibling paths to import.meta.url.

These naturally end up quite unwieldy, for example loading a worker in the same folder:

new Worker(import.meta.url + '/../worker.js', { type: 'module' });

or even:

new Worker(new URL('./worker.js', import.meta.url).href, { type: 'module' });

Just like Node.js supports both __filename and __dirname, if we had cross-platform support for import.meta.urlBase (name to be bikeshed further), then this could be simplified to:

new Worker(`import.meta.urlBase + '/worker.js', { type: 'module' });

This could help in simplifying conventions around these sibling fetch cases.

@domenic
Copy link
Member

domenic commented Mar 28, 2019

I don't think encouraging URL resolution via the + operator is a direction we want to encourage, at least on the web.

@jkrems
Copy link

jkrems commented Mar 28, 2019

Would it be enough if there was an equivalent of import.meta.resolve('./worker.js')? That would also support getting URLs from import maps (import.meta.resolve('image-processor/worker')).

@domenic
Copy link
Member

domenic commented Mar 28, 2019

Yep, I think that's a good direction. However it's unclear whether we can make a sync import.meta.resolve() given import maps with fallbacks :-/. WICG/import-maps#79

Hoping that making more progress on the import maps spec (as opposed to explainer) will make the issues there clearer and unblock progress.

@jkrems
Copy link

jkrems commented Mar 28, 2019

Ah, yes. Forgot about that part. :(

@guybedford
Copy link
Author

@domenic could you perhaps elaborate on why you find issue with string concatenation relative to a parent?

@domenic
Copy link
Member

domenic commented Mar 28, 2019

URL parsing is complicated and any attempts to replace it with just string concatenation are bug farms. For example, depending on whether import.meta.url / import.meta.baseURL ends with a slash or a dot or various other special characters, you would get unexpected results.

@guybedford
Copy link
Author

All APIs that accept URLs will do their own URL parse. Given that import.meta.url / import.meta.baseURL are already known to be valid URLs, we know for a fact that the URL parse will at least get as far as that part of the string concatenation.

If import.meta.baseURL does not end in /, then yes, it will be possible to construct URLs that combine with the directory name, but I don't think that would surprise anyone.

If users start the concatenation part with ../ then they can backtrack below the import.meta.url / import.meta.baseURL, yes, but again that seems a feature not a bug.

What other complexities are there?

@MylesBorins
Copy link
Member

Closing as proposal is now stage 4

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants