willmcpo / body-scroll-lock

Body scroll locking that just works with everything 😏
MIT License
4.06k stars 340 forks source link

Add #__PURE__ to improve tree-shaking #209

Open webistomin opened 3 years ago

webistomin commented 3 years ago

Hi! There are some global variables that are not tree-shaked even we didn't use them.

if (typeof window !== 'undefined') {
  var passiveTestOptions = {
    get passive() {
      return undefined;
    }
  };
  window.addEventListener('testPassive', null, passiveTestOptions);
  window.removeEventListener('testPassive', null, passiveTestOptions);
}

var isIosDevice = typeof window !== 'undefined' && window.navigator && window.navigator.platform && (/iP(ad|hone|od)/.test(window.navigator.platform) || window.navigator.platform === 'MacIntel' && window.navigator.maxTouchPoints > 1);

Failed to tree-shake lib\bodyScrollLock.esm.js

This PR adds /*#__PURE__*/ for some expressions to improve the three-shaking. Also, I converted isIosDevice to function to lazily define if the current browser is iOS.

Success! lib\bodyScrollLock.esm.js is fully tree-shakeable

There is some info about library size before this PR and after:

Before

Total
Size limit: 1 KB
Size:       936 B with all dependencies, minified and gzipped

disableBodyScroll
Package size is 59 B less than limit
Size limit: 800 B
Size:       741 B with all dependencies, minified and gzipped

clearAllBodyScrollLocks
Package size is 32 B less than limit
Size limit: 400 B
Size:       368 B with all dependencies, minified and gzipped

enableBodyScroll
Package size limit has exceeded by 3 B
Size limit: 450 B
Size:       453 B with all dependencies, minified and gzipped

After

Total
Size limit: 1 KB
Size:       904 B with all dependencies, minified and gzipped

disableBodyScroll
Package size is 92 B less than limit
Size limit: 800 B
Size:       708 B with all dependencies, minified and gzipped

clearAllBodyScrollLocks
Package size is 66 B less than limit
Size limit: 400 B
Size:       334 B with all dependencies, minified and gzipped

enableBodyScroll
Package size is 33 B less than limit
Size limit: 450 B
Size:       417 B with all dependencies, minified and gzipped

I added a command for size check. You can run it by yourself.

yarn size