-
Notifications
You must be signed in to change notification settings - Fork 5
/
plugin.html-beautify.js
35 lines (32 loc) · 1.05 KB
/
plugin.html-beautify.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
const HtmlWebpackPlugin = require('html-webpack-plugin');
const deepMerge = require('lodash.merge');
const beautify = require('js-beautify');
/**
* @typedef { import('webpack').Compiler } Compiler
*/
/**
* `js-beautify` integration with `html-webpack-plugin`
*/
class HtmlBeautifyPlugin {
/**
* @param {object} options - plugin options
*/
constructor(options = {}) {
this.pluginName = 'BeautifyPlugin';
this.options = deepMerge({}, options);
}
/**
* Apply plugin to compliler.
* @param {Compiler} compiler - webpack compiler object
*/
apply(compiler) {
compiler.hooks.compilation.tap(this.pluginName, (compilation) =>
HtmlWebpackPlugin.getHooks(compilation).beforeEmit.tap(this.pluginName, (htmlPluginData) => {
if (htmlPluginData.outputName && htmlPluginData.outputName.endsWith('.html')) {
htmlPluginData.html = beautify.html(htmlPluginData.html, this.options);
}
})
);
}
}
module.exports = HtmlBeautifyPlugin;