Repository navigation
Expand file tree
/
Copy pathapi.js
More file actions
1310 lines (1133 loc) · 39.9 KB
/
Copy pathapi.js
File metadata and controls
1310 lines (1133 loc) · 39.9 KB
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
<
10BC8
div data-line-number="321" class="react-line-number react-code-text" style="padding-right:16px">321322
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
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Module dependencies.
*/
var readfile = require('fs').readFileSync;
var readdir = require('fs').readdirSync;
var coffee = require('coffee-script');
var resolve = require('path').resolve;
var exist = require('fs').existsSync;
var lstat = require('fs').lstatSync;
var cache = require('../lib/cache');
var rmrf = require('rimraf').sync;
var File = require('../lib/file');
var util = require('../lib/util');
var join = require('path').join;
var assert = require('assert');
var wait = require('co-wait');
var styl = require('styl');
var fs = require('co-fs');
var Duo = require('..');
var vm = require('vm');
var slice = [].slice;
var token = util.token();
/**
* Tests.
*/
describe('Duo API', function () {
beforeEach(function* () {
yield cache.clean();
cleanup();
});
after(function* () {
yield cache.clean();
cleanup();
});
it('should throw without root', function () {
var err;
try {
Duo();
} catch (e) {
err = e;
}
assert(err && /root directory/.test(err.message));
});
it('should set default options', function () {
var duo = Duo(__dirname);
assert.equal(duo._root, __dirname);
assert.equal(duo._entry, undefined);
assert.equal(duo._manifest, 'component.json');
assert.equal(duo._global, undefined);
assert.equal(duo._installTo, 'components');
assert.equal(duo._buildTo, 'build');
assert.equal(duo._copy, false);
assert.equal(duo._development, false);
assert.equal(duo._concurrency, 50);
assert.equal(duo._cache, true);
});
describe('.entry()', function () {
it('should get the current entry file', function () {
var duo = Duo(__dirname);
duo._entry = 'entry';
assert.equal(duo.entry(), 'entry');
});
it('should be classified as a local', function () {
var duo = Duo(__dirname);
duo.entry('path');
var file = duo.entry();
assert(file instanceof File);
assert(file.local());
assert(!file.remote());
});
});
describe('.entry(path)', function () {
it('should set the entry file by path', function () {
var duo = Duo(__dirname);
duo.entry('path');
var file = duo.entry();
assert(file instanceof File);
assert.equal(file.path, join(__dirname, 'path'));
assert.equal(file.entry, true);
});
});
describe('.entry(source, type)', function () {
it('should set the entry file by source and type', function () {
var duo = Duo(__dirname);
duo.entry('string', 'js');
var file = duo.entry();
assert(file instanceof File);
assert.equal(file.raw, 'string');
assert.equal(file.type, 'js');
});
});
describe('.root()', function () {
it('should get the root path', function () {
var duo = Duo(__dirname);
assert.equal(duo.root(), __dirname);
});
});
describe('.root(path)', function () {
it('should set the root path', function () {
var duo = Duo(__dirname);
duo.root('root');
assert.equal(duo.root(), 'root');
});
});
describe('.token()', function () {
it('should get the token name', function () {
var duo = Duo(__dirname);
duo._token = 'token';
assert.equal(duo.token(), 'token');
});
});
describe('.token(name)', function () {
it('should set the token name', function () {
var duo = Duo(__dirname);
duo.token('secret');
assert.equal(duo.token(), 'secret');
});
});
describe('.manifest()', function () {
it('should get the manifest name', function () {
var duo = Duo(__dirname);
assert.equal(duo.manifest(), 'component.json');
});
});
describe('.manifest(name)', function () {
it('should set the manifest name', function () {
var duo = Duo(__dirname);
duo.manifest('package.json');
assert.equal(duo.manifest(), 'package.json');
});
});
describe('.global()', function () {
it('should get the global name', function () {
var duo = Duo(__dirname);
duo._global = 'global';
assert.equal(duo.global(), 'global');
});
});
describe('.global(name)', function () {
it('should set the global name', function () {
var duo = Duo(__dirname);
duo.global('package.json');
assert.equal(duo.global(), 'package.json');
});
});
describe('.development()', function () {
it('should get whether to build with development dependencies', function () {
var duo = Duo(__dirname);
assert.equal(duo.development(), false);
});
});
describe('.development(name)', function () {
it('should set whether to build with development dependencies', function () {
var duo = Duo(__dirname);
duo.development(true);
assert.equal(duo.development(), true);
});
});
describe('.copy()', function () {
it('should get whether to copy files instead of symlinking', function () {
var duo = Duo(__dirname);
assert.equal(duo.copy(), false);
});
});
describe('.copy(name)', function () {
it('should set whether to copy files instead of symlinking', function () {
var duo = Duo(__dirname);
duo.copy(true);
assert.equal(duo.copy(), true);
});
});
describe('.concurrency()', function () {
it('should get the download concurrency value', function () {
var duo = Duo(__dirname);
assert.equal(duo.concurrency(), 50);
});
});
describe('.concurrency(value)', function () {
it('should set the download concurrency value', function () {
var duo = Duo(__dirname);
duo.concurrency(1);
assert.equal(duo.concurrency(), 1);
});
});
describe('.update()', function () {
it('should get the update flag', function () {
var duo = Duo(__dirname);
assert.equal(duo.update(), false);
});
});
describe('.update(value)', function () {
it('should set the update flag', function () {
var duo = Duo(__dirname);
duo.update(true);
assert.equal(duo.update(), true);
duo.update(false);
assert.equal(duo.update(), false);
});
});
describe('.cache()', function () {
it('should get the cache flag', function () {
var duo = Duo(__dirname);
assert.equal(duo.cache(), true);
});
});
describe('.cache(value)', function () {
it('should set the cache flag', function () {
var duo = Duo(__dirname);
duo.cache(true);
assert.equal(duo.cache(), true);
duo.cache(false);
assert.equal(duo.cache(), false);
});
});
describe('.installTo()', function () {
it('should get the relative path to the install directory', function () {
var duo = Duo(__dirname);
assert.equal(duo.installTo(), 'components');
});
it('should set the relative path to the install directory', function () {
var duo = Duo(__dirname);
duo.installTo('modules');
assert.equal(duo.installTo(), 'modules');
});
});
describe(
10BC8
039;.buildTo()', function () {
it('should get the relative path to the build directory', function () {
var duo = Duo(__dirname);
assert.equal(duo.buildTo(), 'build');
});
it('should set the relative path to the build directory', function () {
var duo = Duo(__dirname);
duo.buildTo('public');
assert.equal(duo.buildTo(), 'public');
});
});
describe('.path(paths...)', function () {
it('should resolve paths relative to the root directory', function () {
var duo = Duo(__dirname);
var dir = join(__dirname, 'folder');
assert.equal(duo.path('folder'), dir);
});
});
describe('.buildPath(paths...)', function () {
it('should resolve paths relative to the build directory', function () {
var duo = Duo(__dirname);
var dir = join(__dirname, 'build', 'folder');
assert.equal(duo.buildPath('folder'), dir);
});
});
describe('.installPath(paths...)', function () {
it('should resolve paths relative to the install directory', function () {
var duo = Duo(__dirname);
var dir = join(__dirname, 'components', 'folder');
assert.equal(duo.installPath('folder'), dir);
});
});
describe('.hash(input)', function () {
var duo = Duo(__dirname);
it('should hash a string into md5', function () {
assert.equal(duo.hash('Hello World'), 'b10a8db164e0754105b7a99be72e3fe5');
});
it('should hash an object by serializing to JSON first', function () {
assert.equal(duo.hash({}), '99914b932bd37a50b983c5e7c90ae93b');
});
it('should allow Buffers too', function () {
assert.equal(duo.hash(new Buffer('abc')), '900150983cd24fb0d6963f7d28e17f72');
});
});
describe('.cleanCache()', function () {
it('should destroy the cache', function* () {
var duo = build('simple-deps');
yield duo.run();
assert(exists('simple-deps/components/duo-cache'));
yield duo.cleanCache();
assert(!exists('simple-deps/components/duo-cache'));
});
it('should not throw an error when no cache exists', function* () {
var duo = build('simple-deps');
assert(!exists('simple-deps/components/duo-cache'));
yield duo.cleanCache();
assert(!exists('simple-deps/components/duo-cache'));
});
});
describe('.include(name, src, [type])', function () {
it('should add the specified module to the includes hash', function () {
var duo = build('includes');
duo.include('some-include', 'module.exports = "a"');
assert('some-include' in duo.includes);
});
it('should parse the included file for dependencies (when a type is specified)', function () {
var duo = build('includes');
duo.include('some-include', 'require("other-include");', 'js');
assert('other-include' in duo.includes['some-include'].deps);
});
});
describe('.included(name)', function () {
it('should tell us if something has already been included', function () {
var duo = build('includes');
assert(!duo.included('some-include'));
duo.include('some-include', 'module.exports = "a"');
assert(duo.included('some-include'));
});
});
describe('.run([fn])', function () {
it('should ignore runs without an entry or source', function* () {
var js = yield Duo(__dirname).run();
assert.deepEqual(js, { code: '' });
});
it('should build simple modules', function* () {
var js = yield build('simple').run();
var ctx = evaluate(js.code);
assert.deepEqual(['one', 'two'], ctx.main);
});
it('should build require conflicts', function* () {
this.timeout(10000);
var js = yield build('require-conflict').run();
var ctx = evaluate(js.code);
var mod = ctx.main;
assert(mod.send !== mod.json, 'segmentio/json == yields/send-json');
});
it('should build with no deps', function* () {
var js = yield build('no-deps').run();
var ctx = evaluate(js.code).main;
assert.equal('a', ctx);
});
it('resolve directories like `require(./lib)`', function* () {
var js = yield build('resolve').run();
var ctx = evaluate(js.code);
assert.deepEqual('resolved', ctx.main);
});
it('should resolve relative files like `require(../path/file.js`)', function* () {
var js = yield build('resolve-file').run();
var ctx = evaluate(js.code);
assert.equal('resolved', ctx.main);
});
it('should resolve dependencies like require("..")', function* () {
var js = yield build('relative-path', 'test/test.js').run();
var ctx = evaluate(js.code);
assert.equal('index', ctx.main);
});
it('should resolve dependencies that have dots in the name', function* () {
var js = yield build('file-dots', 'index.js').run();
var ctx = evaluate(js.code);
assert.strictEqual(ctx.a1, ctx.a2);
});
it('should fail when relative requires are not found', function* () {
try {
var js = yield build('resolve-relative-missing', 'index.js').run();
} catch (err) {
assert(err instanceof Error);
}
assert(!js, 'should have failed');
});
it('should fail when absolute requires are not found', function* () {
try {
var js = yield build('resolve-absolute-missing', 'index.js').run();
} catch (err) {
assert(err instanceof Error);
}
assert(!js, 'should have failed');
});
it('should fetch and build direct dependencies', function* () {
this.timeout(15000);
var js = yield build('simple-deps').run();
var ctx = evaluate(js.code);
assert(ctx.mods);
assert.equal(2, ctx.mods.length);
assert.equal('string', ctx.mods[0](''));
assert.equal(typeof ctx.mods[1], 'function');
});
it('should fetch dependencies from manifest', function* () {
var js = yield build('manifest-deps').run();
var ctx = evaluate(js.code);
var type = ctx.main;
assert.equal('string', type(''));
});
it('should fetch dependencies via semver ranges', function* () {
var js = yield build('deps-semver').run();
var ctx = evaluate(js.code).main;
assert.equal(ctx, 'function');
});
it('should fail when manifest has a syntax error', function* () {
try {
var success = yield build('manifest-syntax-err').run();
} catch (e) {
assert(e instanceof SyntaxError);
}
assert(!success);
});
it('should decorate the SyntaxError object', function* () {
try {
var success = yield build('manifest-syntax-err').run();
} catch (e) {
assert.equal(e.message, 'Unexpected token }');
assert.equal(e.fileName, path('manifest-syntax-err/component.json'));
}
assert(!success);
});
it('should be idempotent', function* () {
var a = yield build('idempotent').run();
var b = yield build('idempotent').run();
var c = yield build('idempotent').run();
a = evaluate(a.code).main;
b = evaluate(b.code).main;
c = evaluate(c.code).main;
assert.equal('string', a(''));
assert.equal('string', b(''));
assert.equal('string', c(''));
});
it('should rebuild correctly when a file is touched', function* () {
var p = join(path('rebuild'), 'index.js');
var js = yield fs.readFile(p, 'utf8');
var a = build('rebuild');
var b = build('rebuild');
var c = build('rebuild');
a = yield a.run();
fs.writeFile(p, js);
b = yield b.run();
c = yield c.run();
assert(a.code && b.code && c.code);
assert.equal(a.code, b.code);
assert.equal(b.code, c.code);
});
it('should resolve repos with different names', function* () {
this.timeout(15000);
var js = yield build('different-names').run();
var ms = evaluate(js.code).main;
assert.equal(36000000, ms('10h'));
});
it('should resolve dependencies that use require("{user}-{repo}")', function* () {
var js = yield build('user-repo-dep').run();
var type = evaluate(js.code).main;
assert.equal('string', type(js.code));
});
it('.run(fn) should work with a function', function (done) {
build('simple').run(function (err, js) {
assert(!err);
var ctx = evaluate(js.code);
assert.deepEqual(['one', 'two'], ctx.main);
done();
});
});
it('should support multiple versions in the same file', function* () {
var js = yield build('multiple-versions').run();
var mimes = evaluate(js.code).mimes;
assert.equal('image/x-nikon-nef', mimes[0].lookup('.nef')); // 0.0.2
assert.equal(null, mimes[1].lookup('.nef')); // 0.0.1
assert.equal('image/jpeg', mimes[1].lookup('.jpg'));
});
it('should properly mark local and remote files', function* () {
var duo = build('local-vs-remote');
duo.use(function test(file) {
switch (file.id) {
case 'index.js':
case 'local.js':
assert(file.local());
break;
case 'components/component-to-function@2.0.5/index.js':
case 'components/component-props@1.1.2/index.js':
assert(file.remote());
break;
default:
throw new Error('unhandled file ' + file.id);
}
});
yield duo.run();
});
it('should properly mark local and remote files even when installed somewhere else', function* () {
var duo = build('local-vs-remote');
duo.installTo('not components');
duo.use(function test(file) {
switch (file.id) {
case 'index.js':
case 'local.js':
assert(file.local());
break;
case 'not components/component-to-function@2.0.5/index.js':
case 'not components/component-props@1.1.2/index.js':
assert(file.remote());
break;
default:
throw new Error('unhandled file ' + file.id);
}
});
yield duo.run();
});
it('should update dependencies when the manifest changes', function* () {
var duo = build('manifest-modify');
var a = path('manifest-modify', 'component-a.json');
var b = path('manifest-modify', 'component-b.json');
var manifest = path('manifest-modify', 'component.json');
// write manifest a
yield fs.writeFile(manifest, yield fs.readFile(a, 'utf8'));
var adeps = yield duo.install();
yield wait(1000); // need to wait so mtime changes
// write manifest b
yield fs.writeFile(manifest, yield fs.readFile(b, 'utf8'));
var bdeps = yield duo.install();
// assertions
assert.notDeepEqual(Object.keys(adeps), Object.keys(bdeps));
// cleanup
yield fs.unlink(manifest);
});
describe('with .entry(path)', function () {
it('should work with full paths for entry files', function* () {
var entry = join(path('simple'), 'index.js');
var js = yield build('simple', entry).run();
var ctx = evaluate(js.code);
assert.deepEqual(['one', 'two'], ctx.main);
});
it('should throw if the entry file doesn\'t exist', function* () {
var duo = Duo(__dirname).entry('zomg.js');
try {
yield duo.run();
} catch (e) {
assert(~e.message.indexOf('cannot find entry: zomg.js'));
}
});
it('should treat entries idempotently', function* () {
var root = path('simple');
var duo = Duo(root).entry('hi.js');
duo.entry('index.js');
var js = yield duo.run();
var ctx = evaluate(js.code);
assert.deepEqual(['one', 'two'], ctx.main);
var json = yield mapping(duo);
assert.equal(true, json['index.js'].entry);
});
});
describe('with .entry(source, type)', function () {
it('should support passing raw source for entry files', function* () {
var src = read('simple/index.js');
var root = path('simple');
var duo = Duo(root).entry(src, 'js');
var js = yield duo.run();
var ctx = evaluate(js.code);
assert.deepEqual(['one', 'two'], ctx.main);
});
it('should support passing transformed raw source for entry files', function* () {
var src = read('coffee/index.coffee');
var root = path('coffee');
var duo = Duo(root).use(cs).entry(src, 'coffee');
var js = yield duo.run();
var ctx = evaluate(js.code).main;
assert.equal(ctx.a, 'a');
assert.equal(ctx.b, 'b');
assert.equal(ctx.c, 'c');
});
it('should not fail when the input source code is empty', function* () {
yield Duo(path('simple')).entry('', 'css').run();
});
});
// describe('with .development(false)');
describe('with .development(true)', function () {
it('should fetch and bundle development dependencies', function* () {
this.timeout(20000);
var duo = build('simple-dev-deps');
duo.development(true);
var js = yield duo.run();
var ctx = evaluate(js.code);
assert.equal('trigger', ctx.mods[0].name);
assert.equal(typeof ctx.mods[1].equal, 'function');
});
});
describe('with .sourceMap(false)', function () {
it('should not generate sourcemaps', function* () {
var js = yield build('simple').run();
assert.equal(js.code.indexOf('//# sourceMappingURL'), -1);
assert(!js.map);
});
});
describe('with .sourceMap(true)', function () {
it('should generate sourcemaps', function* () {
var duo = build('simple').sourceMap(true);
var js = yield duo.run();
assert(js.code.indexOf('//# sourceMappingURL') > -1);
assert(js.map);
});
});
describe('with .sourceMap("inline")', function () {
it('should generate sourcemaps', function* () {
var duo = build('simple').sourceMap('inline');
var js = yield duo.run();
assert(js.code.indexOf('//# sourceMappingURL') > -1);
assert(!js.map);
});
});
describe('with .copy(false)', function () {
it('should symlink files', function* () {
var duo = build('symlink', 'index.css');
var file = path('symlink/build/duo.png');
var out = read('symlink/index.out.css');
var css = yield duo.run();
assert.equal(css.code, out);
var stat = yield fs.lstat(file);
assert(stat.isSymbolicLink());
});
});
describe('with .copy(true)', function () {
it('should copy files instead of symlink', function* () {
var duo = build('copy', 'index.css').copy(true);
var file = path('copy/build/duo.png');
var out = read('copy/index.out.css');
var css = yield duo.run();
assert.equal(css.code, out);
var stat = yield fs.lstat(file);
assert(!stat.isSymbolicLink());
});
it('should not empty the target file when symlinked then copied. fixes: #356', function* () {
// symlink
var duo = build('copy', 'index.css').copy(false);
var original = read('copy/duo.png');
var file = path('copy/build/duo.png');
var css = yield duo.run();
assert(css);
var stat = yield fs.lstat(file);
assert.equal(original, read(file));
assert(stat.isSymbolicLink());
// copy
duo = build('copy', 'index.css').copy(true);
css = yield duo.run();
stat = yield fs.lstat(file);
assert(!stat.isSymbolicLink());
assert.equal(original, read(file));
assert.equal(original, read('copy/duo.png'));
});
});
describe('with .global(name)', function () {
it('should expose the entry as a global', function* () {
var duo = build('global');
duo.global('global-module');
var js = yield duo.run();
var ctx = evaluate(js.code);
assert.equal(ctx['global-module'], 'global module');
});
});
describe('with .cache(false)', function () {
it('should have an empty mapping', function* () {
var duo = build('idempotent').cache(false);
yield duo.run();
assert.deepEqual(duo.mapping, {});
});
});
describe('with .use(fn|gen)', function () {
it('should transform entry files', function* () {
var duo = build('coffee', 'index.coffee');
duo.use(cs);
var js = yield duo.run();
var ctx = evaluate(js.code).main;
assert.equal(ctx.a, 'a');
assert.equal(ctx.b, 'b');
assert.equal(ctx.c, 'c');
});
it('should transform deps', function* () {
var duo = build('coffee-deps');
duo.use(cs);
var js = yield duo.run();
var ctx = evaluate(js.code).main;
assert.equal(ctx.a, 'a');
assert.equal(ctx.b, 'b');
assert.equal(ctx.c, 'c');
});
it('should work with generators', function* () {
var duo = build('coffee', 'index.coffee');
var called = false;
duo.use(cs).use(function* () { called = true; });
var js = yield duo.run();
var ctx = evaluate(js.code).main;
assert.equal(ctx.a, 'a');
assert.equal(ctx.b, 'b');
assert.equal(ctx.c, 'c');
assert(called);
});
it('should work async', function* () {
var duo = build('no-deps');
var called = false;
duo.use(function (file, entry, fn) {
setTimeout(function () {
called = true;
fn();
}, 30);
});
yield duo.run();
assert(called);
});
it('should work sync', function* () {
var duo = build('no-deps');
var called = false;
duo.use(function () {
called = true;
});
yield duo.run();
assert(called);
});
it('should be idempotent across duos', function () {
var a = build('simple');
var b = build('no-deps');
// plugins
a.use(function ap() {});
b.use(function bp() {});
assert.equal(1, a.plugins.fns.length);
assert.equal(1, b.plugins.fns.length);
});
});
describe('with .use(alt)', function () {
it('should support calling "alternate" plugins on the resulting build', function* () {
var plugin = require(path('alt-plugin/plugin.js'));
var duo = build('alt-plugin').use(plugin);
var results = yield duo.run();
assert(/^\"use strict";\n\n/.test(results.code));
});
});
describe('with .include(name, source)', function () {
it('should include a string as a source', function* () {
var duo = build('includes');
duo.include('some-include', 'module.exports = "a"');
var js = yield duo.run();
var ctx = evaluate(js.code).main;
assert.equal(ctx, 'a');
});
it('should be idempotent', function* () {
var duo = build('includes');
duo.include('some-include', 'module.exports = "a"');
var js = yield duo.run();
var ctx = evaluate(js.code).main;
assert.equal(ctx, 'a');
js = yield duo.run();
ctx = evaluate(js.code).main;
assert.equal(ctx, 'a');
});
});
describe('with bundles', function () {
it('should support multiple bundles', function* () {
this.timeout(10000);
var 'one.js');
var two = build('bundles', 'two.js');
var one.run();
var twojs = yield two.run();
var ms = evaluate(onejs.code).main;
var type = evaluate(twojs.code).main;
assert.equal(36000000, ms('10h'));
assert.equal('string', type(''));
// Ensure both bundles are in the manifest
var json = yield mapping(one);
assert(json['one.js'], 'one.js not found in manifest');
assert(json['two.js'], 'two.js not found in manifest');
// don't let pack change the IDs of the deps
assert.equal(typeof json['one.js'].deps.ms, 'string');
assert.equal(typeof json['two.js'].deps.type, 'string');
});
});
describe('with css', function () {
it('should work with no deps', function* () {
var duo = build('css-no-deps', 'index.css');
var css = yield duo.run();
var out = read('css-no-deps/index.out.css');
assert.equal(css.code, out);
});
it('should resolve relative files', function* () {
var duo = build('css-relative-files', 'index.css');
var css = yield duo.run();
var out = read('css-relative-files/index.out.css');
assert.equal(css.code, out);
});
it('should resolve files with hashes and querystrings', function* () {
var duo = build('css-hash-query-files', 'index.css');
var css = yield duo.run();
var out = read('css-hash-query-files/index.out.css');
assert.equal(css.code.trim(), out.trim());
});
it('should support entry css transforms', function* () {
var duo = build('css-styl', 'index.styl');
duo.use(stylus);
var css = yield duo.run();
var out = read('css-styl/index.out.css');
assert.equal(css.code.trim(), out.trim());
});
it('should support css transforms', function* () {
var duo = build('css-styl-deps', 'index.css');
duo.use(stylus);
var css = yield duo.run();
var out = read('css-styl-deps/index.out.css');
assert.equal(css.code, out);
});
it('should load a simple dep', function* () {
this.timeout(15000);
var duo = build('css-simple-dep', 'index.css');
var css = yield duo.run();
var out = read('css-simple-dep/index.out.css');
assert.equal(css.code.trim(), out.trim());
});
it('should work with user/repo@ref:path', function* () {
this.timeout(15000);
var duo = build('user-repo-ref-path', 'index.css');
var css = yield duo.run();
var out = read('user-repo-ref-path/index.out.css');
assert.equal(css.code.trim(), out.trim());
});
it('should work with empty deps', function* () {
var duo = build('empty-css-file', 'index.css');
var css = yield duo.run();
var out = read('empty-css-file/index.out.css');
assert.equal(css.code, out);
});
it('should ignore http deps', function* () {
var duo = build('css-http-dep', 'index.css');
var css = yield duo.run();
var out = read('css-http-dep/index.out.css');
assert.equal(css.code, out);
});
it('should ignore image urls if asset is local', function* () {
var duo = build('css-url', 'lib/inline.css');
var css = yield duo.run();
var out = read('css-url/index.out.css');
assert.equal(css.code, out);
});
it('should keep duplicate references to assets', function* () {
var duo = build('css-dup-asset', 'index.css');
var css = yield duo.run();
var out = read('css-dup-asset/index.out.css');
assert.equal(css.code.trim(), out.trim());
});
it('should ignore unresolved remote paths', function* () {
var duo = build('css-ignore-unresolved', 'index.css');
var css = yield duo.run();
var out = read('css-ignore-unresolved/index.out.css');
assert.equal(css.code, out);
});
});
describe('with json', function () {
it('should load json files', function* () {
var duo = build('json-dep');
var js = yield duo.run();
var ctx = evaluate(js.code).main;
assert.equal(1, ctx.a);
assert.equal(2, ctx.b);
});
});
describe('with components', function () {
it('should build multi-asset components', function* () {
this.timeout(15000);
var duo = build('js-css-dep');
var js = yield duo.run();
var ctx = evaluate(js.code).main;
assert(ctx({}).dom);
duo = build('js-css-dep', 'index.css');
var out = read('js-css-dep/index.out.css');
var css = yield duo.run();
assert.equal(css.code.trim(), out.trim());
});
it('should build components with a main object', function* () {
var duo = build('main-obj');
var js = yield duo.run();
var ctx = evaluate(js.code).main;
assert.equal('local', ctx);
duo = build('main-obj', 'index.css');
var out = read('main-obj/index.out.css');
var css = yield duo.run();
assert.equal(css.code.trim(), out.trim());
});
it('should work on a full hybrid that triggers css-compat', function* () {
this.timeout(20000);