forked from ashutoshSce/minify-html-webpack-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
62 lines (51 loc) · 2.16 KB
/
index.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
'use strict';
const fs = require('fs');
const path = require('path')
const minifier = require('html-minifier').minify;
class MinifyHtmlWebpackPlugin {
constructor(options = {}) {
this.options = options
}
minfifyFiles(srcDir, destDir) {
fs.readdir(srcDir, (err, files) => {
if (err) throw err;
files.forEach(file => {
if (!this.pattern.test(file)) {
let inputFile = path.resolve(srcDir, file);
if (fs.statSync(inputFile).isDirectory()) {
this.minfifyFiles(inputFile, path.resolve(destDir, file));
} else {
let source = fs.readFileSync(inputFile, 'utf8');
if (!this.contentPattern.test(source)) {
let result = minifier(source, this.options.rules);
let outputFile = path.resolve(destDir, file);
fs.writeFileSync(outputFile, result);
}
}
}
});
});
}
apply(compiler) {
compiler.hooks.emit.tap('MinifyHtmlWebpackPlugin', compilation => {
if (this.options.verbose) {
console.log('Starting to minimize HTML...')
}
const root = compilation.options.context;
const dir = this.options.dir || root;
if (!this.options.src) {
throw new Error('`src` is missing from the options.')
}
if (!this.options.dest) {
console.warn('Warning... Original source code will be minified and overwritten, because `dest` is missing from the options.');
}
const dest = this.options.dest || this.options.src;
this.pattern = this.options.ignoreFileNameRegex || /''/;
this.contentPattern = this.options.ignoreFileContentsRegex || /''/;
const srcDir = path.resolve(dir, this.options.src);
const destDir = path.resolve(dir, dest);
this.minfifyFiles(srcDir, destDir);
})
}
}
module.exports = MinifyHtmlWebpackPlugin;