html-webpack-scripts-plugin
improves control over scripts generated by Webpack.
Introduction
html-webpack-plugin
will add scripts generated by Webpack into generated HTML like <script type="text/javascript" src="/bundle/vendor.0a78e31b5c440.js"></script>
without need to include them manually. However it won't give you additional control. For example you can't set defer
attribute or make them inline.
This plugin is similar to script-ext-html-webpack-plugin
, however it won't work in conjunction with html-webpack-template
meanwhile html-webpack-scripts-plugin
will.
Installation
npm install html-webpack-plugin --save-dev npm install html-webpack-scripts-plugin --save-dev
Usage
Suppose you have two scripts generated by webpack:
vendor.0a78e31b5c440.js
app.4234fe71c300ea.js
Plugin gives you ability to:
async
defer
id
charset
:
Add specific attributes like // webpack.config.js const HtmlWebpackScriptsPlugin = const HtmlWebpackScriptsPluginInstance = 'defer charset=utf-8': /vendor/ 'async id=appscript': /app/moduleexports = ... plugins: ... HtmlWebpackScriptsPluginInstance ...
Output:
data-*
Add custom attributes like // webpack.config.js const HtmlWebpackScriptsPlugin = const HtmlWebpackScriptsPluginInstance = 'defer data-script-defer=true': /vendor/ 'charset=utf-8 id=appscript inline data-script-inline=true': /app/moduleexports = ... plugins: ... HtmlWebpackScriptsPluginInstance ...
Output:
Make scripts inline:
// vendor.0a78e31b5c440.jsconsole // app.4234fe71c300ea.jsconsole
// webpack.config.js const HtmlWebpackScriptsPlugin = const HtmlWebpackScriptsPluginInstance = inline: /vendor|app/ moduleexports = ... plugins: ... HtmlWebpackScriptsPluginInstance ...
Output:
Template extension:
By default html-webpack-plugin
generates .html file. In case if it generates file with different extension you can specify extension manually:
const HtmlWebpackScriptsPlugin = const HtmlWebpackScriptsPluginInstance = templateExtension: 'ext' inline: /\.js$/