/
css.js
636 lines (563 loc) Β· 16.2 KB
/
css.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
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
const assert = require('assert');
const path = require('path');
const fs = require('@parcel/fs');
const {
bundle,
run,
assertBundleTree,
rimraf,
ncp
} = require('@parcel/test-utils');
describe('css', function() {
it('should produce two bundles when importing a CSS file', async function() {
let b = await bundle(path.join(__dirname, '/integration/css/index.js'));
await assertBundleTree(b, {
name: 'index.js',
assets: ['index.js', 'index.css', 'local.js', 'local.css'],
childBundles: [
{
name: 'index.js.map'
},
{
name: 'index.css',
assets: ['index.css', 'local.css'],
childBundles: [
{
type: 'map'
}
]
}
]
});
let output = await run(b);
assert.equal(typeof output, 'function');
assert.equal(output(), 3);
});
it('should support loading a CSS bundle along side dynamic imports', async function() {
let b = await bundle(
path.join(__dirname, '/integration/dynamic-css/index.js')
);
await assertBundleTree(b, {
name: 'index.js',
assets: [
'index.js',
'index.css',
'bundle-loader.js',
'bundle-url.js',
'js-loader.js',
'css-loader.js'
],
childBundles: [
{
type: 'css',
name: 'index.css',
assets: ['index.css'],
childBundles: [
{
type: 'map'
}
]
},
{
name: 'index.js.map'
},
{
type: 'js',
assets: ['local.css', 'local.js'],
childBundles: [
{
type: 'css',
assets: ['local.css'],
childBundles: [
{
type: 'map'
}
]
},
{
type: 'map'
}
]
}
]
});
let output = await run(b);
assert.equal(typeof output, 'function');
assert.equal(await output(), 3);
});
it('should support importing CSS from a CSS file', async function() {
let b = await bundle(
path.join(__dirname, '/integration/css-import/index.js')
);
await assertBundleTree(b, {
name: 'index.js',
assets: ['index.js', 'index.css', 'other.css', 'local.css'],
childBundles: [
{
name: 'index.css',
assets: ['index.css', 'other.css', 'local.css'],
childBundles: [
{
type: 'map'
}
]
},
{
name: 'index.js.map',
type: 'map'
}
]
});
let output = await run(b);
assert.equal(typeof output, 'function');
assert.equal(output(), 2);
let css = await fs.readFile(
path.join(__dirname, '/dist/index.css'),
'utf8'
);
assert(css.includes('.local'));
assert(css.includes('.other'));
assert(/@media print {\s*.other/.test(css));
assert(css.includes('.index'));
});
it('should support linking to assets with url() from CSS', async function() {
let b = await bundle(path.join(__dirname, '/integration/css-url/index.js'));
await assertBundleTree(b, {
name: 'index.js',
assets: ['index.js', 'index.css'],
childBundles: [
{
name: 'index.css',
assets: ['index.css'],
childBundles: [
{
type: 'map'
}
]
},
{
type: 'map'
},
{
type: 'woff2',
assets: ['test.woff2'],
childBundles: []
}
]
});
let output = await run(b);
assert.equal(typeof output, 'function');
assert.equal(output(), 2);
let css = await fs.readFile(
path.join(__dirname, '/dist/index.css'),
'utf8'
);
assert(/url\("test\.[0-9a-f]+\.woff2"\)/.test(css));
assert(css.includes('url("http://google.com")'));
assert(css.includes('.index'));
assert(css.includes('url("data:image/gif;base64,quotes")'));
assert(css.includes('.quotes'));
assert(css.includes('url(data:image/gif;base64,no-quote)'));
assert(css.includes('.no-quote'));
assert(
await fs.exists(
path.join(
__dirname,
'/dist/',
css.match(/url\("(test\.[0-9a-f]+\.woff2)"\)/)[1]
)
)
);
});
it('should support linking to assets with url() from CSS in production', async function() {
let b = await bundle(
path.join(__dirname, '/integration/css-url/index.js'),
{
production: true
}
);
await assertBundleTree(b, {
name: 'index.js',
assets: ['index.js', 'index.css'],
childBundles: [
{
name: 'index.css',
assets: ['index.css'],
childBundles: [
{
type: 'map'
}
]
},
{
type: 'map'
},
{
type: 'woff2',
assets: ['test.woff2'],
childBundles: []
}
]
});
let output = await run(b);
assert.equal(typeof output, 'function');
assert.equal(output(), 2);
let css = await fs.readFile(
path.join(__dirname, '/dist/index.css'),
'utf8'
);
assert(/url\(test\.[0-9a-f]+\.woff2\)/.test(css), 'woff ext found in css');
assert(css.includes('url(http://google.com)'), 'url() found');
assert(css.includes('.index'), '.index found');
assert(css.includes('url("data:image/gif;base64,quotes")'));
assert(css.includes('.quotes'));
assert(css.includes('url(data:image/gif;base64,no-quote)'));
assert(css.includes('.no-quote'));
assert(
await fs.exists(
path.join(
__dirname,
'/dist/',
css.match(/url\((test\.[0-9a-f]+\.woff2)\)/)[1]
)
)
);
});
it('should support linking to assets in parent folders with url() from CSS', async function() {
let b = await bundle(
[
path.join(__dirname, '/integration/css-url-relative/src/a/style1.css'),
path.join(__dirname, '/integration/css-url-relative/src/b/style2.css')
],
{
production: true,
sourceMaps: false
}
);
await assertBundleTree(b, [
{
type: 'css',
assets: ['style1.css'],
childBundles: [
{
type: 'png'
}
]
},
{
type: 'css',
assets: ['style2.css']
}
]);
let css = await fs.readFile(
path.join(__dirname, '/dist/a/style1.css'),
'utf8'
);
assert(css.includes('background-image'), 'includes `background-image`');
assert(/url\([^)]*\)/.test(css), 'includes url()');
assert(
await fs.exists(
path.join(
__dirname,
path.dirname('/dist/a/style1.css'),
css.match(/url\(([^)]*)\)/)[1]
)
),
'path specified in url() exists'
);
});
it('should support transforming with postcss', async function() {
let b = await bundle(path.join(__dirname, '/integration/postcss/index.js'));
await assertBundleTree(b, {
name: 'index.js',
assets: ['index.js', 'index.css'],
childBundles: [
{
name: 'index.css',
assets: ['index.css'],
childBundles: [
{
type: 'map'
}
]
},
{
type: 'map'
}
]
});
let output = await run(b);
assert.equal(typeof output, 'function');
let value = output();
assert(/_index_[0-9a-z]/.test(value));
let cssClass = value.match(/(_index_[0-9a-z]+)/)[1];
let css = await fs.readFile(
path.join(__dirname, '/dist/index.css'),
'utf8'
);
assert(css.includes(`.${cssClass}`));
});
it('should support transforming with postcss twice with the same result', async function() {
let b = await bundle(
path.join(__dirname, '/integration/postcss-plugins/index.js')
);
let c = await bundle(
path.join(__dirname, '/integration/postcss-plugins/index2.js')
);
let [run1, run2] = await Promise.all([await run(b), await run(c)]);
assert.equal(run1(), run2());
});
it('should support postcss composes imports', async function() {
let b = await bundle(
path.join(__dirname, '/integration/postcss-composes/index.js')
);
await assertBundleTree(b, {
name: 'index.js',
assets: ['index.js', 'composes-1.css', 'composes-2.css', 'mixins.css'],
childBundles: [
{
name: 'index.css',
assets: ['composes-1.css', 'composes-2.css', 'mixins.css'],
childBundles: [
{
type: 'map'
}
]
},
{
type: 'map'
}
]
});
let output = await run(b);
assert.equal(typeof output, 'function');
let value = output();
const composes1Classes = value.composes1.split(' ');
const composes2Classes = value.composes2.split(' ');
assert(composes1Classes[0].startsWith('_composes1_'));
assert(composes1Classes[1].startsWith('_test_'));
assert(composes2Classes[0].startsWith('_composes2_'));
assert(composes2Classes[1].startsWith('_test_'));
let css = await fs.readFile(
path.join(__dirname, '/dist/index.css'),
'utf8'
);
let cssClass1 = value.composes1.match(/(_composes1_[0-9a-z]+)/)[1];
assert(css.includes(`.${cssClass1}`));
let cssClass2 = value.composes2.match(/(_composes2_[0-9a-z]+)/)[1];
assert(css.includes(`.${cssClass2}`));
});
it('should not include css twice for postcss composes imports', async function() {
let b = await bundle(
path.join(__dirname, '/integration/postcss-composes/index.js')
);
await run(b);
let css = await fs.readFile(
path.join(__dirname, '/dist/index.css'),
'utf8'
);
assert.equal(
css.indexOf('height: 100px;'),
css.lastIndexOf('height: 100px;')
);
});
it('should support postcss composes imports for sass', async function() {
let b = await bundle(
path.join(__dirname, '/integration/postcss-composes/index2.js')
);
await assertBundleTree(b, {
name: 'index2.js',
assets: ['index2.js', 'composes-3.css', 'mixins.scss'],
childBundles: [
{
name: 'index2.css',
assets: ['composes-3.css', 'mixins.scss'],
childBundles: [
{
type: 'map'
}
]
},
{
type: 'map'
}
]
});
let output = await run(b);
assert.equal(typeof output, 'function');
let value = output();
const composes3Classes = value.composes3.split(' ');
assert(composes3Classes[0].startsWith('_composes3_'));
assert(composes3Classes[1].startsWith('_test_'));
let css = await fs.readFile(
path.join(__dirname, '/dist/index2.css'),
'utf8'
);
assert(css.includes('height: 200px;'));
});
it('should support postcss composes imports with custom path names', async function() {
let b = await bundle(
path.join(__dirname, '/integration/postcss-composes/index3.js')
);
await assertBundleTree(b, {
name: 'index3.js',
assets: ['index3.js', 'composes-4.css', 'mixins.css'],
childBundles: [
{
name: 'index3.css',
assets: ['composes-4.css', 'mixins.css'],
childBundles: [
{
type: 'map'
}
]
},
{
type: 'map'
}
]
});
let output = await run(b);
assert.equal(typeof output, 'function');
let value = output();
const composes4Classes = value.composes4.split(' ');
assert(composes4Classes[0].startsWith('_composes4_'));
assert(composes4Classes[1].startsWith('_test_'));
let css = await fs.readFile(
path.join(__dirname, '/dist/index3.css'),
'utf8'
);
assert(css.includes('height: 100px;'));
});
it('should support deep nested postcss composes imports', async function() {
let b = await bundle(
path.join(__dirname, '/integration/postcss-composes/index4.js')
);
await assertBundleTree(b, {
name: 'index4.js',
assets: [
'index4.js',
'composes-5.css',
'mixins-intermediate.css',
'mixins.css'
],
childBundles: [
{
name: 'index4.css',
assets: ['composes-5.css', 'mixins-intermediate.css', 'mixins.css'],
childBundles: [
{
type: 'map'
}
]
},
{
type: 'map'
}
]
});
let output = await run(b);
assert.equal(typeof output, 'function');
let value = output();
const composes5Classes = value.composes5.split(' ');
assert(composes5Classes[0].startsWith('_composes5_'));
assert(composes5Classes[1].startsWith('_intermediate_'));
assert(composes5Classes[2].startsWith('_test_'));
let css = await fs.readFile(
path.join(__dirname, '/dist/index4.css'),
'utf8'
);
assert(css.includes('height: 100px;'));
assert(css.includes('height: 300px;'));
assert(css.indexOf('._test_') < css.indexOf('._intermediate_'));
});
it('should support postcss composes imports for multiple selectors', async function() {
let b = await bundle(
path.join(__dirname, '/integration/postcss-composes/index5.js')
);
await assertBundleTree(b, {
name: 'index5.js',
assets: ['index5.js', 'composes-6.css', 'mixins.css'],
childBundles: [
{
name: 'index5.css',
assets: ['composes-6.css', 'mixins.css'],
childBundles: [
{
type: 'map'
}
]
},
{
type: 'map'
}
]
});
let output = await run(b);
assert.equal(typeof output, 'function');
let value = output();
const composes6Classes = value.composes6.split(' ');
assert(composes6Classes[0].startsWith('_composes6_'));
assert(composes6Classes[1].startsWith('_test_'));
assert(composes6Classes[2].startsWith('_test-2_'));
});
it('should minify CSS in production mode', async function() {
let b = await bundle(
path.join(__dirname, '/integration/cssnano/index.js'),
{
production: true
}
);
let output = await run(b);
assert.equal(typeof output, 'function');
assert.equal(output(), 3);
let css = await fs.readFile(
path.join(__dirname, '/dist/index.css'),
'utf8'
);
assert(css.includes('.local'));
assert(css.includes('.index'));
assert.equal(css.split('\n').length, 2); // sourceMappingURL
});
it('should automatically install postcss plugins with npm if needed', async function() {
await rimraf(path.join(__dirname, '/input'));
await ncp(
path.join(__dirname, '/integration/autoinstall/npm'),
path.join(__dirname, '/input')
);
await bundle(path.join(__dirname, '/input/index.css'));
// cssnext was installed
let pkg = require('./input/package.json');
assert(pkg.devDependencies['postcss-cssnext']);
// peer dependency caniuse-lite was installed
assert(pkg.devDependencies['caniuse-lite']);
// cssnext is applied
let css = await fs.readFile(
path.join(__dirname, '/dist/index.css'),
'utf8'
);
assert(css.includes('rgba'));
});
it('should automatically install postcss plugins with yarn if needed', async function() {
await rimraf(path.join(__dirname, '/input'));
await ncp(
path.join(__dirname, '/integration/autoinstall/yarn'),
path.join(__dirname, '/input')
);
await bundle(path.join(__dirname, '/input/index.css'));
// cssnext was installed
let pkg = require('./input/package.json');
assert(pkg.devDependencies['postcss-cssnext']);
// peer dependency caniuse-lite was installed
assert(pkg.devDependencies['caniuse-lite']);
// appveyor is not currently writing to the yarn.lock file and will require further investigation
// let lockfile = await fs.readFile(path.join(__dirname, '/input/yarn.lock'), 'utf8');
// assert(lockfile.includes('postcss-cssnext'));
// cssnext is applied
let css = await fs.readFile(
path.join(__dirname, '/dist/index.css'),
'utf8'
);
assert(css.includes('rgba'));
});
});