# rollup-plugin-index-html

> Plugin for generating a index html with rollup

Latest version **1.12.8** (published 2020-10-11) · MIT license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install rollup-plugin-index-html
pnpm add rollup-plugin-index-html
yarn add rollup-plugin-index-html
bun add rollup-plugin-index-html
```

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 1.12.8 |
| Published | 2020-10-11 |
| First published | 2019-06-23 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 7 |
| Unpacked size | 38.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 2400 |
| Author | open-wc |
| Maintainers | d4kmor, larsdenbakker |
| Keywords | rollup-plugin, minify, polyfill |

## Links

- npm: https://www.npmjs.com/package/rollup-plugin-index-html
- Repository: https://github.com/open-wc/open-wc
- Homepage: https://github.com/open-wc/open-wc/tree/master/packages/rollup-plugin-index-html
- Issues: https://github.com/open-wc/open-wc/issues
- npm.io page: https://npm.io/package/rollup-plugin-index-html

## Dependencies (7)

- [md5](https://npm.io/package/md5.md) ^2.2.1
- [mkdirp](https://npm.io/package/mkdirp.md) ^0.5.1
- [parse5](https://npm.io/package/parse5.md) ^5.1.1
- [lit-html](https://npm.io/package/lit-html.md) ^1.0.0
- [deepmerge](https://npm.io/package/deepmerge.md) ^4.2.2
- [@import-maps/resolve](https://npm.io/package/@import-maps/resolve.md) ^1.0.0
- [@open-wc/building-utils](https://npm.io/package/@open-wc/building-utils.md) ^2.18.3

## Alternatives

- [raw-loader](https://npm.io/package/raw-loader.md) — 4.3M weekly downloads
- [plop](https://npm.io/package/plop.md) — 1.4M weekly downloads
- [webpack-deadcode-plugin](https://npm.io/package/webpack-deadcode-plugin.md) — 80.3K weekly downloads
- [@storybook/preact-vite](https://npm.io/package/@storybook/preact-vite.md) — 54.2K weekly downloads
- [vite-plugin-transform](https://npm.io/package/vite-plugin-transform.md) — 2.4K weekly downloads

## Recent versions

- 1.12.8 (latest) — 2020-10-11
- 1.12.7 — 2020-09-25
- 1.12.6 — 2020-08-27
- 1.12.5 — 2020-08-19
- 1.12.4 — 2020-05-13
- 1.12.3 — 2020-05-07
- 1.12.2 — 2020-05-07
- 1.11.1 — 2020-04-20
- 1.11.0 — 2020-04-14
- 1.10.6 — 2020-04-12
- 1.10.5 — 2020-04-05
- 1.10.4 — 2020-03-24
- 1.10.3 — 2020-03-11
- 1.10.2 — 2020-03-10
- 1.10.1 — 2020-03-06
- … 46 more at https://npm.io/package/rollup-plugin-index-html/versions

## README

# Rollup Plugin Index HTML

> **WARNING**: This project is deprecated and no longer maintained. See [@open-wc/rollup-plugin-html](https://github.com/open-wc/open-wc/tree/master/packages/rollup-plugin-html) and [@open-wc/rollup-plugin-polyfills-loader](https://github.com/open-wc/open-wc/tree/master/packages/rollup-plugin-polyfills-loader) for replacements.

Rollup plugin to make rollup understand your index.html.

> Part of [Open Web Components](https://github.com/open-wc/open-wc/): guides, tools and libraries for modern web development and web components

[![CircleCI](https://circleci.com/gh/open-wc/open-wc.svg?style=shield)](https://circleci.com/gh/open-wc/open-wc)
[![BrowserStack Status](https://www.browserstack.com/automate/badge.svg?badge_key=M2UrSFVRang2OWNuZXlWSlhVc3FUVlJtTDkxMnp6eGFDb2pNakl4bGxnbz0tLUE5RjhCU0NUT1ZWa0NuQ3MySFFWWnc9PQ==--86f7fac07cdbd01dd2b26ae84dc6c8ca49e45b50)](https://www.browserstack.com/automate/public-build/M2UrSFVRang2OWNuZXlWSlhVc3FUVlJtTDkxMnp6eGFDb2pNakl4bGxnbz0tLUE5RjhCU0NUT1ZWa0NuQ3MySFFWWnc9PQ==--86f7fac07cdbd01dd2b26ae84dc6c8ca49e45b50)
[![Renovate enabled](https://img.shields.io/badge/renovate-enabled-brightgreen.svg)](https://renovatebot.com/)

1. Takes in a standard index.html:

```html
<html lang="en-GB">
  <head>
    <title>My app</title>
    <style>
      my-app {
        display: block;
      }
    </style>
  </head>

  <body>
    <h1>
      <span>
        Hello world!
      </span>
    </h1>
    <my-app></my-app>

    <script>
      (function () {
        var message = 'hello inline script';
        console.log(message);
      })();
    </script>

    <script type="module" src="./app.js"></script>
  </body>
</html>
```

2. Extracts any `<script type="module" src="...">` and feeds them to rollup as entry point(s)

3. Outputs the same index.html with updated file hashes and all inline HTML, CSS and JS minified:

```html
<html lang="en-GB">
  <head>
    <title>My app</title>
    <style>
      my-app {
        display: block;
      }
    </style>
  </head>
  <body>
    <h1><span>Hello world!</span></h1>
    <my-app></my-app>
    <script>
      console.log('hello inline script');
    </script>
    <script src="app.202933f045cc9f6cdf51.js"></script>
  </body>
</html>
```

4. Optionally adds a loader script for conditionally loading polyfills and/or a separate build for older browsers.

Note that only module scripts with a `src` attribute are used as entrypoints, regular scripts and inline modules are minified but not parsed by rollup.

## Usage

To use this plugin, add it to your rollup configuration and set your index.html as entrypoint:

```js
const path = require('path');
const indexHTML = require('rollup-plugin-index-html');

