-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
67 lines (60 loc) · 1.96 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
'use strict';
const fs = require('fs');
const archiver = require('archiver');
class WebpackFilesArchivePlugin {
constructor(options = {}) {
this.options = options;
}
// Define `apply` as its prototype method which is supplied with compiler as its argument
apply(compiler) {
// Specify the event hook to attach to
compiler.hooks.afterEmit.tap('WebpackFilesArchivePlugin', compilation => {
const options = this.options;
// Set output location
const output = options.output ? options.output : compiler.options.output.path;
// Create archive streams
let streams = [];
let zip = true;
let tar = true;
if (options.format) {
if (typeof options.format === 'string') {
zip = (options.format === 'zip');
tar = (options.format === 'tar');
} else if (Array.isArray(options.format)) {
zip = (options.format.indexOf('zip') !== -1);
tar = (options.format.indexOf('tar') !== -1);
}
}
if (zip) {
const ext = options.ext || 'zip';
let stream = archiver('zip');
stream.pipe(fs.createWriteStream(`${output}.${ext}`));
streams.push(stream);
}
if (tar) {
const ext = options.ext || 'tar.gz';
let stream = archiver('tar', {
gzip: true,
gzipOptions: {
level: 1
}
});
stream.pipe(fs.createWriteStream(`${output}.${ext}`));
streams.push(stream);
}
// Add assets
for (let asset in compilation.assets) {
if (compilation.assets.hasOwnProperty(asset)) {
for (let stream of streams) {
stream.append(fs.createReadStream(`${output}/${asset}`), { name: asset });
}
}
}
// Finalize streams
for (let stream of streams) {
stream.finalize();
}
});
}
}
module.exports = WebpackFilesArchivePlugin;