forked from astralarya/webpack-archive-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
74 lines (67 loc) · 1.86 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
'use strict';
const path = require('path');
const fs = require('fs');
const archiver = require('archiver');
function WebpackArchivePlugin(options) {
options = options || {};
if(typeof options === 'string') {
this.options = {output: options};
} else {
this.options = options;
}
}
WebpackArchivePlugin.prototype.apply = function(compiler) {
const options = this.options;
compiler.plugin('done', function(compiler) {
// 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}`));
if(options.directory) stream.directory(options.directory, options.directoryRoot || false)
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}`));
if(options.directory) stream.directory(options.directory, options.directoryRoot || false)
streams.push(stream);
}
// Add assets
if(!options.directory) {
for(let asset in compiler.assets) {
if(compiler.assets.hasOwnProperty(asset)) {
for(let stream of streams) {
stream.append(fs.createReadStream(compiler.assets[asset].existsAt), {name: asset});
}
}
}
}
// Finalize streams
for(let stream of streams) {
stream.finalize();
}
});
}
module.exports = WebpackArchivePlugin;