-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
130 lines (112 loc) · 4.49 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
'use strict';
const fs = require('fs');
const path = require('path')
const minifier = require('html-minifier').minify;
class MinifyHtmlWebpackPlugin {
constructor(options = {}) {
this.options = options
}
prepareRules(replaceRules) {
let rules = [];
replaceRules.forEach((rule) => {
if (Array.isArray(rule.search)) {
rules = rules.concat(rule.search.map((item, index) => ({
search: item,
replace: rule.replace,
index
})));
} else {
rules.push({
search: rule.search,
replace: rule.replace,
index: null
});
}
});
return rules;
}
searchAndReplace(source) {
return this.searchAndReplaceRules.reduce((source, rule) =>
source.replace(
rule.search,
typeof rule.replace === 'string' ? rule.replace : rule.replace(rule.search, rule.index)
),
source
)
}
minifyFiles(srcDir, destDir) {
fs.readdir(srcDir, (err, files) => {
if (err) throw err;
files.forEach(file => {
if (!this.pattern || !this.pattern.test(file)) {
let inputFile = path.resolve(srcDir, file);
if (fs.statSync(inputFile).isDirectory()) {
const directory = path.resolve(destDir, file);
if(!fs.existsSync(directory)) {
fs.mkdirSync(directory);
}
this.minifyFiles(inputFile, directory);
} else {
let source = fs.readFileSync(inputFile, 'utf8');
if (!this.contentPattern || !this.contentPattern.test(source)) {
if (this.searchAndReplaceRules.length > 0) {
source = this.searchAndReplace(source);
}
let result = minifier(source, this.options.rules);
let outputFile = path.resolve(destDir, file);
fs.writeFileSync(outputFile, result);
}
}
}
});
});
}
process() {
if (this.options.verbose) {
console.log('Starting to minimize HTML...')
}
const dir = this.options.dir || this.root;
if (!dir) {
console.error("Either missing base `dir` to find the files or the root of webpack context not provided.")
return;
}
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;
this.searchAndReplaceRules = [];
if (this.options.searchAndReplace && Array.isArray(this.options.searchAndReplace) && this.options.searchAndReplace.length > 0) {
this.searchAndReplaceRules = this.prepareRules(this.options.searchAndReplace);
}
const srcDir = path.resolve(dir, this.options.src);
const destDir = path.resolve(dir, dest);
this.minifyFiles(srcDir, destDir);
}
apply(compiler) {
compiler.hooks.emit.tap('MinifyHtmlWebpackPlugin', compilation => {
const afterBuild = this.options.afterBuild || false;
this.root = compilation.options.context;
if (!afterBuild) {
if (Array.isArray(compilation.errors) && compilation.errors.length > 0) {
return;
}
this.process();
}
});
compiler.hooks.done.tap('MinifyHtmlWebpackPluginAfterBuild', stats => {
const afterBuild = this.options.afterBuild || false;
if (afterBuild) {
if (stats && stats.compilation && Array.isArray(stats.compilation.errors) && stats.compilation.errors.length > 0) {
return;
}
this.process();
}
})
}
}
module.exports = MinifyHtmlWebpackPlugin;