Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

add-asset-html-webpack-plugin

SimenB194.2kMIT6.0.0TypeScript support: included

Add a JS or CSS assets to a generated HTML file

dll, html-webpack-plugin, webpack

readme

add-asset-html-webpack-plugin

Add a JavaScript or CSS asset to the HTML generated by html-webpack-plugin

NPM Version CI Build Status Code Coverage branch

Installation

Install the plugin with npm:

$ npm i add-asset-html-webpack-plugin -D

NOTE: This plugin requires html-webpack-plugin@^3, html-webpack-plugin@^4, or html-webpack-plugin@^5.

Basic Usage

The plugin will add the given JS or CSS file to the files Webpack knows about, and put it into the list of assets html-webpack-plugin injects into the generated html. Add the plugin to your config, providing it a filepath:

const HtmlWebpackPlugin = require('html-webpack-plugin');
const AddAssetHtmlPlugin = require('add-asset-html-webpack-plugin');
const webpackConfig = {
  entry: 'index.js',
  output: {
    path: 'dist',
    filename: 'index_bundle.js',
  },
  plugins: [
    new HtmlWebpackPlugin(),
    new AddAssetHtmlPlugin({ filepath: require.resolve('./some-file') }),
  ],
};

This will add a script tag to the HTML generated by html-webpack-plugin, and look like:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8" />
    <title>Webpack App</title>
  </head>
  <body>
    <script src="index_bundle.js"></script>
    <script src="some-file.js"></script>
  </body>
</html>

NOTE: You can also pass an array of assets to be added. Same API as mentioned below, just pass multiple objects as an array.

new AddAssetHtmlPlugin([
  { filepath: require.resolve('./some-file') },
  { filepath: require.resolve('./some-other-file') },
  // Glob to match all of the dll file, make sure to use forward slashes on Windows
  { glob: require.resolve('./**/*.dll.js') },
]);

Options

Options are passed to the plugin during instantiation.

new AddAssetHtmlPlugin({ filepath: require.resolve('./some-file') });

filepath

Type: string, mandatory unless glob is defined

The absolute path of the file you want to add to the compilation, and resulting HTML file.

glob

Type: string, mandatory unless filepath is defined

A glob used to locate files to add to the compilation. See globby's docs for how to use it.

files

