resize-observer icon indicating copy to clipboard operation
resize-observer copied to clipboard

Ponyfill

Open andershansander opened this issue 6 years ago • 5 comments

I am not sure about this but I think that since the index.ts file call the install function immediately, then window.ResizeObserver will always be overwritten, and that that should be avoided by ponyfills.

Feel free to close this if I have misunderstood your code or what ponyfill means. 🙂

andershansander avatar Aug 07 '19 13:08 andershansander

You're right about the index.ts, but the package.json defines lib/ResizeObserver.js (the compiled ResizeObserver.ts) as the main entrypoint:

https://github.com/pelotoncycle/resize-observer/blob/64efdf34dc577286fccb22a531b1b171e902b739/package.json#L5

So importing resize-observer as a module will load it as a ponyfill as described in the readme.

Alternatively, you can load resize-observer as a polyfill by loading dist/resize-observer.js directly in a script tag.

As-is, the only way to accidentally load resize-observer as a polyfill would be to import the index.js file directly by one of the following:

import 'resize-observer/lib';

// or:

import 'resize-observer/lib/index';

At that point, I'd almost like to rename it and call it a feature. We could move/build index.ts to polyfill/index.js so that import 'resize-observer/polyfill' installs resize-observer as a polyfill. Then users don't have to deal with adding another <script> tag to their html.

In fact, that really sounds like something that should be done. I'm going to keep this issue open to track that.

devrelm avatar Oct 02 '19 13:10 devrelm

Oh, I see. I missed that. Sorry for reporting on a non-existing issue.

andershansander avatar Oct 02 '19 14:10 andershansander

@devrelm This means that if the browser already support ResizeObserver we will use the native?

sag1v avatar Aug 18 '22 11:08 sag1v

@sag1v

No. When you use resize-observer you will always get this package's implementation. This package does no checks for a native implementation.

The reason for this is that no matter how hard we try to keep this package in line with the ResizeObserver spec, we have seen some differences pop up here and there. To keep from having strange behavior pop up on some browsers but not others, we decided it would be better to have resize-observer always use its own version.

devrelm avatar Aug 19 '22 14:08 devrelm

Thanks @devrelm 🙏 In our case (a shared library) we want to use the native RO and use a ponyfill/pollyfill in test environment (because jest doesn't support it)

So i wanted to understand if the logic of conditionally using the native already done or should we write it ourselves. I understand that the latter is the case 🙂

sag1v avatar Aug 19 '22 15:08 sag1v