-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
42 lines (35 loc) · 1.21 KB
/
main.js
File metadata and controls
42 lines (35 loc) · 1.21 KB
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
const fs = require('fs');
const path = require('path');
const escape_regexp = /[-[\]{}()*+?.,\\^$|#\s]/g;
const escape_regexp_with = '\\$&';
const defaults = {
input : null,
output : null,
root_path : null,
prefix : '[[[',
suffix : ']]]',
callbacks : {},
};
class InlineFileWebpackPlugin {
constructor(config) {
this.config = Object.assign({}, defaults, config);
}
apply(compiler) {
const input = this.config.input;
const output = this.config.output || input
const root_path = this.config.root_path ? path.resolve(this.config.root_path) : path.dirname(input);
const prefix = this.config.prefix.replace(escape_regexp, escape_regexp_with);
const suffix = this.config.suffix.replace(escape_regexp, escape_regexp_with);
const callbacks = this.config.callbacks || {};
compiler.plugin('done', () => {
fs.writeFileSync(output,
fs.readFileSync(input, {encoding : 'utf8'})
.replace(new RegExp(`${prefix}(.*?)${suffix}`, 'g'), (match, file) => {
const text = fs.readFileSync(path.resolve(`${root_path}/${file}`), {encoding : 'utf8'});
return callbacks[file] ? callbacks[file](text) : text;
})
);
});
}
}
module.exports = InlineFileWebpackPlugin;