Type: string|Array<string>, default `[]

Files that the assets will be added to.

By default the assets will be included in all files. If files are defined, the assets will only be included in specified file globs.

hash

Type: boolean, default: false

If true, will append a unique hash of the file to the filename. This is useful for cache busting.

includeRelatedFiles

Type: boolean, default: true

If true, will add filepath + '.*' to the compilation as well. E.g filepath.map and filepath.gz.

outputPath

Type: string

If set, will be used as the output directory of the file.

publicPath

Type: string

If set, will be used as the public path of the script or link tag.

typeOfAsset

Type: string, default: js

Can be set to css to create a link-tag instead of a script-tag.

attributes

Type: object, default: {}

Extra attributes to be added to the generated tag. Useful to for instance add nomodule to a polyfill script. The attributes object uses the key as the name of the attribute, and the value as the value of it. If value is simply true no value will be added.

An example of this is included in the repository.

Currently only supports script tags.

Examples

When adding assets, it's added to the start of the array, so when html-webpack-plugin injects the assets, it's before other assets. If you depend on some order for the assets beyond that, and ordering the plugins doesn't cut it, you'll have to create a custom template and add the tags yourself.

Add a DLL file from webpack.DllPlugin

Note: Remember to build the DLL file in a separate build.

See example for an example of how to set it up. You can run it by cloning this repo, running yarn followed by yarn run example.

Webpack config

const path = require('path');
const webpack = require('webpack');
const webpackConfig = {
  entry: {
    vendor: ['react', 'redux', 'react-router'],
  },
  devtool: '#source-map',
  output: {
    path: path.join(__dirname, 'build'),
    filename: '[name].dll.js',
    library: '[name]_[hash]',
  },
  plugins: [
    new webpack.DllPlugin({
      path: path.join(__dirname, 'build', '[name]-manifest.json'),
      name: '[name]_[hash]',
    }),
  ],
};

Your main build:

const path = require('path');
const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const AddAssetHtmlPlugin = require('add-asset-html-webpack-plugin');
const webpackConfig = {
  entry: 'index.js',
  output: {
    path: 'dist',
    filename: 'index_bundle.js',
  },
  plugins: [
    new webpack.DllReferencePlugin({
      context: path.join(__dirname),
      manifest: require('./build/vendor-manifest.json'),
    }),
    new HtmlWebpackPlugin(),
    new AddAssetHtmlPlugin({
      filepath: path.resolve(__dirname, './build/*.dll.js'),
    }),
  ],
};

Add a polyfill file you have locally

See example for an example of how to use it. You can run it by cloning this repo, running yarn followed by yarn run example.

Webpack config

const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const AddAssetHtmlPlugin = require('../../');

const webpackConfig = {
  entry: 'entry.js',
  devtool: '#source-map',
  mode: 'development',
  output: {
    path: 'dist',
    filename: 'index_bundle.js',
  },
  plugins: [
    new HtmlWebpackPlugin(),
    new AddAssetHtmlPlugin({
      filepath: path.resolve(__dirname, './polyfill.js'),
      attributes: {
        nomodule: true,
      },
    }),
  ],
};

changelog

Change Log

All notable changes to this project will be documented in this file. This project adheres to Semantic Versioning.

Latest version of this document will always be available on https://github.com/SimenB/add-asset-html-webpack-plugin/releases

Unreleased

Features

  • Replace includeSourcemap with a more generic includeRelatedFiles which support adding all sort of files, not just source maps

Changed

  • Drop support for Node 4 and html-webpack-plugin@2

2.1.3 - 2018-03-03

Fixes

  • Webpack 4 compatibility

Changed

  • Replace Bluebird with p-each-series
  • Replace minimatch with micromatch

2.1.2 - 2017-09-15

Fixes

  • Fix TypeScript definition file
    • Allows an array of options in the plugin constructor
    • Added files property to options

2.1.1 - 2017-08-16

Fixes

  • Fix typo in readme (filter -> files)

2.1.0 - 2017-08-07

Added

  • Support globby string as filepath option (#78, thanks @Genuifx and @NumerHero)

2.0.1 - 2017-04-23

Fixes

  • Support only string as option to files

2.0.0 - 2017-04-23

Breaking

  • Drop support for node<4

Added

1.0.2 - 2016-08-07

Fixes

  • Fix TypeScript definition file (#22, thanks @hh10k)

1.0.1 - 2016-08-06

Fixes

  • Make require work without .default again (Fixes #20)

1.0.0 - 2016-07-29

Breaking

  • Rename filname to filepath, which makes much more sense

Added

  • A Changelog!
  • A first attempt to add typings
  • Tests for 100% coverage (#17)
  • outputPath option (#16, thanks @wadahiro)

Fixes

  • Fix wrong documentation

Internal

  • Centralise linting

0.4.0 - 2016-07-21

Added

  • publicPath option (#9, thanks @wadahiro)

Internal

  • Lint the code on travis

0.3.0 - 2016-07-15

Added

  • Add hash option to append a hash to the filename (#8, thanks @wadahiro)

Changed

  • Added a note in the readme regarding what version of html-webpack-plugin is needed

Internal

  • Use AirBnB ESLint config instead of Standard

0.2.0 - 2016-05-30

Added

  • Allow passing an array of files to the plugin (#6)

0.1.0 - 2016-05-28

Changed

  • Pass htmlPluginData to the callback (#5, thanks @RyanEwen)

0.0.3 - 2016-03-17

Changed

  • Use html-webpack-plugin-before-html-generation as the event from html-webpack-plugin

0.0.2 - 2016-02-27

Added

  • A Changelog!
  • Use publicPath from webpack config if available

Internal

  • Fix URL to repo in package.json

0.0.1 - 2016-02-27

Initial release