forked from otelnov/replace-webpack-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
99 lines (86 loc) · 2.83 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
var path = require('path');
var fs = require('fs');
const minifier = require('html-minifier').minify,
webpackSources = require('webpack-sources');
var regex = /(\n?)([ \t]*)(<!--\s*replace:(\w+(?:-\w+)*)\s*-->)\n?([\s\S]*?)\n?(<!--\s*endreplace\s*-->)\n?/ig;
function regexMatchAll(string, regexp) {
var matches = [];
string.replace(regexp, function () {
var arr = Array.prototype.slice.call(arguments);
matches.push(arr);
});
return matches;
}
function minify(content) {
content = content instanceof Buffer ? content.toString('utf8') : content;
try {
return minifier(content, {
collapseBooleanAttributes: true,
collapseWhitespace: true,
decodeEntities: true,
minifyCSS: true,
minifyJS: true,
removeAttributeQuotes: true,
removeComments: true,
removeScriptTypeAttributes: true,
removeStyleLinkTypeAttributes: true
});
} catch (e) {
console.warn(path);
return content;
}
};
function Replace(config) {
this.skip = config.skip || false;
this.entry = config.entry;
this.output = config.output;
this.data = config.data;
this.hash = config.hash;
this.hashValue = config.hashValue;
this.replaceWithAssets = config.replaceWithAssets || [];
this.isMin = config.min;
}
Replace.prototype.apply = function (compiler) {
var self = this;
var folder = compiler.options.context;
var entry = path.join(folder, self.entry);
let fileName = this.output || path.basename(this.entry);
compiler.plugin('compilation', (compilation) => {
compilation.plugin('additional-assets', (callback) => {
try {
let data = fs.readFileSync(entry, 'utf8');
if (!self.skip) {
var matches = regexMatchAll(data, regex);
matches.forEach(function (match) {
var str = match[0];
var key = match[4];
if (key in self.data) data = data.replace(str, '\n' + self.data[key] + '\n');
});
}
if (self.hash) {
var changeWith = typeof self.hashValue === 'string' ? self.hashValue : compilation.hash,
assetsKeys = Object.keys(compilation.assets);
self.replaceWithAssets.forEach(it => {
let reg;
for(let i = assetsKeys.length; i--;){
reg = new RegExp(it.assetReg);
if(reg.test(assetsKeys[i])){
reg = new RegExp('\\' + it.search, 'g');
data = data.replace(reg, assetsKeys[i]);
break;
}
}
});
var reg = new RegExp('\\' + self.hash, 'g');
data = data.replace(reg, changeWith);
}
if (self.isMin) data = minify(data);
compilation.assets[fileName] = new webpackSources.RawSource(data);
} catch (err) {
console.error(err);
}
callback();
});
});
};
module.exports = Replace;