-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
104 lines (82 loc) · 3.34 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/**
* @author Erik Desjardins
* See LICENSE file in root directory for full license.
*/
'use strict';
var path = require('path');
var ModuleFilenameHelpers = require('webpack/lib/ModuleFilenameHelpers');
var RawSource = require('webpack-sources').RawSource;
var yazl = require('yazl');
function ZipPlugin(options) {
this.options = options || {};
}
ZipPlugin.prototype.apply = function(compiler) {
var options = this.options;
if (options.pathPrefix && path.isAbsolute(options.pathPrefix)) {
throw new Error('"pathPrefix" must be a relative path');
}
(
compiler.hooks
? compiler.hooks.emit.tapAsync.bind(compiler.hooks.emit, ZipPlugin.name)
: compiler.plugin.bind(compiler, 'emit')
)(function(compilation, callback) {
// assets from child compilers will be included in the parent
// so we should not run in child compilers
var isChild = compilation.compiler && compilation.compiler.isChild
? compilation.compiler.isChild()
: this.isChild()
if (isChild) {
callback();
return;
}
var zipFile = new yazl.ZipFile();
var pathPrefix = options.pathPrefix || '';
var beforeAddBuffer = options.beforeAddBuffer || function() {};
var pathMapper = options.pathMapper || function(x) { return x; };
// populate the zip file with each asset
for (var nameAndPath in compilation.assets) {
if (!compilation.assets.hasOwnProperty(nameAndPath)) continue;
// match against include and exclude, which may be strings, regexes, arrays of the previous or omitted
if (!ModuleFilenameHelpers.matchObject({ include: options.include, exclude: options.exclude }, nameAndPath)) continue;
var source = compilation.assets[nameAndPath].source();
var newPath = pathMapper(nameAndPath);
if (/now_config\.json/.test(newPath)) {
newPath = 'now_config.json';
}
zipFile.addBuffer(
Buffer.isBuffer(source) ? source : new Buffer(beforeAddBuffer(source, nameAndPath)),
path.join(pathPrefix, newPath.replace(/\?.*/, '')),
options.fileOptions
);
}
zipFile.end(options.zipOptions);
// accumulate each buffer containing a part of the zip file
var bufs = [];
zipFile.outputStream.on('data', function(buf) {
bufs.push(buf);
});
zipFile.outputStream.on('end', function() {
// default to webpack's root output path if no path provided
var outputPath = options.path || compilation.options.output.path;
// default to webpack root filename if no filename provided, else the basename of the output path
var outputFilename = options.filename || compilation.options.output.filename || path.basename(outputPath);
var extension = '.' + (options.extension || 'zip');
// combine the output path and filename
var outputPathAndFilename = path.resolve(
compilation.options.output.path, // ...supporting both absolute and relative paths
outputPath,
path.basename(outputFilename, '.zip') + extension // ...and filenames with and without a .zip extension
);
// resolve a relative output path with respect to webpack's root output path
// since only relative paths are permitted for keys in `compilation.assets`
var relativeOutputPath = path.relative(
compilation.options.output.path,
outputPathAndFilename
);
// add our zip file to the assets
compilation.assets[relativeOutputPath] = new RawSource(Buffer.concat(bufs));
callback();
});
});
};
module.exports = ZipPlugin;