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

@open-wc/rollup-plugin-polyfills-loader

v1.1.8

Published

Plugin for generating an html file with rollup

Downloads

5,527

Readme


permalink: 'building/rollup-plugin-polyfills-loader.html' title: Rollup Plugin Polyfills Loader section: guides tags:

  • guides

Rollup Plugin Polyfills Loader

Inject Polyfills Loader into a HTML file generated by @open-wc/rollup-plugin-html.

Examples

Basic rollup build

import html from '@open-wc/rollup-plugin-html';
import polyfillsLoader from '@open-wc/rollup-plugin-polyfills-loader';

export default {
  output: {
    dir: 'dist',
  },
  plugins: [
    html({
      inputPath: 'index.html',
      inject: false,
    }),
    polyfillsLoader({
      polyfills: {
        coreJs: true,
        fetch: true,
        webcomponents: true,
      },
    }),
  ],
};

Multiple rollup build outputs

import html from '@open-wc/rollup-plugin-html';
import polyfillsLoader from '@open-wc/rollup-plugin-polyfills-loader';

const htmlPlugin = html({
  inputPath: 'demo/single-build/index.html',
  inject: false,
});

export default {
  output: [
    {
      format: 'system',
      dir: './demo/dist/legacy',
      plugins: [htmlPlugin.addOutput('legacy')],
    },
    {
      format: 'es',
      dir: './demo/dist',
      plugins: [htmlPlugin.addOutput('modern')],
    },
  ],
  plugins: [
    htmlPlugin,
    polyfillsLoader({
      modernOutput: 'modern',
      legacyOutput: { name: 'legacy', test: "!('noModule' in HTMLScriptElement.prototype)" },
      polyfills: {
        coreJs: true,
        fetch: true,
        webcomponents: true,
      },
    }),
  ],
};

Customized file type

import html from '@open-wc/rollup-plugin-html';
import polyfillsLoader from '@open-wc/rollup-plugin-polyfills-loader';

const htmlPlugin = html({
  inputPath: 'demo/single-build/index.html',
  inject: false,
});

export default {
  output: [
    {
      format: 'es',
      dir: './demo/dist',
      plugins: [htmlPlugin.addOutput('modern')],
    },
    {
      format: 'es',
      dir: './demo/dist/legacy',
      plugins: [htmlPlugin.addOutput('legacy')],
    },
  ],
  plugins: [
    htmlPlugin,
    polyfillsLoader({
      modernOutput: { name: 'modern', type: 'module' },
      legacyOutput: {
        name: 'legacy',
        type: 'system',
        test: "!('noModule' in HTMLScriptElement.prototype)",
      },
      polyfills: {
        coreJs: true,
        fetch: true,
        webcomponents: true,
      },
    }),
  ],
};

Multi page build

import html from '@open-wc/rollup-plugin-html';
import polyfillsLoader from '@open-wc/rollup-plugin-polyfills-loader';

export default {
  output: {
    dir: './demo/dist',
  },
  plugins: [
    html({
      inputPath: './demo/multi-page/index.html',
    }),
    polyfillsLoader({
      htmlFileName: 'index.html',
      polyfills: { coreJs: true, fetch: true },
    }),

    html({
      inputPath: './demo/multi-page/pages/page-a.html',
      name: 'pages/page-a.html',
    }),
    polyfillsLoader({
      htmlFileName: 'pages/page-a.html',
      polyfills: { coreJs: true, fetch: true },
    }),

    html({
      inputPath: './demo/multi-page/pages/page-b.html',
      name: 'pages/page-b.html',
    }),
    polyfillsLoader({
      htmlFileName: 'pages/page-b.html',
      polyfills: { coreJs: true, fetch: true },
    }),

    html({
      inputPath: './demo/multi-page/pages/page-c.html',
      name: 'pages/page-c.html',
    }),
    polyfillsLoader({
      htmlFileName: 'pages/page-c.html',
      polyfills: { coreJs: true, fetch: true },
    }),
  ],
};

You can make this shorter with a helper function:

import html from '@open-wc/rollup-plugin-html';
import polyfillsLoader from '@open-wc/rollup-plugin-polyfills-loader';

function createPage(inputPath, name) {
  return [
    html({ inputPath, name }),
    polyfillsLoader({
      htmlFileName: name,
      polyfills: { coreJs: true, fetch: true },
    }),
  ];
}

export default {
  output: {
    dir: './demo/dist',
  },
  plugins: [
    ...createPage('./demo/multi-page/index.html', 'index.html'),
    ...createPage('./demo/multi-page/pages/page-a.html', 'pages/page-a.html'),
    ...createPage('./demo/multi-page/pages/page-b.html', 'pages/page-b.html'),
    ...createPage('./demo/multi-page/pages/page-c.html', 'pages/page-c.html'),
  ],
};

Types

import { PolyfillsConfig, FileType } from 'polyfills-loader';

export interface OutputConfig {
  name: string;
  type?: FileType;
}

export interface LegacyOutputConfig extends OutputConfig {
  test: string;
}

export interface PluginOptions {
  htmlFileName?: string;
  modernOutput?: OutputConfig;
  legacyOutput?: LegacyOutputConfig | LegacyOutputConfig[];
  polyfills?: PolyfillsConfig;
}