一覧へ戻る
Example of rollup-plugin-polyfill with resizeObserver
- star 数
- 2
- watcher 数
- 1
- fork 数
- 0
- issue 数
- 1
- 最終更新
- 2021/08/12
Example of rollup-plugin-polyfill with resizeObserver
Example of rollup-plugin-polyfill with resizeObserver Polyfill.
An example of using ResizeObserver:
const div = document.getElementById('myDIV'); div.innerText = 'My default width is ' + div.offsetWidth; const resizeObserver = new ResizeObserver(entries => { console.log('Something change...'); div.innerText = 'My new width is ' + entries[0].contentRect.width; }); resizeObserver.observe(div);
If I use rollup-plugin-polyfill with resize-observer-polyfill, it changes the function ResizeObserver to ResizeObserver$1. I think it is conflicting with new ResizeObserver .
const polyfill = require('rollup-plugin-polyfill'); const plugins = [ polyfill(['resize-observer-polyfill']), ]
So, I created a wrapper to put resize-observer-polyfill in global.
import ResizeObserver from 'resize-observer-polyfill'; //Finding the global variable. const myGlobal = (function () { if (typeof global !== 'undefined' && global.Math === Math) { return global; } if (typeof self !== 'undefined' && self.Math === Math) { return self; } if (typeof window !== 'undefined' && window.Math === Math) { return window; } })(); //If ResizeObserver doesn't exist, set the Polyfill. if(!myGlobal.ResizeObserver) { myGlobal.ResizeObserver = ResizeObserver; }
npm installnpm run build-iifenpm run build-commonjsnpm run build-umd