-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
323 lines (245 loc) · 6.57 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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
const path = require('path');
const vinyl = require('vinyl');
const vinylSa = require('vinyl-sourcemaps-apply');
const through = require('through2');
const rollup = require('rollup');
const hash = require('object-hash');
const root = require('njfs').root;
const PLUGIN_NAME = 'gulp-rollup-2';
let cache = {};
const modules = ['es', 'amd', 'cjs', 'iife', 'umd', 'system'];
const isInMod = (format) => modules.indexOf(format) > -1;
const isArray = arg => Object.prototype.toString.call(arg) === '[object Array]';
const unique = (obj) => Array.prototype.filter.call(obj, (v, i) => obj.indexOf(v) === i);
const isEqual = (a, b) => {
const oka = Object.keys(a);
const okb = Object.keys(b);
const ola = oka.length;
const olb = okb.length;
if (ola !== olb) {
return false;
}
let i = 0;
for (; i < ola; i += 1) {
const ka = oka[i];
if (b[ka] === undefined) {
return false;
}
const va = a[ka];
const vb = b[ka];
if (va == null || typeof va === 'string' || typeof va === 'number') {
if (va !== vb) {
return false;
}
} else if (typeof va === 'function') {
if (va.name !== vb.name) {
return false;
}
} else if (va.constructor !== vb.constructor) {
return false;
} else {
return isEqual(va, vb);
}
}
return true;
};
const sanitize = (opts, out) => {
if (!opts) {
throw new Error(`${PLUGIN_NAME}: Missing rollup config options!`);
}
if (typeof opts === 'string') {
opts = [{
output: {
format: opts
}
}];
} else if (!isArray(opts)) {
opts = [opts];
}
opts.filter(e => {
if (out && !e.input) {
throw new Error(`${PLUGIN_NAME}: Input option required!`);
} else if (!e.output) {
throw new Error(`${PLUGIN_NAME}: Output option required!`);
} else if (isArray(e.output)) {
if (e.output.filter((i) => !i.file || !i.format || !isInMod(i.format)).length) {
throw new Error(`${PLUGIN_NAME}: Missing options file, format or unknown format type!`);
}
} else if (!e.output.file || !e.output.format || !isInMod(e.output.format)) {
throw new Error(`${PLUGIN_NAME}: Missing options file, format or unknown format type!`);
}
});
const options = [];
opts.forEach((opt, i) => {
const io = Object.assign({}, opt);
const oo = io.output;
delete io.output;
options[i] = {};
options[i]['io'] = io;
options[i]['oo'] = isArray(oo) ? oo : [oo];
});
opts = options;
opts.forEach((opt, i) => {
const currI = opt.io;
opts.forEach((e, j) => {
if (i !== j && isEqual(currI, e.io)) {
throw new Error(`${PLUGIN_NAME}: Repetitive input options!`);
}
});
});
const of = [];
opts.forEach((opt) => {
const oo = opt.oo;
oo.forEach((o, i) => {
const curr = o;
oo.forEach((e, j) => {
if (i !== j) {
if (isEqual(curr, e) || curr.file === e.file) {
throw new Error(`${PLUGIN_NAME}: Two or more output have same file option!`);
} else {
of .push(curr.file);
}
}
});
});
});
if ( of .length !== unique( of ).length) {
throw new Error(`${PLUGIN_NAME}: Two or more output have same file option!`);
}
return opts;
};
const inside = (opts) => {
opts = sanitize(opts);
return through({
objectMode: true
},
async (file, encoding, callback) => {
if (file.isStream()) {
this.emit('Error', Error(`${PLUGIN_NAME}: Unsupported file type: Stream!`));
callback();
}
const _map = !!file.sourceMap;
const _dir = file.cwd;
const _pat = file.path;
const _inp = path.relative(_dir, _pat);
const bundles = await Promise.all(opts.map(async opt => {
const io = opt.io;
const oo = opt.oo;
const ch = hash(io);
cache[ch] = {};
io.input = io.input ? path.relative(_dir, io.input) : _inp;
io.cache = io.cache ? cache[ch] : false;
const bundle = await rollup.rollup(io);
cache[ch] = bundle.cache;
return {
bundle,
oo,
input: io.input
};
}));
let start = 2;
await Promise.all(bundles.map(async bundle => {
const oo = bundle.oo;
const input = bundle.input;
bundle = bundle.bundle;
await Promise.all(oo.map(async o => {
start--;
const f = o.format;
const s = start > 0;
if ((f === 'umd' || f === 'iife') && !o.name) {
o.name = input;
}
if ((f === 'umd' || f === 'amd') && !o.amd || !!o.amd && !o.amd.id) {
o.amd = Object.assign({}, o.amd, {
id: o.name
});
}
const build = await bundle.generate(o);
const [output] = build.output;
const {
code
} = output;
const buffer = Buffer.from(code, encoding);
const oBase = path.dirname(path.join(_dir, input));
const oName = path.basename(o.file);
const oPath = path.resolve(oBase, oName);
let oFile = s ? file : new vinyl();
let map = output.map;
oFile.path = oPath;
oFile.contents = buffer;
oFile.base = oBase;
if (map !== null) {
vinylSa(oFile, map);
} else if (_map && s) {
map = file.sourceMap;
map.file = input;
map.sources = map.sources.map(src => path.relative(_dir, path.join(oBase, src)));
vinylSa(oFile, map);
}
return s ? oFile : this.push(oFile);
}));
}));
callback(null, file);
});
};
const outside = async (opts) => {
opts = sanitize(opts, true);
const _dir = root();
const _obj = through.obj();
const bundles = await Promise.all(opts.map(async (opt) => {
const io = opt.io;
const oo = opt.oo;
const ch = hash(io);
cache[ch] = {};
io.input = path.relative(_dir, io.input);
io.cache = io.cache ? cache[ch] : false;
const bundle = await rollup.rollup(io);
cache[ch] = bundle.cache;
return {
bundle,
oo,
input: io.input
};
}));
await Promise.all(bundles.map(async bundle => {
const oo = bundle.oo;
const input = bundle.input;
bundle = bundle.bundle;
await Promise.all(oo.map(async o => {
const f = o.format;
if ((f === 'umd' || f === 'iife') && !o.name) {
o.name = input;
}
if ((f === 'umd' || f === 'amd') && !o.amd || !!o.amd && !o.amd.id) {
o.amd = Object.assign({}, o.amd, {
id: o.name
});
}
const build = await bundle.generate(o);
const [output] = build.output;
const {
code,
map
} = output;
const buffer = Buffer.from(code, "utf8");
const oBase = path.dirname(path.join(_dir, input));
const oName = path.basename(o.file);
const oPath = path.resolve(oBase, oName);
const oFile = new vinyl({
base: oBase,
path: oPath,
contents: buffer
});
if (map !== null) {
vinylSa(oFile, map);
}
_obj.push(oFile);
}));
}));
_obj.push(null);
return _obj;
};
module.exports = {
rollup: inside,
src: outside
};