module.exports = {
  input: path.resolve(__dirname, './index.html'),
  plugins: [indexHTML(config)],
};
```

## Configuration

### Polyfills

> Note when using `@open-wc/building-rollup` many polyfills are already configured for you.

Depending on which browser you need to support you may need to polyfill certain browser features. To keep your bundles small, we don't serve any polyfills by default. You can enable polyfills in the configuration.

When enabling polyfills a small loader script is injected to your index.html. Polyfills are loaded based on feature detection. This causes a small delay in loading your app. We mediate this by adding a preload link during the build.

To enable polyfills:

```js
indexHTML({
  polyfills: {
    coreJs: true,
    regeneratorRuntime: true,
    webcomponents: true,
    fetch: true,
    intersectionObserver: true,
  },
});
```

`core-js` polyfills many language features such as `Promise`, `Symbol` and `String.prototype.includes`. `regeneratorRuntime` is necessary when you compile `async await` code which is transpiled to javascript ES5. These two polyfills are mainly for supporting legacy browsers. They are only loaded on browsers which don't support modules, such as IE11.

The rest of the polyfills target specific browser features, see their documentation for more info:

- [core-js](https://github.com/zloirock/core-js)
- [regenerator-runtime](https://github.com/facebook/regenerator/tree/master/packages/regenerator-runtime)
- [webcomponents](https://github.com/webcomponents/webcomponentsjs)
- [fetch](https://github.com/github/fetch)
- [intersection-observer](https://github.com/w3c/IntersectionObserver)

If you need a polyfill which is not on this list, consider creating an issue so that we can add it. You can also specify custom polyfills:

```js
indexHTML({
  polyfills: {
    coreJs: true,
    customPolyfills: [
      {
        // the name of your polyfill
        name: 'my-feature',
        // expression which is run in the browser to determine if the polyfill should be loaded
        test: "'myFeature' in window",
        // path to your polyfill
        path: require.resolve('my-feature-polyfill/dist/bundled.js'),
        // path to the sourcemaps of your polyfill. optional
        sourcemapPath: require.resolve('my-feature-polyfill/dist/bundled.js.map'),
      },
    ],
  },
});
```

### Multi (legacy and modern) build

> Note when using `@open-wc/building-rollup/modern-and-legacy-config` the multi build is already configured for you

If you need to support non-modern browsers, such IE11 or older versions of chrome, safari and firefox, it's better to create multiple builds of your app.

You can make one build for modern browsers using modern syntax and features, and one build for legacy browsers compiled to javascript ES5 and with more polyfills loaded. This way you don't penalize all your users for your lowest browser target.

To create multiple rollup builds, export an array of rollup configs instead of a single config. Set the `multiBuild` option in both instances of the plugin and set `legacy` option in the legacy build:

```javascript
const path = require('path');
const indexHTML = require('rollup-plugin-index-html');

module.exports = [
  {
    entry: path.resolve(__dirname, './index.html'),
    plugins: [
      indexHTML({
        multiBuild: true,
        polyfills: {
          coreJs: true,
          regeneratorRuntime: true,
          webcomponents: true,
        },
      }),
    ],
  },

  {
    entry: path.resolve(__dirname, './index.html'),
    module: {
      rules: [
        // Note: You will probably also want to configure babel for the legacy build.
        // this is not a complete example, you will need to add more configuration for babel
        { test: /\.js/, use: { loader: 'babel-loader' } },
      ],
    },
    plugins: [
      indexHTML({
        multiBuild: true,
        legacy: true,
      }),
    ],
  },
];
```

For the legacy build you do not need to configure any polyfills, as these are already injected by the modern build.

You will probably need to use babel as well to transpile your code to ES5. Remember to change the browser targets for the modern and legacy build accordingly. For example latest 2 of the major browsers for modern and IE11 for the legac build.

### Minification

We use [html-minifier](https://github.com/kangax/html-minifier) for minifcation with a default configuration. You can adjust this configuration by passing a minify object:

```js
indexHTML({
  minify: {
    // minify options
  },
});
```

The options object is passed as is to `html-minifier`. See the documentation of [html-minifier](https://github.com/kangax/html-minifier) for all possible minification options.

It is also possible to turn off minification completely by passing minify:

```js
indexHTML({
  minify: false,
});
```

### Non index.html entrypoints

You can use this plugin without an index.html plugin if you still want to make use of the polyfilling features. You can do this by adding a custom template function:

```js
const path = require('path');
const indexHTML = require('rollup-plugin-index-html');

module.exports = {
  entry: path.resolve(__dirname, './my-app.js'),

  output: {
    filename: '[name].[chunkhash].js',
    chunkFilename: '[name].[chunkhash].js',
  },

  plugins: [
    indexHTML({
      template: ({ assets, entries, legacyEntries, variation }) => `
        <html>
          <head></head>
          <body></body>
        </html>
      `,
    }),
  ],
};
```

### CSP

When loading polyfills we inject a small script in your index.html. If you need CSP you can separate the script in a separate file:

```js
const path = require('path');
const indexHTML = require('rollup-plugin-index-html');

module.exports = {
  entry: path.resolve(__dirname, './my-app.js'),

  output: {
    filename: '[name].[chunkhash].js',
    chunkFilename: '[name].[chunkhash].js',
  },

  plugins: [
    indexHTML({
      polyfills: {
        webcomponents: true,
      },
      loader: 'external',
    }),
  ],
};
```

The template function receives the project's `assets` and `entries`. If applicable it also receives the `legacyEntries` and `variation`.

---
_Source: https://npm.io/package/rollup-plugin-index-html · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
