-
Notifications
You must be signed in to change notification settings - Fork 12
/
index.js
243 lines (210 loc) · 7.09 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
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
'use strict';
var PLUGIN_NAME = 'gulp-rollup';
var util = require('util');
var PluginError = require('plugin-error');
var File = require('vinyl');
var Transform = require('readable-stream').Transform;
var hypothetical = require('rollup-plugin-hypothetical');
var path = require('path');
var bufferFrom = require('buffer-from');
function cloneWithBlacklist(obj) {
var out = {};
outer:
for (var key in obj) {
for (var i = 1; i < arguments.length; ++i) {
if (arguments[i] === key) {
continue outer;
}
}
out[key] = obj[key];
}
return out;
}
function deepEqual(a, b) {
if (typeof a !== 'object' || typeof b !== 'object') {
return a === b;
}
var key;
for (key in a) {
if (!(key in b) || !deepEqual(a[key], b[key])) {
return false;
}
}
for (key in b) {
if (!(key in a)) {
return false;
}
}
return true;
}
function deExternalizePath(path) {
if (/^(\.?\.?|[A-Za-z]:)\//.test(path)) {
return path;
} else {
// path is external
return './' + path;
}
}
function GulpRollup(options) {
var self = this;
Transform.call(self, { objectMode: true });
var options0 = options || {};
options = cloneWithBlacklist(options0,
'rollup',
'allowRealFiles',
'impliedExtensions',
'separateCaches',
'generateUnifiedCache');
var rollup = options0.rollup || require('rollup');
var allowRealFiles = options0.allowRealFiles;
var impliedExtensions = options0.impliedExtensions;
if (impliedExtensions === undefined) {
impliedExtensions = ['.js'];
} else if (impliedExtensions !== false && !Array.isArray(impliedExtensions)) {
throw new Error('options.impliedExtensions must be false, undefined, or an Array!');
}
var unifiedCachedModules = options0.generateUnifiedCache && {};
var separateCaches = options0.separateCaches;
if (separateCaches) {
separateCaches = cloneWithBlacklist(separateCaches);
}
var wonderland = {}, vinylFiles = {};
var haveSourcemaps;
var inputWasNamedEntry = Boolean(options.entry);
var inputName = inputWasNamedEntry ? 'options.entry' : 'options.input';
var entryFiles = Promise.resolve(
inputWasNamedEntry ? options.entry : options.input
).then(function(entryFiles) {
if (typeof entryFiles === 'string') {
return [entryFiles];
} else if (Array.isArray(entryFiles)) {
if (entryFiles.some(function(entryFile) {
return typeof entryFile !== 'string';
})) {
throw new Error(inputName + ' must include only strings!');
}
return entryFiles;
} else {
throw new Error(inputName + ' must be a string or array of strings!');
}
});
self._transform = function(file, enc, cb) {
if (!file.isBuffer()) {
self.emit('error', new PluginError(PLUGIN_NAME, 'Input files must be buffered!'));
return cb();
}
if (haveSourcemaps === undefined) {
haveSourcemaps = file.sourceMap !== undefined;
} else if (haveSourcemaps !== (file.sourceMap !== undefined)) {
self.emit('error', new PluginError(PLUGIN_NAME, 'Mixing of sourcemapped and non-sourcemapped files!'));
return cb();
}
var nonExternalFilePath = deExternalizePath(file.path);
if (haveSourcemaps) {
wonderland[nonExternalFilePath] = {
code: file.contents.toString(),
map: file.sourceMap
};
} else {
wonderland[nonExternalFilePath] = file.contents.toString();
}
vinylFiles[nonExternalFilePath] = file;
cb();
};
self._flush = function(cb) {
if (!options.plugins) {
options.plugins = [];
} else if (!Array.isArray(options.plugins)) {
options.plugins = [options.plugins];
}
options.plugins = options.plugins.concat(hypothetical({
files: wonderland,
allowFallthrough: allowRealFiles,
impliedExtensions: impliedExtensions
}));
if (options.output) {
options.output.sourcemap = haveSourcemaps;
} else {
options.sourcemap = haveSourcemaps;
}
var vinylSystem = hypothetical({ files: vinylFiles, allowFallthrough: true, impliedExtensions: impliedExtensions });
var options1 = options;
entryFiles.then(function(entryFiles) {
return Promise.all(entryFiles.map(function(entryFile) {
var options = cloneWithBlacklist(options1);
if (inputWasNamedEntry) {
options.entry = entryFile;
} else {
options.input = entryFile;
}
if (separateCaches && Object.prototype.hasOwnProperty.call(separateCaches, entryFile)) {
options.cache = separateCaches[entryFile];
}
return rollup.rollup(options).then(function(bundle) {
self.emit('bundle', bundle, entryFile);
if (unifiedCachedModules) {
var modules = bundle.modules;
for (var i = 0; i < modules.length; ++i) {
var module = modules[i], id = module.id;
if (Object.prototype.hasOwnProperty.call(unifiedCachedModules, id)) {
if (!deepEqual(module, unifiedCachedModules[id])) {
throw new Error('Conflicting caches for module "' + id + '"!');
}
} else {
unifiedCachedModules[id] = module;
}
}
}
return bundle.generate(options);
}).then(function(result) {
// get the corresponding entry Vinyl file to output with.
// this makes file.history work. maybe expando properties too if you use them.
var file = vinylSystem.resolveId(entryFile);
if (file !== undefined) {
file = vinylSystem.load(file);
}
if (file === undefined) { // possible if options.allowRealFiles is true
file = new File({
path: entryFile,
contents: bufferFrom(result.code)
});
} else {
file.contents = bufferFrom(result.code);
}
var map = result.map;
if (map) {
// This makes sure the paths in the generated source map (file and
// sources) are relative to file.base:
map.file = unixStylePath(file.relative);
map.sources = map.sources.map(function(fileName) {
return unixStylePath(path.relative(file.base, fileName));
});
file.sourceMap = map;
}
self.push(file);
});
}));
}).then(function() {
if (unifiedCachedModules) {
var modules = [];
for (var id in unifiedCachedModules) {
modules.push(unifiedCachedModules[id]);
}
self.emit('unifiedcache', { modules: modules });
}
cb(); // it's over!
}).catch(function(err) {
setImmediate(function() {
self.emit('error', new PluginError(PLUGIN_NAME, err));
cb();
});
});
};
}
util.inherits(GulpRollup, Transform);
function unixStylePath(filePath) {
return filePath.split(path.sep).join('/');
}
module.exports = function(options) {
return new GulpRollup(options);
};