npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

dynamic-polyfiller

v0.0.9

Published

A small, lightweight script to check for native browser feature support - only polyfill when necessary (using the polyfill.io service), no wasted requests on browsers that have native support!

Downloads

3

Readme

HitCount npm

contributions welcome Maintainability

Dynamically polyfill features for a script

A little script that allows you to only polyfill a feature when absolutely necessary - no wasted requests on browsers that have native support! 😆🤓

This script is ~4.2KB unminified (121 lines) (1.04KB unminified and gzipped), ~8.37KB unminified with inline comments (121 lines) (2.12KB unminified and gzipped), or ~1.9KB minified (1 line) (743B minified and gzipped), so it's fairly light. :smile:

Getting Started

See deployment for notes on how to deploy the project on a live system.

Prerequisites

Make sure you know what features your script is reliant on and polyfill those not natively supported on the browsers you support (you can check https://caniuse.com/).

Deployment

Loading via NPM

  1. Run npm i dynamic-polyfiller
  2. Import in your app (example below for React):
import React from 'react';  
import {render} from 'react-dom';  

//The import below is the bit you need!
import dynamicPolyfill from 'dynamic-polyfiller';

dynamicPolyfill(
  ["IntersectionObserver", "Object.assign"],
  'https://cdn.jsdelivr.net/npm/[email protected]/dist/quicklink.umd.js',
  'quicklink();'
);

Loading locally (recommended as is easiest):

  1. Copy the contents of dynamicpolyfill.js
  2. Paste it into your existing JS file(s)
  3. Add a new line after it and call dynamicPolyfill(); Note: Case-sensitive
    1. Example:
    dynamicPolyfill( ["IntersectionObserver", "Object.assign"], 'https://cdn.jsdelivr.net/npm/[email protected]/dist/quicklink.umd.js', 'quicklink();' )
  4. Add an onLoad attribute to the tag calling the dynamicPolyfill() function and passing your parameters
    1. Note: the first parameter is the feature polyfills you want to pass. This is expected as an array.
    2. Note: the second paramter is the URL of the script you want to use. This is expected as a either a string or an array, but can be blank ('') or null if you're not loading a third party script.
    3. Note: the third parameter is the function that you would run once the script has loaded. This is expected as a string or an array .
    4. Note: the 4th parameter has now been deprecated.

Loading from CDN (less recommended):

  1. Add a <script></script> tag linking to this script
    1. Example:
    <script src='https://cdn.jsdelivr.net/gh/willstocks-tech/dynamically-polyfill-features-for-a-script@master/dynamicpolyfill.min.js'>
    </script>
  2. Add an onLoad attribute to the tag calling the dynamicPolyfill() function and passing your parameters
    1. Note: the first parameter is the feature polyfills you want to pass. This is expected as an array.
    2. Note: the second paramter is the URL of the script you want to use. This is expected as a either a string or an array, but can be blank ('') or null if you're not loading a third party script.
    3. Note: the third parameter is the function that you would run once the script has loaded. This is expected as a string or an array .
    4. Note: the 4th parameter has now been deprecated.

Note: Loading from a CDN would still result in a potentially wasted request :disappointed:

Example method of usage

Local
String variables
dynamicPolyfill( 'IntersectionObserver', 'https://cdn.jsdelivr.net/npm/[email protected]/dist/quicklink.umd.js', 'quicklink();' );
Array variables
dynamicPolyfill( ["IntersectionObserver", "Object.assign"], ['https://cdn.jsdelivr.net/npm/[email protected]/dist/quicklink.umd.js', 'https://other.cdn.net/script.js'], ['quicklink();', 'otherFunction();'] );
CDN

Note: You need to ensure that before you call the dynamicPolyfill() function that the actual script itself has loaded. If you're going to host the script yourself (rather than calling out to a CDN), make sure you include the script code first, then call the function. You can do this in the same manner as above, but replace the CDN URL with the path to your own JS file, if you're not going to call it from the same file.

String variables:
<script
	src='https://cdn.jsdelivr.net/gh/willstocks-tech/dynamically-polyfill-features-for-a-script@master/dynamicpolyfill.min.js' 
	onload='dynamicPolyfill( ["IntersectionObserver", "Object.assign"], 'https://cdn.jsdelivr.net/npm/[email protected]/dist/quicklink.umd.js', 'quicklink();')'>
</script>
Array variables:
<script
	src='https://cdn.jsdelivr.net/gh/willstocks-tech/dynamically-polyfill-features-for-a-script@master/dynamicpolyfill.min.js' 
	onload='dynamicPolyfill(["IntersectionObserver", "Object.assign"], ['https://cdn.jsdelivr.net/npm/[email protected]/dist/quicklink.umd.js', 'https://other.cdn.net/script.js'], ['quicklink();', 'otherFunction();'])'>
</script>

Built With

  • Vanilla Javascript - no framework dependencies whatsoever!
  • Polyfill.io - for the actual polyfills!

Versioning

For the versions available, check out the repo release history. I'd recommend sticking to the "latest release" though!

Authors

See also the list of contributors who have participated in this little nugget!

Contributing

Please read CONTRIBUTING.md for details on the code of conduct, and the process for submitting pull requests.

License

This project is licensed under the MIT License - see the LICENSE.md file for details

Acknowledgments