-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
68 lines (55 loc) · 1.5 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
/**
* @module WebpackCopyOnBuildPlugin
*/
/**
* @constructor
* @param {onBuildCallback} files - an array of objects
* @param files[x].from - string filename. File should exist after webpack is done building.
* @param files[x].to - string. files[x].from will be renamed to this.
*/
function WebpackCopyOnBuildPlugin(files) {
console.log('Started webpack-rename-on-build with parameter ' + JSON.stringify(files));
this.files = files;
}
WebpackCopyOnBuildPlugin.prototype.apply = function(compiler) {
var files = this.files;
compiler.plugin('done', function(stats) {
files.forEach(function(file) {
var to = file.to.replace('[hash]', stats.hash);
var from = file.from.replace('[hash]', stats.hash);
var fs;
if(compiler.outputFileSystem.constructor.name === 'MemoryFileSystem')
{
console.log('Copying from ' + from + ' to ' + to + ' using memory-fs');
fs = compiler.outputFileSystem;
}
else
{
console.log('Copying from ' + from + ' to ' + to + ' using fs');
fs = require('fs');
}
console.log();
var rd = fs.createReadStream(from);
rd.on('error', function (err) {
done(err);
});
var wr = fs.createWriteStream(to);
wr.on('error', function (err) {
done(err);
});
wr.on('close', function (ex) {
done();
});
rd.pipe(wr);
function done(err) {
if (!err) {
console.log('Copied file file ' + from + ' to ' + to);
}
else {
console.log(err);
}
}
});
});
};
module.exports = WebpackCopyOnBuildPlugin;