-
Notifications
You must be signed in to change notification settings - Fork 72
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
Conditional package maps #55
Comments
Seems a little unfortunate if this is a feature that you can only access through legacy scripts forever... |
I believe “legacy” is just an example here; you could call it “use cdn” or “in china” or “on vpn” as well. |
The only problem with this example is that everyone would need to run this feature detection script to get full browser support. Maybe the feature detection logic could be shipped with the package since they are already generating two scripts. <script src="https://unpkg.com/[email protected]/feature-detection.js"></script>
<script>
let legacy = !__lodashglobalDetectFeatures();
packagemapEnvironment.set('legacy', legacy);
</script>
<script type="packagemap">
{
"packages": {
"lodash": {
"legacy": "https://unpkg.com/[email protected]/legacy.js",
"default": "https://unpkg.com/[email protected]/index.js"
}
}
}
</script> Instead of environments, could you dynamically set the packagemap after running feature detection? Something like this: <script src="https://unpkg.com/[email protected]/feature-detection.js"></script>
<script>
let legacy = !__lodashglobalDetectFeatures();
packagemapEnvironment.set('legacy', legacy);
</script>
<script type="packagemap">
if (__lodashglobalDetectFeatures()) {
return ({
"packages": {
"lodash": "https://unpkg.com/[email protected]/index.js"
}
});
} else {
return ({
"packages": {
"lodash": "https://unpkg.com/[email protected]/legacy.js"
}
});
}
</script> |
@ljharb I didn't mean that the name was |
Ah, thanks for clarifying. Perhaps it could still run first with the Module goal if it didn’t have any imports? |
@ljharb Maybe, I dunno if that would cause no-imports modules to be suddenly different timing-wise though. |
I was envisioning only allowing a single no-imports Module to run before the package map to be able to set up these kinds of flags. |
I don't see how that would make it any more or less OK for it to have different timing (though I don't have any strong opinions here). |
Because all other no-imports modules would have unchanged timing - iow, it would just be a special "module that can prime the package map" - we'd probably want to mark it as such so the timing doesn't change by accident. (Definitely no strong opinions here either, just thinking out loud) |
I don't see why this would need first-class support. Could you not just generate alternate import maps depending on the environment? Ideally server-side, but client-side if you must. For example, I would write the OP's example (using new import map syntax) as <script type="importmap">
{
"imports": {
"lodash": "/lodash.js"
}
}
</script>
<script>
if (!featureDetection()) {
const im = document.createElement('script');
im.type = 'importmap';
im.textContent = '{ "imports": { "lodash": "/lodash-legacy-browsers.js" } }';
document.currentScript.after(im);
}
</script> Perhaps the best way to resolve this issue is by incorporating such an example into the README? |
That makes sense. Your current proto-spec explains the timing for how this works well; I guess I missed that before. |
@domenic wasn't it a security feature that only one importmap could exists, and it could not be modified afterwards? |
Is feature detection to dynamically generate an import-map still considered a good solution if you want to point different browsers at packages which only contain syntax that they support? It seems to be the example that is shown in the Is that what issues like "Driving work on a preload spec" and "Solving the waterfall problem with depcache" are looking to fix? /cc @guybedford The other approach that we discussed was using a CDN like Pika by @FredKSchott which uses the (Sorry to post in a closed issue. I felt that if it wouldn't work as intended or would cause difficulties then it is good to put this information where people might find it. And that if it is decided that this is something that should be solved by |
It depends on your use case. If you can only detect things at runtime, then you don't have much choice but to use runtime feature detection. But of course, if you can detect them earlier, at request time using the user agent headers, then that'll be even better. |
The advice makes sense but it's also surprising because for a very long time Taking a read of the However, Mozilla's current position on the standard is that it's "non-harmful" but not their preference or worth prototyping. |
It could be useful to define "environment variables" which conditionally define resolution paths in package maps.
Something like:
The text was updated successfully, but these errors were encountered: