-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Better static resolution for dynamic/partial specifiers #5
Comments
@jamiebuilds I'm not entirely sure if this is needed on the first iteration. I know I would like to be able to setup relative locations of assets dynamically. Globs get into territory like url parsing potentially with things like A tool could compile these glob patterns into a series of individual patterns potentially though. asset translations from './translations/*.json';
fetch(translations['en']); becomes let translations = {};
asset en from './translations/en.json';
translations.en = en;
asset zh from './translations/zh.json';
translations.zh = en;
fetch(translations['en']); I think the ability to have multiple paths makes it a bit more plumbing than just a simple reference type, which is part of why I want to put it off. If we could support globs we should probably figure out if that list of internal mappings is immutable/static or dynamic. Using a function like you show above makes me think you want it to be dynamic, but I might be wrong. |
The reason I think this is important to consider is that this is by far the more problematic scenario in the ecosystem, and the design of this proposal should lend itself to being extended for this purpose. I'm not suggesting that globs are the perfect solution, but they are much better than regex (regexes against URLs are awful to write) I don't think creating a key map is necessarily the best thing to do either, but if they were to be used, I'd give the fully qualified specifier:
|
I used a function because I don't think it needs to do anything more than asset the path you pass through matches the original glob/specifier Another reason we wouldn't want to use a map of matched paths is that there would be no efficient way to discover them all at runtime |
@jamiebuilds one of the points of this proposal is to allow eager fetching/ahead of time tooling like is done with bundlers. How could we do that if we don't make a map of possible things to load?
I'm not sure I understand this test function. If you just pass in specifiers, you wouldn't need to iterate and discover them all. Wouldn't the same be true for mappings? If we only expose a query interface to a map that returns true or false if it exists in the map it would also not need to do runtime discovery. |
It would be easy for a bundler to translate
I know, but I'm more interested in these motivations for this particular issue:
The only purpose of the test function is to have early errors at runtime when requesting unexpected paths. Otherwise the runtime doesn't necessarily align with what we expect statically. I would like to be able to ban code from specifying dependencies in ways that don't have both static and runtime guarantees that // Good: Will throw error at runtime if runtime string doesn't match glob
asset translations from './translations/*.json';
fetch(translations(`./translations/${lang}.json`));
// Bad: Could silently not match glob
asset translations from './translations/*.json';
fetch(`./translations/${lang}.json`);
// Bad: Could silently not match glob
fetch(`./translations/${lang}.json`); |
A good alternative to globs would be to use parameters: asset translations from './translations/$0/$1.json';
await import(translations('en', 'dashboard')) Edit: Although, you'd still need to decide what is valid for matching against that... |
Another option being standardized elsewhere is https://github.com/WICG/urlpattern asset translations from "./translations/:lang/:category.json"
await import(translations(`./translations/${lang}/${category}.json`)) |
Writing imports like this are super common in the ecosystem:
Tools like Webpack support this by turning the expression into a regex matcher like:
/\.\/translations\/.*
Which then pulls in every single file inside the
./translations
directory (including any sub-directories) into the JavaScript bundle.I've seen cases where people have accidentally added 10s of MBs of code to their JavaScript bundles by accident not understanding this behavior.
Not to mention all the complexities of having to support statically analyzing syntax like this. It's just outright no supported by many tools, and the ones that do support it have subtlety different behaviors.
In the past I suggested having something like:
Which would get desugared to:
Obviously globs are not something that is part of the Web or JavaScript at all (although they are the best solution I know of to this problem).
But if globs are totally unacceptable, maybe we can try giving some sort of better primitive for this?
The text was updated successfully, but these errors were encountered: