Skip to content

Rollup plugin for automatically injecting a script tag with the final bundle into an html file.

License

Notifications You must be signed in to change notification settings

f-rich/rollup-plugin-generate-html-template

 
 

Repository files navigation

rollup-plugin-generate-html-template

build status npm version code coverage

Auto-inject the resulting rollup bundle via script and link tags into an HTML template.

Installation

npm install --save-dev rollup-plugin-generate-html-template

Usage

// rollup.config.js
import htmlTemplate from 'rollup-plugin-generate-html-template';

export default {
  entry: 'src/index.js',
  dest: 'dist/js/bundle.js',
  plugins: [
    htmlTemplate({
      template: 'src/template.html',
      target: 'index.html',
    }),
  ],
};

On final bundle generation the provided template file will have a script tag injected directly above the closing body tag with a link to the js bundle and similarly a link tag above the closing head to the css bundle. By default it uses the same file name and places it directly next to the JS bundle.

<!-- src/index.html -->
<html>
  <head>
    <title>Example</title>
  <head>
<body>
  <canvas id="main"></canvas>
</body>
</html>

<!-- dist/index.html -->
<html>
  <head>
    <title>Example</title>
    <link rel="stylesheet" type="text/css" href="bundle.css">
  <head>
<body>
  <canvas id="main"></canvas>
  <script src="bundle.js"></script>
</body>
</html>

Options

  • template: string (required) The path to the source template.
  • target: string The directory and file name to use for the html file generated with the bundle
  • attrs: string[] The attributes provided to the generated bundle script tag, for example:
    ['async', 'defer'] will generate <script async defer src="bundle.js"></script>
  • replaceVars: {[key: string]: string | function} An object containing variables that will be replaced in the generated html, for example:
    { '__CDN_URL__': process.env.NODE_ENV === 'production' ? 'https://mycdn.com' : '' } will replace all instances of __CDN_URL__ with http://mycdn.com if the environment is production
  • embedContent: bool - If true, rather than generating <script src="..."> tag, it will embed the script content directly inside the HTML

License

MIT

About

Rollup plugin for automatically injecting a script tag with the final bundle into an html file.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • HTML 93.5%
  • JavaScript 5.4%
  • CSS 1.1%