forked from markshapiro/webpack-merge-and-include-globally
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.node6-compatible.js
141 lines (126 loc) · 4.98 KB
/
index.node6-compatible.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
'use strict';
function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; }
const fs = require('fs');
const glob = require('glob');
const { promisify } = require('es6-promisify');
const revHash = require('rev-hash');
const readFile = promisify(fs.readFile);
const listFiles = promisify(glob);
const joinContent = (() => {
var _ref = _asyncToGenerator(function* (promises, separator) {
return promises.reduce((() => {
var _ref2 = _asyncToGenerator(function* (acc, curr) {
return `${yield acc}${(yield acc).length ? separator : ''}${yield curr}`;
});
return function (_x3, _x4) {
return _ref2.apply(this, arguments);
};
})(), '');
});
return function joinContent(_x, _x2) {
return _ref.apply(this, arguments);
};
})();
class MergeIntoFile {
constructor(options, onComplete) {
this.options = options;
this.onComplete = onComplete;
}
apply(compiler) {
if (compiler.hooks) {
const plugin = { name: 'MergeIntoFile' };
compiler.hooks.emit.tapAsync(plugin, this.run.bind(this));
} else {
compiler.plugin('emit', this.run.bind(this));
}
}
static getHashOfRelatedFile(assets, fileName) {
let hashPart = null;
Object.keys(assets).forEach(existingFileName => {
const match = existingFileName.match(/-([0-9a-f]+)(\.min)?(\.\w+)(\.map)?$/);
const fileHashPart = match && match.length && match[1];
if (fileHashPart) {
const canonicalFileName = existingFileName.replace(`-${fileHashPart}`, '').replace(/\.map$/, '');
if (canonicalFileName === fileName.replace(/\.map$/, '')) {
hashPart = fileHashPart;
}
}
});
return hashPart;
}
run(compilation, callback) {
const { files, transform, encoding, hash, chunks } = this.options;
if (chunks && compilation.chunks && compilation.chunks.filter(chunk => chunks.indexOf(chunk.name) >= 0 && chunk.rendered).length === 0) {
callback();
return;
}
console.log(this.options);
console.log(this.options.files);
const generatedFiles = {};
let filesCanonical = [];
if (!Array.isArray(files)) {
Object.keys(files).forEach(newFile => {
filesCanonical.push({
src: files[newFile],
dest: newFile
});
});
} else {
filesCanonical = files;
}
filesCanonical.forEach(fileTransform => {
if (typeof fileTransform.dest === 'string') {
const destFileName = fileTransform.dest;
fileTransform.dest = code => ({ // eslint-disable-line no-param-reassign
[destFileName]: transform && transform[destFileName] ? transform[destFileName](code) : code
});
}
});
const finalPromises = filesCanonical.map((() => {
var _ref3 = _asyncToGenerator(function* (fileTransform) {
const listOfLists = yield Promise.all(fileTransform.src.map(function (path) {
return listFiles(path, null);
}));
const flattenedList = Array.prototype.concat.apply([], listOfLists);
const filesContentPromises = flattenedList.map(function (path) {
return readFile(path, encoding || 'utf-8');
});
const content = yield joinContent(filesContentPromises, '\n');
const resultsFiles = yield fileTransform.dest(content);
Object.keys(resultsFiles).forEach(function (newFileName) {
let newFileNameHashed = newFileName;
if (hash) {
const hashPart = MergeIntoFile.getHashOfRelatedFile(compilation.assets, newFileName) || revHash(resultsFiles[newFileName]);
newFileNameHashed = newFileName.replace(/(\.min)?\.\w+(\.map)?$/, function (suffix) {
return `-${hashPart}${suffix}`;
});
const fileId = newFileName.replace(/\.map$/, '').replace(/\.\w+$/, '');
const chunk = compilation.addChunk(fileId);
chunk.id = fileId;
chunk.ids = [chunk.id];
chunk.files.push(newFileNameHashed);
}
generatedFiles[newFileName] = newFileNameHashed;
compilation.assets[newFileNameHashed] = { // eslint-disable-line no-param-reassign
source() {
return resultsFiles[newFileName];
},
size() {
return resultsFiles[newFileName].length;
}
};
});
});
return function (_x5) {
return _ref3.apply(this, arguments);
};
})());
Promise.all(finalPromises).then(() => {
if (this.onComplete) {
this.onComplete(generatedFiles);
}
callback();
}).catch(error => callback(error));
}
}
module.exports = MergeIntoFile;