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

@creative-web-solution/front-polyfill

v1.2.1

Published

Concat and return the needed polyfills for browsers

Downloads

57

Readme

Front polyfill bundle by CWS

This bundle use ES6+ syntax

  • let, const
  • class
  • Set
  • Template literals string

Polyfill list

A sample of the configuration file can be found here: Ressources/frontPolyfill-settings.sample.json.

List

  • Array.findIndex()
  • Array.fill()
  • Array.find()
  • Array.from()
  • Array.includes()
  • Array.of()
  • new Blob()
  • Element.classList
  • Element.closest()
  • Element.append
  • Element.nextElementSibling
  • NodeList.forEach()
  • Element.matches()
  • window.matchMedia()
  • Object.assign()
  • Object.entries()
  • Object.values()
  • window.CSS.paintWorklet()
  • HTMLPictureElement
  • requestAnimationFrame()
  • requestIdleCallback()
  • Promise
  • Promise.finally
  • fetch()
  • URLSearchParams
  • AbortController
  • IntersectionObserver
  • IntersectionObserverEntry.isIntersecting

Configuration

Polyfill tests

Create a configuration based on Ressources/frontPolyfill-settings.sample.json somewhere in your project and set to true the active property of all the polyfill you want to be tested in the browser.

Twig extensions

Add get_front_polyfill_list and get_front_polyfill_content extentions to Twig:

import FrontPolyfill from 'front-polyfill/FrontPolyfill';

function extendTwig( Twig ) {
    // path/to/frontPolyfill.json is created from (a copy of) Ressources/frontPolyfill-settings.sample.json
    const frontPolyfill = new FrontPolyfill( 'path/to/frontPolyfill.json' );

    Twig.extendFunction( 'get_front_polyfill_list',    frontPolyfill.getPolyfillList );
    Twig.extendFunction( 'get_front_polyfill_content', frontPolyfill.getPolyfillContent );
}

Route

Add a route to handle the call.

/!\ The route MUST CONTAINS the :polyfill_list param.

app.get(
    '/path/to/config-:polyfill_list.js',
    function( request, response, next ) {
        response.render( 'my_template_file.html.twig', {
            "polyfill_list": request.params.polyfill_list
        } );
    }
);

Twig template

Insert the polyfills in the response:

{{ get_front_polyfill_content( polyfill_list ) }}
// Other JS stuff...

Javascript support tests

Get the active polyfill list:

With an object list

{% set polyfillList = get_front_polyfill_list() %}

<script>
var myPolyfillArray = [
{%- for name, polyfill in polyfillList.list -%}
    {
        "test": {{ polyfill.test|raw }},
        "name": "{{ name|raw }}"
    }{{ loop.last ? '' : ',' }}
{%- endfor -%}
];
</script>

With a JS array-like string

var myPolyfillArray = {{ get_front_polyfill_list('js') }};

This will give :

var myPolyfillArray = [
    {
        "test": test1,
        "name": "test1"
    },{
        "test": test2,
        "name": "test2"
    },
    ...
];

You can change the name of the properties:

var myPolyfillArray = {{ get_front_polyfill_list('js', 'a', 'b') }};

Results in :

var myPolyfillArray = [{
        "b": test1,
        "a": "test1"
    },{
        "b": test2,
        "a": "test2"
    },
    ...
];

Here a full example to create an url like 'js/polyfill-pf1-pf2-pf3.js':

{% set polyfillArrayString = get_front_polyfill_list('js') %}

<script>
    let polyfillContentUrl;

    {%- if polyfillArrayString is defined and polyfillArrayString|length > 2 -%}
        let neededPolyfill = [];

        {{ polyfillArrayString }}
            .forEach( function( polyfill ) {
                if (typeof polyfill.test === 'function' && polyfill.test() ||
                    typeof polyfill.test !== 'function' && polyfill.test) {
                    neededPolyfill.push( polyfill.name );
                }
            });

        if ( neededPolyfill.length ) {
            polyfillContentUrl = `js/polyfill-${ neededPolyfill.join( '-' ) }.js`;
        }
    {%- endif -%}

    [
        polyfillContentUrl,
        'other-file.js',
        'some-lib.js'
    ].forEach( function( src ) {
        if ( !src ) {
            return;
        }
        var script = document.createElement('script');
        script.src = src;
        script.async = false;
        document.head.appendChild(script);
    } );
</script>

Polyfill loading

Use the array of test to create an url to load the polyfill content.

There is 2 ways to load the polyfills:

The polyfill names are contained in the filename itself and separated by -

This is the recommended way to do it because this way allow you to create a real file. Handy as if the file exists, it will not be rerendered.

<script src="js/polyfill-domch-eachnl-picture.js"></script>

Inside the Twig file that render the response for polyfill-domch-eachnl-picture.js (and save the file if you want):

{{ get_front_polyfill_content( polyfill_list ) }}

// Other stuff in JS

If your router is correctly setup, you should be able to access request.params.polyfill_list which contains all the polyfill to load and will be used by get_front_polyfill_content.

The route to this file MUST contains a placeholder. By default its name is polyfill_list. So, in our example /js/polyfill-domch-eachnl-picture.js, the route must be /js/polyfill-{polyfill_list}.js

When there is a clear cache action, it is recommended to delete those generated files as well.