-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathZipDirectoryPlugin.js
More file actions
58 lines (52 loc) · 2.05 KB
/
ZipDirectoryPlugin.js
File metadata and controls
58 lines (52 loc) · 2.05 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
const path = require('path');
const fs = require('fs');
const yazl = require('yazl');
const glob = require('glob');
const { RawSource } = require('webpack-sources');
class ZipDirectoryPlugin {
constructor({directory, zipFileName, outputDir}) {
this.directory = directory;
this.zipFileName = zipFileName;
this.outputDir = outputDir;
}
apply(compiler) {
compiler.hooks.emit.tapAsync(ZipDirectoryPlugin.name, (compilation, callback) => {
// Webpack has child "subprocesses" and parent processes. We only want
// to do this on the parent.
if (compilation.compiler.isChild()) {
callback();
return;
}
const zipfile = new yazl.ZipFile();
// Will store the zip file in binary as it comes out of the output stream
// in segments.
const buffers = [];
// works better than fs.readdir because this will get all files recursively.
glob(path.join(this.directory, '/**/*.*'), (err, files) => {
if (err) {
throw `Unable to read ${this.directory}.`
}
// if directory = /Users/azizj1/proj/build/, then files like
// /Users/azizj1/proj/build/f1.txt and /Users/azizj1/proj/build/public/f2.txt
// need to become f1.txt and public/f2.txt in the zip folder.
for (const f of files) {
zipfile.addFile(f, path.relative(this.directory, f));
}
zipfile.end();
});
zipfile.outputStream.on('data', buff => buffers.push(buff));
zipfile.outputStream.on('end', () => {
const outputPathAndFilename = path.resolve(this.outputDir, this.zipFileName);
// resolve a relative output path with respect to webpack's root output path
// since only relative paths are permitted for keys in `compilation.assets`
const relativeOutputPath = path.relative(
compilation.options.output.path,
outputPathAndFilename,
);
compilation.assets[relativeOutputPath] = new RawSource(Buffer.concat(buffers));
callback();
});
});
}
}
module.exports = ZipDirectoryPlugin;