|
| 1 | +# add-asset-html-webpack-plugin |
| 2 | +> Add a JavaScript or CSS asset to the HTML generated by `html-webpack-plugin` |
| 3 | +
|
| 4 | +[![NPM Version][npm-image]][npm-url] |
| 5 | + |
| 6 | +[![Dependency Status][david-image]][david-url] |
| 7 | +[![Dev Dependency Status][david-dev-image]][david-dev-url] |
| 8 | +[![Peer Dependency Status][david-peer-image]][david-peer-url] |
| 9 | + |
| 10 | +## Installation |
| 11 | +Install the plugin with `npm`: |
| 12 | +```sh |
| 13 | +$ npm i add-asset-html-webpack-plugin -D |
| 14 | +``` |
| 15 | + |
| 16 | +## Basic Usage |
| 17 | +The plugin will add the given JS or CSS file to the files Webpack knows about, and put it into the list of assets |
| 18 | +`html-webpack-plugin` injects into the generated html. Add the plugin the your config, providing it a filename: |
| 19 | + |
| 20 | +```js |
| 21 | +var HtmlWebpackPlugin = require('html-webpack-plugin') |
| 22 | +var AddAssetHtmlPlugin = require('add-asset-html-webpack-plugin') |
| 23 | +var webpackConfig = { |
| 24 | + entry: 'index.js', |
| 25 | + output: { |
| 26 | + path: 'dist', |
| 27 | + filename: 'index_bundle.js' |
| 28 | + }, |
| 29 | + plugins: [ |
| 30 | + new HtmlWebpackPlugin(), |
| 31 | + new AddAssetHtmlPlugin({ filename: require.resolve('./some-file') }) |
| 32 | + ] |
| 33 | +} |
| 34 | +``` |
| 35 | + |
| 36 | +This will add a script tag to the HTML generated by `html-webpack-plugin`, and look like: |
| 37 | +```html |
| 38 | +<!DOCTYPE html> |
| 39 | +<html> |
| 40 | + <head> |
| 41 | + <meta charset="UTF-8"> |
| 42 | + <title>Webpack App</title> |
| 43 | + </head> |
| 44 | + <body> |
| 45 | + <script src="index_bundle.js"></script> |
| 46 | + <script src="some-file.js"></script> |
| 47 | + </body> |
| 48 | +</html> |
| 49 | +``` |
| 50 | + |
| 51 | +## Options |
| 52 | +Options are passed to the plugin during instantiation. |
| 53 | + |
| 54 | +```js |
| 55 | +new AddAssetHtmlPlugin({ filename: require.resolve('./some-file') }) |
| 56 | +``` |
| 57 | + |
| 58 | +#### `filename` |
| 59 | +Type: `string`, mandatory |
| 60 | + |
| 61 | +The absolute path of the file you want to add to the compilation, and resulting HTML file. |
| 62 | + |
| 63 | +#### `includeSourcemap` |
| 64 | +Type: `boolean`, default: `false` |
| 65 | + |
| 66 | +If `true`, will add `filename + '.map'` to the compilation as well. |
| 67 | + |
| 68 | +#### `typeOfAsset` |
| 69 | +Type: `string`, default: `js` |
| 70 | + |
| 71 | +Can be set to `css` to create a `link`-tag instead of a `script`-tag. |
| 72 | + |
| 73 | +## Examples |
| 74 | +### Add a DLL file from webpack.DllPlugin |
| 75 | +Note: Remember to build the DLL file in a separate build. |
| 76 | + |
| 77 | +When adding assets, it's added to the start of the array, so when |
| 78 | +`html-webpack-plugin` injects the assets, it's before other assets. If you |
| 79 | +depend on some order for the assets beyond that, you'll have to create some |
| 80 | +sort of intermediate file in the given order. |
| 81 | + |
| 82 | +#### Webpack config |
| 83 | +```js |
| 84 | +var path = require('path') |
| 85 | +var webpack = require('webpack') |
| 86 | +var webpackConfig = { |
| 87 | + entry: { |
| 88 | + vendor: ['react', 'redux', 'react-router'] |
| 89 | + }, |
| 90 | + devtool: '#source-map', |
| 91 | + output: { |
| 92 | + path: path.join(__dirname, 'build'), |
| 93 | + filename: '[name].dll.js', |
| 94 | + library: '[name]_[hash]' |
| 95 | + }, |
| 96 | + plugins: [ |
| 97 | + new webpack.DllPlugin({ |
| 98 | + path: path.join(__dirname, 'build', '[name]-manifest.json'), |
| 99 | + name: '[name]_[hash]' |
| 100 | + }), |
| 101 | + ], |
| 102 | +} |
| 103 | +``` |
| 104 | + |
| 105 | +Your main build: |
| 106 | +```js |
| 107 | +var path = require('path') |
| 108 | +var webpack = require('webpack') |
| 109 | +var HtmlWebpackPlugin = require('html-webpack-plugin') |
| 110 | +var AddAssetHtmlPlugin = require('add-asset-html-webpack-plugin') |
| 111 | +var webpackConfig = { |
| 112 | + entry: 'index.js', |
| 113 | + output: { |
| 114 | + path: 'dist', |
| 115 | + filename: 'index_bundle.js' |
| 116 | + }, |
| 117 | + plugins: [ |
| 118 | + new webpack.DllReferencePlugin({ |
| 119 | + context: path.join(__dirname), |
| 120 | + manifest: require('./build/vendor-manifest.json') |
| 121 | + }), |
| 122 | + new HtmlWebpackPlugin(), |
| 123 | + new AddAssetHtmlPlugin({ |
| 124 | + filename: require.resolve('./build/vendor.dll.js'), |
| 125 | + includeSourcemap: true |
| 126 | + }) |
| 127 | + ] |
| 128 | +} |
| 129 | +``` |
| 130 | + |
| 131 | + |
| 132 | +[npm-url]: https://npmjs.org/package/add-asset-html-webpack-plugin |
| 133 | +[npm-image]: https://img.shields.io/npm/v/add-asset-html-webpack-plugin.svg |
| 134 | +[david-url]: https://david-dm.org/SimenB/add-asset-html-webpack-plugin |
| 135 | +[david-image]: https://img.shields.io/david/SimenB/add-asset-html-webpack-plugin.svg |
| 136 | +[david-dev-url]: https://david-dm.org/SimenB/add-asset-html-webpack-plugin#info=devDependencies |
| 137 | +[david-dev-image]: https://img.shields.io/david/dev/SimenB/add-asset-html-webpack-plugin.svg |
| 138 | +[david-peer-url]: https://david-dm.org/SimenB/add-asset-html-webpack-plugin#info=peerDependencies |
| 139 | +[david-peer-image]: https://img.shields.io/david/peer/SimenB/add-asset-html-webpack-plugin.svg |
0 commit comments