-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
99 lines (80 loc) · 2.68 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
'use strict';
const fs = require('fs');
const logger = require('./helpers/logger');
const validBuildTrigger = ['after-emit', 'done'];
module.exports = class ContentReplacerWebpackPlugin {
constructor(options) {
if (ContentReplacerWebpackPlugin.hasValidOptions(options)) {
this.modifications = options.modifications;
this.modifiedFile = options.modifiedFile;
// Optional parameters
this.buildTrigger = options.buildTrigger || 'after-emit';
this.silent = options.silent === true || false;
this.verbose = !this.silent;
this.logLevel = options.logLevel || 'strict';
this.addLogger();
} else {
// Throw exception
const error = typeof options === 'object'
? 'Required parameters are missing'
: 'Parameters are invalid';
this.silent = false;
this.logLevel = 'strict';
this.addLogger();
this.exceptionLogger.warn(error);
throw new Error(error);
}
}
static hasRequiredParameters(options) {
return Object.hasOwnProperty.call(options, 'modifications') &&
Object.hasOwnProperty.call(options, 'modifiedFile');
}
static hasValidOptions(options) {
if (typeof options !== 'object') {
return false;
}
return ContentReplacerWebpackPlugin.hasRequiredParameters(options) &&
(validBuildTrigger.indexOf(options.buildTrigger) >= 0 ||
!Object.hasOwnProperty.call(options, 'buildTrigger')) &&
(Array.isArray(options.modifications) &&
options.modifications.length > 0);
}
addLogger() {
if (!this.silent) {
this.exceptionLogger = logger(this.logLevel);
} else {
// Empty logger when silent mode is activated
this.exceptionLogger = logger();
}
}
replace() {
const that = this;
if (fs.existsSync(this.modifiedFile)) {
[].forEach.call(this.modifications, (modif) => {
const str = fs.readFileSync(that.modifiedFile, 'utf8');
const out = str.replace(new RegExp(modif.regex), modif.modification);
fs.writeFileSync(that.modifiedFile, out);
if (that.verbose) {
const replacementDebug = '\x1B[1m\x1B[34mReplacing in %s: %s => %s\x1B[0m';
// eslint-disable-next-line no-console
console.log(replacementDebug, that.modifiedFile, modif.regex, modif.modification);
}
});
return true;
}
this.exceptionLogger.warn('File not found');
if (this.logLevel === 'strict') {
throw new Error('File not found');
}
return false;
}
apply(compiler) {
const that = this;
compiler.plugin(this.buildTrigger, (compilation, callback) => {
that.replace();
if (callback) {
callback();
}
});
}
};