kenchris / urlpattern-polyfill

URLPattern polyfill
https://www.npmjs.com/package/urlpattern-polyfill
MIT License
267 stars 30 forks source link

Polyfill example #47

Closed GrosSacASac closed 2 years ago

GrosSacASac commented 2 years ago

urlpattern.js

if (!globalThis.URLPattern) {
    const URLPatternModule = await import("urlpattern-polyfill/dist/index.umd.js");
    const { URLPattern } = URLPatternModule.default;
    globalThis.URLPattern = URLPattern;
}

main source file

import "./urlpattern.js";
justinfagnani commented 2 years ago

It'd also be great to vend an entrypoint that does the feature detection and global patching for you.

kenchris commented 2 years ago

Yes, my motion sensor polyfill does something like that, e.g. it returns the native implementation if it exists or else the polyfilled version:

export const RelativeOrientationSensor = window.RelativeOrientationSensor ||
class RelativeOrientationSensor extends DeviceOrientationMixin(Sensor, 'deviceorientation') { ... }

https://github.com/kenchris/sensor-polyfills/blob/master/src/motion-sensors.js#L159

Would something like that be useful? What do you think @wanderview ?

abdonrd commented 2 years ago

Any news on this? Thanks!

kenchris commented 2 years ago

I am OK with this, so want to do a PR @abdonrd ?

@justinfagnani I didn't follow what you meant with "vend an entrypoint"?

kenchris commented 2 years ago

I have a tentative solution to this: https://github.com/kenchris/urlpattern-polyfill/pull/71

kenchris commented 2 years ago

This has now been committed, thanks to @justinfagnani for review.

It doesn't use dynamic import, as that seems a user concern. As Justin said it

I personally think that's a bit of a separate concern. You have potentially three things:

  • Implementation
  • Patching the global
  • Dynamic loading the code

A lot of apps will have their own systems for (3) that ensure that app code is run after polyfills load.