/
index.js
573 lines (539 loc) · 13.3 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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
const path = require('path');
const assert = require('assert');
const sander = require('sander');
const { loader } = require('../utils.js');
const rollup = require('../../dist/rollup.js');
describe('hooks', () => {
it('supports buildStart and buildEnd hooks', () => {
let buildStartCnt = 0;
let buildEndCnt = 0;
return rollup
.rollup({
input: 'input',
plugins: [
loader({ input: `alert('hello')` }),
{
buildStart () {
buildStartCnt++;
},
buildEnd () {
buildEndCnt++;
}
}
]
})
.then(bundle => {
assert.equal(buildStartCnt, 1);
assert.equal(buildEndCnt, 1);
return rollup
.rollup({
input: 'input',
plugins: [
loader({ input: `invalid_source - @#$%^&*` }),
{
buildStart () {
buildStartCnt++;
},
buildEnd () {
buildEndCnt++;
}
}
]
})
})
.catch(err => {
assert.ok(err);
})
.then(() => {
assert.equal(buildStartCnt, 2);
assert.equal(buildEndCnt, 2);
});
});
it('supports warnings in buildStart and buildEnd hooks', () => {
let callCnt = 0;
return rollup
.rollup({
input: 'input',
onwarn (warning) {
if (callCnt === 0) {
assert.equal(warning.message, 'build start');
callCnt++;
} else if (callCnt === 1) {
assert.equal(warning.message, 'build end');
callCnt++;
}
},
plugins: [
loader({ input: `alert('hello')` }),
{
buildStart () {
this.warn('build start');
},
buildEnd () {
this.warn('build end');
}
}
]
})
.then(() => {
assert.equal(callCnt, 2);
});
});
it('supports isExternal on plugin context', () => {
let callCnt = 0;
return rollup
.rollup({
input: 'input',
external: ['test'],
plugins: [
loader({ input: `alert('hello')` }),
{
buildStart () {
assert.equal(this.isExternal('test'), true);
assert.equal(this.isExternal('another'), false);
}
}
]
});
});
it('supports resolveId on plugin context', () => {
let callCnt = 0;
return rollup
.rollup({
input: 'input',
plugins: [
loader({
input: `import 'test'`,
dep1: `import 'next'`,
dep2: `alert('hello')`
}),
{
resolveId (id, parent) {
if (id === 'test')
return 'dep1';
if (id === 'next')
return this.resolveId('final');
if (id === 'final')
return 'dep2';
}
}
]
})
.then(bundle => {
return bundle.generate({ format: 'es' });
})
.then(output => {
assert.equal(output.code, `alert('hello');\n`);
});
});
it('passes bundle & output object to ongenerate & onwrite hooks', () => {
const file = path.join(__dirname, 'tmp/bundle.js');
return rollup
.rollup({
input: 'input',
plugins: [
loader({ input: `alert('hello')` }),
{
ongenerate(bundle, out) {
out.ongenerate = true;
},
onwrite(bundle, out) {
assert.equal(out.ongenerate, true);
}
}
]
})
.then(bundle => {
return bundle.write({
file,
format: 'es'
});
})
.then(() => {
return sander.unlink(file);
});
});
it('calls ongenerate hooks in sequence', () => {
const result = [];
return rollup
.rollup({
input: 'input',
plugins: [
loader({ input: `alert('hello')` }),
{
ongenerate(info) {
result.push({ a: info.format });
}
},
{
ongenerate(info) {
result.push({ b: info.format });
}
}
]
})
.then(bundle => bundle.generate({ format: 'cjs' }))
.then(() => {
assert.deepEqual(result, [{ a: 'cjs' }, { b: 'cjs' }]);
});
});
it('calls onwrite hooks in sequence', () => {
const result = [];
const file = path.join(__dirname, 'tmp/bundle.js');
return rollup
.rollup({
input: 'input',
plugins: [
loader({ input: `alert('hello')` }),
{
onwrite(info) {
return new Promise(fulfil => {
result.push({ a: info.file, format: info.format });
fulfil();
});
}
},
{
onwrite(info) {
result.push({ b: info.file, format: info.format });
}
}
]
})
.then(bundle => {
return bundle.write({
file,
format: 'cjs'
});
})
.then(() => {
assert.deepEqual(result, [{ a: file, format: 'cjs' }, { b: file, format: 'cjs' }]);
return sander.unlink(file);
});
});
it('calls missingExport hook', () => {
let wasCalled;
return rollup
.rollup({
input: 'main',
plugins: [
loader({
main: `import def from 'foo'; console.log( def );`,
foo: `export const named = 42;`
}),
{
missingExport() {
wasCalled = true;
return true;
}
}
]
})
.then(() => {
assert.ok(wasCalled);
});
});
it('supports asset emission', () => {
return rollup
.rollup({
input: 'input',
experimentalCodeSplitting: true,
plugins: [
loader({ input: '' }),
{
transform (code, id) {
const assetId = this.emitAsset('test.ext', 'hello world');
return `export default import.meta.ROLLUP_ASSET_URL_${assetId};`;
}
}
]
})
.then(bundle => {
return bundle.generate({ format: 'es' });
})
.then(({ output }) => {
assert.equal(output['assets/test-19916f7d.ext'], 'hello world');
assert.equal(output['input.js'].code, `var input = new URL(\'../assets/test-19916f7d.ext\', import.meta.url).href;\n\nexport default input;\n`);
});
});
it('supports CommonJS asset urls', () => {
return rollup
.rollup({
input: 'input',
experimentalCodeSplitting: true,
plugins: [
loader({ input: '' }),
{
transform (code, id) {
const assetId = this.emitAsset('test.ext', 'hello world');
return `export default import.meta.ROLLUP_ASSET_URL_${assetId};`;
}
}
]
})
.then(bundle => {
return bundle.generate({ format: 'cjs' });
})
.then(({ output }) => {
assert.equal(output['input.js'].code, `'use strict';
var input = new (typeof URL !== 'undefined' ? URL : require('ur'+'l').URL)((process.browser ? '' : 'file:') + __dirname + '/assets/test-19916f7d.ext', process.browser && document.baseURI).href;
module.exports = input;
`);
});
});
it('supports custom asset file names', () => {
return rollup
.rollup({
input: 'input',
experimentalCodeSplitting: true,
plugins: [
loader({ input: `alert('hello')` }),
{
transform (code, id) {
const assetId = this.emitAsset('test.ext', 'hello world');
return '';
}
}
]
})
.then(bundle => {
return bundle.generate({
format: 'es',
assetFileNames: '[name][extname]'
});
})
.then(({ output }) => {
assert.equal(output['test.ext'], 'hello world');
});
});
it('allows setting asset source separately', () => {
let assetId;
return rollup
.rollup({
input: 'input',
experimentalCodeSplitting: true,
plugins: [
loader({ input: `alert('hello')` }),
{
transform (code, id) {
assetId = this.emitAsset('test.ext');
return '';
},
generateBundle () {
this.setAssetSource(assetId, 'hello world');
}
}
]
})
.then(bundle => {
return bundle.generate({ format: 'es' });
})
.then(({ output }) => {
assert.equal(output['assets/test-19916f7d.ext'], 'hello world');
});
});
it('throws when setting asset source twice', () => {
return rollup
.rollup({
input: 'input',
experimentalCodeSplitting: true,
plugins: [
loader({ input: `alert('hello')` }),
{
transform (code, id) {
const assetId = this.emitAsset('test.ext');
this.setAssetSource(assetId, 'hello world');
try {
this.setAssetSource(assetId, 'another');
}
catch (e) {
assert.equal(e.code, 'ASSET_SOURCE_ALREADY_SET');
return '';
}
assert.fail();
}
}
]
})
.then(bundle => {
return bundle.generate({ format: 'es' });
})
.then(({ output }) => {
assert.equal(output['assets/test-19916f7d.ext'], 'hello world');
});
});
it('throws when emitting assets too late', () => {
let calledHook = false;
return rollup
.rollup({
input: 'input',
experimentalCodeSplitting: true,
plugins: [
loader({ input: `alert('hello')` }),
{
transformBundle (code, id) {
calledHook = true;
try {
const assetId = this.emitAsset('test.ext', 'hello world');
}
catch (e) {
assert.equal(e.code, 'ASSETS_ALREADY_FINALISED');
}
}
}
]
})
.then(bundle => {
return bundle.generate({
format: 'es',
assetFileNames: '[name][extname]'
});
})
.then(() => {
assert.equal(calledHook, true);
});
});
it('supports transformChunk in place of transformBundle', () => {
let calledHook = false;
return rollup
.rollup({
input: 'input',
experimentalCodeSplitting: true,
plugins: [
loader({ input: `alert('hello')` }),
{
transformChunk (code, id) {
calledHook = true;
try {
const assetId = this.emitAsset('test.ext', 'hello world');
}
catch (e) {
assert.equal(e.code, 'ASSETS_ALREADY_FINALISED');
}
}
}
]
})
.then(bundle => {
return bundle.generate({
format: 'es',
assetFileNames: '[name][extname]'
});
})
.then(() => {
assert.equal(calledHook, true);
});
});
it('passes bundle object to generateBundle hook', () => {
return rollup
.rollup({
input: 'input',
experimentalCodeSplitting: true,
plugins: [
loader({ input: `alert('hello')` }),
{
transform (code, id) {
const assetId = this.emitAsset('test.ext', 'hello world');
return `export default import.meta.ROLLUP_ASSET_URL_${assetId};`;
},
generateBundle (options, outputBundle, isWrite) {
assert.equal(outputBundle['assets/test-19916f7d.ext'], 'hello world');
assert.equal(outputBundle['input.js'].code, `var input = new URL(\'../assets/test-19916f7d.ext\', import.meta.url).href;\n\nexport default input;\n`);
}
}
]
})
.then(bundle => {
return bundle.generate({ format: 'es' });
});
});
it('supports assets uniquely defined in the generateBundle hook', () => {
return rollup
.rollup({
input: 'input',
experimentalCodeSplitting: true,
plugins: [
loader({ input: `alert('hello')` }),
{
generateBundle (options, outputBundle, isWrite) {
if (options.format === 'es') {
const depAssetId = this.emitAsset('lateDepAsset', 'custom source');
const source = `references ${this.getAssetFileName(depAssetId)}`;
this.emitAsset('lateMainAsset', source);
} else {
const depAssetId = this.emitAsset('lateDepAsset', 'different source');
const source = `references ${this.getAssetFileName(depAssetId)}`;
this.emitAsset('lateMainAsset', source);
}
}
}
]
})
.then(bundle =>
bundle.generate({ format: 'es' })
.then(outputBundle1 =>
bundle.generate({ format: 'cjs' })
.then(outputBundle2 => [outputBundle1, outputBundle2])
)
)
.then(([{ output: output1 }, { output: output2 }]) => {
assert.equal(output1['input.js'].code, `alert('hello');\n`);
assert.equal(output1['assets/lateDepAsset-671f747d'], `custom source`);
assert.equal(output1['assets/lateMainAsset-863ea4b5'], `references assets/lateDepAsset-671f747d`);
assert.equal(output2['input.js'].code, `'use strict';\n\nalert('hello');\n`);
assert.equal(output2['assets/lateDepAsset-671f747d'], undefined);
assert.equal(output2['assets/lateMainAsset-863ea4b5'], undefined);
assert.equal(output2['assets/lateDepAsset-c107f5fc'], `different source`);
assert.equal(output2['assets/lateMainAsset-6dc2262b'], `references assets/lateDepAsset-c107f5fc`);
});
});
it('supports errors thrown in the generateBundle hook', () => {
return rollup
.rollup({
input: 'input',
experimentalCodeSplitting: true,
plugins: [
loader({ input: `alert('hello')` }),
{
generateBundle (options, outputBundle, isWrite) {
this.error('test error');
}
}
]
})
.then(bundle => {
return bundle.generate({ format: 'es' });
})
.catch(err => {
assert.equal(err.message, 'test error');
});
});
it('supports processBundle hook including reporting rendered exports and source length', () => {
return rollup
.rollup({
input: 'input',
experimentalCodeSplitting: true,
plugins: [
loader({
input: `export { a as default } from 'dep';`,
dep: `export var a = 1; export var b = 2;`
}),
{
generateBundle (options, outputBundle, isWrite) {
const chunk = outputBundle['input.js'];
// can detect that b has been tree-shaken this way
assert.equal(chunk.modules['dep'].renderedExports[0], 'a');
assert.equal(chunk.modules['dep'].renderedExports.length, 1);
assert.equal(chunk.modules['dep'].removedExports[0], 'b');
assert.equal(chunk.modules['dep'].removedExports.length, 1);
assert.equal(chunk.modules['dep'].renderedLength, 10);
assert.equal(chunk.modules['dep'].originalLength, 35);
}
}
]
})
.then(bundle => {
return bundle.generate({ format: 'es' });
});
});
});