-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Expand file tree
/
Copy pathcss-modules.js
More file actions
779 lines (695 loc) · 20.5 KB
/
Copy pathcss-modules.js
File metadata and controls
779 lines (695 loc) · 20.5 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
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
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
import assert from 'assert';
import path from 'path';
import {
bundle,
run,
runBundle,
assertBundles,
distDir,
outputFS,
} from '@parcel/test-utils';
import postcss from 'postcss';
describe('css modules', () => {
it('should support transforming css modules (require)', async () => {
let b = await bundle(
path.join(__dirname, '/integration/postcss-modules-cjs/index.js'),
);
assertBundles(b, [
{
name: 'index.js',
assets: ['index.js', 'foo.module.css'],
},
{
name: 'index.css',
assets: ['index.css', 'foo.module.css'],
},
]);
let output = await run(b);
assert.equal(typeof output, 'function');
let value = output();
assert(/[_0-9a-zA-Z]+_foo/.test(value));
let cssClass = value.match(/([_0-9a-zA-Z]+_foo)/)[1];
let css = await outputFS.readFile(path.join(distDir, 'index.css'), 'utf8');
assert(css.includes(`.${cssClass}`));
});
it('should support transforming css modules (import default)', async () => {
let b = await bundle(
path.join(
__dirname,
'/integration/postcss-modules-import-default/index.js',
),
{mode: 'production'},
);
assertBundles(b, [
{
name: 'index.js',
assets: ['index.js', 'style.module.css'],
},
{
name: 'index.css',
assets: ['style.module.css'],
},
]);
let output = await run(b);
assert(/[_0-9a-zA-Z]+_b-2/.test(output));
let css = await outputFS.readFile(
b.getBundles().find(b => b.type === 'css').filePath,
'utf8',
);
let includedRules = new Set();
postcss.parse(css).walkRules(rule => {
includedRules.add(rule.selector);
});
assert(includedRules.has('.page'));
assert(includedRules.has(`.${output}`));
});
it('should tree shake unused css modules classes with a namespace import', async () => {
let b = await bundle(
path.join(
__dirname,
'/integration/postcss-modules-import-namespace/index.js',
),
{mode: 'production'},
);
assertBundles(b, [
{
name: 'index.js',
assets: ['index.js', 'style.module.css'],
},
{
name: 'index.css',
assets: ['global.css', 'style.module.css'],
},
]);
let js = await outputFS.readFile(
b.getBundles().find(b => b.type === 'js').filePath,
'utf8',
);
assert(!js.includes('unused'));
let output = await run(b);
assert(/[_0-9a-zA-Z]+_b-2/.test(output));
let css = await outputFS.readFile(
b.getBundles().find(b => b.type === 'css').filePath,
'utf8',
);
let includedRules = new Set();
postcss.parse(css).walkRules(rule => {
includedRules.add(rule.selector);
});
assert.deepStrictEqual(
includedRules,
new Set(['body', `.${output}`, '.page']),
);
});
it('should produce correct css without symbol propagation for css modules classes with a namespace import', async () => {
let b = await bundle(
path.join(
__dirname,
'/integration/postcss-modules-import-namespace/index.js',
),
{
mode: 'production',
defaultTargetOptions: {
shouldScopeHoist: false,
},
},
);
assertBundles(b, [
{
name: 'index.js',
assets: ['index.js', 'style.module.css'],
},
{
name: 'index.css',
assets: ['global.css', 'style.module.css'],
},
]);
let {output} = await run(b, null, {require: false});
assert(/[_0-9a-zA-Z]+_b-2/.test(output));
let css = await outputFS.readFile(
b.getBundles().find(b => b.type === 'css').filePath,
'utf8',
);
let includedRules = new Set();
postcss.parse(css).walkRules(rule => {
includedRules.add(rule.selector);
});
assert(includedRules.has('body'));
assert(includedRules.has(`.${output}`));
assert(includedRules.has('.page'));
});
it('should support importing css modules with a non-static namespace import', async () => {
let b = await bundle(
path.join(
__dirname,
'/integration/postcss-modules-import-namespace-whole/index.js',
),
{mode: 'production'},
);
assertBundles(b, [
{
name: 'index.js',
assets: ['index.js', 'style.module.css'],
},
{
name: 'index.css',
assets: ['global.css', 'style.module.css'],
},
]);
let js = await outputFS.readFile(
b.getBundles().find(b => b.type === 'js').filePath,
'utf8',
);
assert(js.includes('unused'));
let output = await run(b);
assert(/[_0-9a-zA-Z]+_b-2/.test(output['b-2']));
assert(/[_0-9a-zA-Z]+_unused/.test(output['unused']));
let css = await outputFS.readFile(
b.getBundles().find(b => b.type === 'css').filePath,
'utf8',
);
let includedRules = new Set();
postcss.parse(css).walkRules(rule => {
includedRules.add(rule.selector);
});
assert.deepStrictEqual(
includedRules,
new Set(['body', `.${output['b-2']}`, `.${output['unused']}`, '.page']),
);
});
it('should support css modules composes imports', async () => {
let b = await bundle(
path.join(__dirname, '/integration/postcss-composes/index.js'),
);
assertBundles(b, [
{
name: 'index.js',
assets: [
'index.js',
'composes-1.module.css',
'composes-2.module.css',
'mixins.module.css',
],
},
{
name: 'index.css',
assets: [
'composes-1.module.css',
'composes-2.module.css',
'mixins.module.css',
],
},
]);
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].endsWith('_composes1'));
assert(composes1Classes[1].endsWith('_test'));
assert(composes2Classes[0].endsWith('_composes2'));
assert(composes2Classes[1].endsWith('_test'));
let css = await outputFS.readFile(path.join(distDir, 'index.css'), 'utf8');
let cssClass1 = value.composes1.match(/([_0-9a-zA-Z]+_composes1)/)[1];
assert(css.includes(`.${cssClass1}`));
let cssClass2 = value.composes2.match(/([_0-9a-zA-Z]+_composes2)/)[1];
assert(css.includes(`.${cssClass2}`));
});
it('should not include css twice for composes imports', async () => {
let b = await bundle(
path.join(__dirname, '/integration/postcss-composes/index.js'),
);
await run(b);
let css = await outputFS.readFile(path.join(distDir, 'index.css'), 'utf8');
assert.equal(
css.indexOf('height: 100px;'),
css.lastIndexOf('height: 100px;'),
);
});
it('should support composes imports for sass', async () => {
let b = await bundle(
path.join(__dirname, '/integration/postcss-composes/index2.js'),
);
assertBundles(b, [
{
name: 'index2.js',
assets: ['index2.js', 'composes-3.module.css', 'mixins.module.scss'],
},
{
name: 'index2.css',
assets: ['composes-3.module.css', 'mixins.module.scss'],
},
]);
let output = await run(b);
assert.equal(typeof output, 'function');
let value = output();
const composes3Classes = value.composes3.split(' ');
assert(composes3Classes[0].endsWith('_composes3'));
assert(composes3Classes[1].endsWith('_test'));
let css = await outputFS.readFile(path.join(distDir, 'index2.css'), 'utf8');
assert(css.includes('height: 200px;'));
});
it('should support composes imports with custom path names', async () => {
let b = await bundle(
path.join(__dirname, '/integration/postcss-composes/index3.js'),
);
assertBundles(b, [
{
name: 'index3.js',
assets: ['index3.js', 'composes-4.module.css', 'mixins.module.css'],
},
{
name: 'index3.css',
assets: ['composes-4.module.css', 'mixins.module.css'],
},
]);
let output = await run(b);
assert.equal(typeof output, 'function');
let value = output();
const composes4Classes = value.composes4.split(' ');
assert(composes4Classes[0].endsWith('_composes4'));
assert(composes4Classes[1].endsWith('_test'));
let css = await outputFS.readFile(path.join(distDir, 'index3.css'), 'utf8');
assert(css.includes('height: 100px;'));
});
it('should support deep nested composes imports', async () => {
let b = await bundle(
path.join(__dirname, '/integration/postcss-composes/index4.js'),
);
assertBundles(b, [
{
name: 'index4.js',
assets: [
'index4.js',
'composes-5.module.css',
'mixins-intermediate.module.css',
'mixins.module.css',
],
},
{
name: 'index4.css',
assets: [
'composes-5.module.css',
'mixins-intermediate.module.css',
'mixins.module.css',
],
},
]);
let output = await run(b);
assert.equal(typeof output, 'function');
let value = output();
const composes5Classes = value.composes5.split(' ');
assert(composes5Classes[0].endsWith('_composes5'));
assert(composes5Classes[1].endsWith('_intermediate'));
assert(composes5Classes[2].endsWith('_test'));
let css = await outputFS.readFile(path.join(distDir, 'index4.css'), 'utf8');
assert(css.includes('height: 100px;'));
assert(css.includes('height: 300px;'));
assert(css.indexOf('_test') < css.indexOf('_intermediate'));
assert(css.indexOf('_intermediate') < css.indexOf('_composes5'));
});
it('should support composes imports for multiple selectors', async () => {
let b = await bundle(
path.join(__dirname, '/integration/postcss-composes/index5.js'),
);
assertBundles(b, [
{
name: 'index5.js',
assets: ['index5.js', 'composes-6.module.css', 'mixins.module.css'],
},
{
name: 'index5.css',
assets: ['composes-6.module.css', 'mixins.module.css'],
},
]);
let output = await run(b);
assert.equal(typeof output, 'function');
let value = output();
const composes6Classes = value.composes6.split(' ');
assert(composes6Classes[0].endsWith('_composes6'));
assert(composes6Classes[1].endsWith('_test'));
assert(composes6Classes[2].endsWith('_test-2'));
});
it('should throw an error when importing a missing class', async function () {
await assert.rejects(
() =>
bundle(
path.join(
__dirname,
'/integration/no-export-error-with-correct-filetype/src/App.jsx',
),
{
shouldDisableCache: true,
defaultTargetOptions: {
shouldScopeHoist: true,
},
},
),
{
name: 'BuildError',
diagnostics: [
{
codeFrames: [
{
filePath: path.join(
__dirname,
'/integration/no-export-error-with-correct-filetype/src/App.jsx',
),
language: 'js',
codeHighlights: [
{
message: undefined,
end: {
column: 45,
line: 7,
},
start: {
column: 28,
line: 7,
},
},
],
},
],
message:
"integration/no-export-error-with-correct-filetype/src/app.module.css does not export 'notExisting'",
origin: '@parcel/core',
},
],
},
);
});
it('should fall back to postcss for legacy css modules', async function () {
let b = await bundle(
path.join(__dirname, '/integration/css-modules-legacy/index.js'),
);
assertBundles(b, [
{
name: 'index.js',
assets: ['index.js', 'index.module.css'],
},
{
name: 'index.css',
assets: ['index.module.css'],
},
]);
let css = await outputFS.readFile(path.join(distDir, 'index.css'), 'utf8');
assert(css.includes('color: red'));
});
it('should fall back to postcss for legacy css modules with :export', async function () {
let b = await bundle(
path.join(__dirname, '/integration/css-modules-legacy/b.js'),
);
assertBundles(b, [
{
name: 'b.js',
assets: ['b.js', 'b.module.css'],
},
{
name: 'b.css',
assets: ['b.module.css'],
},
]);
let res = await run(b);
assert.deepEqual(res, {color: 'red'});
});
it('should optimize away unused @keyframes', async function () {
let b = await bundle(
path.join(__dirname, '/integration/css-modules-keyframes/index.js'),
{
mode: 'production',
},
);
assertBundles(b, [
{
name: 'index.js',
assets: ['index.js', 'index.module.css'],
},
{
name: 'index.css',
assets: ['index.module.css'],
},
]);
let css = await outputFS.readFile(path.join(distDir, 'index.css'), 'utf8');
assert(/@keyframes _[_0-9a-zA-Z]+_test/.test(css));
assert(!css.includes('unused'));
});
it('should not double optimize css modules processed with postcss', async function () {
let b = await bundle(
path.join(__dirname, '/integration/postcss-modules-optimize/index.js'),
{
mode: 'production',
},
);
assertBundles(b, [
{
name: 'index.js',
assets: ['index.js', 'index.css'],
},
{
name: 'index.css',
assets: ['index.css'],
},
]);
let css = await outputFS.readFile(path.join(distDir, 'index.css'), 'utf8');
assert(css.includes('@keyframes test'));
assert(css.includes('@keyframes unused'));
});
it('should compile css modules for multiple targets', async function () {
let b = await bundle(
path.join(__dirname, '/integration/css-modules-targets/index.html'),
{
mode: 'production',
},
);
assertBundles(b, [
{
name: 'index.html',
assets: ['index.html'],
},
{
type: 'js',
assets: ['index.js', 'foo.module.css'],
},
{
type: 'js',
assets: ['index.js', 'foo.module.css'],
},
{
type: 'css',
assets: ['foo.module.css'],
},
]);
});
it('should not fail with many css modules', async function () {
let b = await bundle(
path.join(__dirname, '/integration/css-modules-bug/src/index.html'),
);
assertBundles(b, [
{
name: 'index.html',
assets: ['index.html'],
},
{
type: 'js',
assets: [
'button.module.css',
'main.js',
'main.module.css',
'other.module.css',
],
},
{
type: 'css',
assets: ['button.module.css', 'main.module.css', 'other.module.css'],
},
]);
});
// Forked because experimental bundler will not merge bundles of same types if they do not share all their bundlegroups
it('should handle @import in css modules', async function () {
let b = await bundle(
[
path.join(__dirname, '/integration/css-modules-import/page1.html'),
path.join(__dirname, '/integration/css-modules-import/page2.html'),
],
{mode: 'production'},
);
let res = [];
await runBundle(
b,
b.getBundles().find(b => b.name === 'page1.html'),
{
sideEffect: s => res.push(s),
},
);
assert.deepEqual(res, [['page1', '_1ZEqVW_a']]);
res = [];
await runBundle(
b,
b.getBundles().find(b => b.name === 'page2.html'),
{
sideEffect: s => res.push(s),
},
);
assert.deepEqual(res, [['page2', '_4fY2uG_foo _1ZEqVW_foo j1UkRG_foo']]);
assertBundles(b, [
{
name: 'page1.html',
assets: ['page1.html'],
},
{
name: 'page2.html',
assets: ['page2.html'],
},
{
type: 'js',
assets: [
'page1.js',
'index.module.css',
'a.module.css',
'b.module.css',
],
},
{
type: 'js',
assets: [
'page2.js',
'index.module.css',
'a.module.css',
'b.module.css',
],
},
{
type: 'css',
assets: ['a.module.css', 'b.module.css'],
},
{
type: 'css',
assets: ['index.module.css'],
},
]);
});
it('should not process inline <style> elements as a CSS module', async function () {
await bundle(
path.join(__dirname, '/integration/css-modules-style/index.html'),
);
let contents = await outputFS.readFile(
path.join(distDir, 'index.html'),
'utf8',
);
assert(contents.includes('.index {'));
});
it('should support global css modules via boolean config', async function () {
let b = await bundle(
path.join(__dirname, '/integration/css-modules-global/a/index.js'),
{mode: 'production'},
);
let res = await run(b);
assert.deepEqual(res, 'C-gzXq_foo');
let contents = await outputFS.readFile(
b.getBundles().find(b => b.type === 'css').filePath,
'utf8',
);
assert(contents.includes('.C-gzXq_foo'));
assert(contents.includes('.x'));
});
it('should support global css modules via object config', async function () {
let b = await bundle(
path.join(__dirname, '/integration/css-modules-global/b/index.js'),
{mode: 'production'},
);
let res = await run(b);
assert.deepEqual(res, 'C-gzXq_foo');
let contents = await outputFS.readFile(
b.getBundles().find(b => b.type === 'css').filePath,
'utf8',
);
assert(contents.includes('.C-gzXq_foo'));
assert(contents.includes('.x'));
});
it('should optimize away unused variables when dashedIdents option is used', async function () {
let b = await bundle(
path.join(__dirname, '/integration/css-modules-vars/index.js'),
{mode: 'production'},
);
let contents = await outputFS.readFile(
b.getBundles().find(b => b.type === 'css').filePath,
'utf8',
);
assert.equal(
contents.split('\n')[0],
':root{--wGsoEa_color:red;--wGsoEa_font:Helvetica;--wGsoEa_theme-sizes-1\\/12:2;--wGsoEa_from-js:purple}body{font:var(--wGsoEa_font)}._4fY2uG_foo{color:var(--wGsoEa_color);width:var(--wGsoEa_theme-sizes-1\\/12);height:var(--height)}',
);
let res = await run(b);
assert.deepEqual(res, ['_4fY2uG_foo', '--wGsoEa_from-js']);
});
it('should group together css and css modules into one bundle', async function () {
let b = await bundle(
path.join(__dirname, '/integration/css-module-css-siblings/index.html'),
);
let res = [];
await runBundle(
b,
b.getBundles().find(b => b.name === 'index.html'),
{
sideEffect: s => res.push(s),
},
);
assert.deepEqual(res, [
['mainJs', '_1ZEqVW_myClass', 'j1UkRG_myOtherClass'],
]);
});
it('should bundle css modules siblings together and their JS assets', async function () {
// This issue was first documented here
// https://github.com/parcel-bundler/parcel/issues/8716
let b = await bundle(
path.join(
__dirname,
'/integration/css-modules-merging-siblings/index.html',
),
);
let res = [];
await runBundle(
b,
b.getBundles().find(b => b.name === 'index.html'),
{
sideEffect: s => res.push(s),
},
);
// Result is [ 'mainJs', 'SX8vmq_container YpGmra_-expand' ]
assert.deepEqual(res[0][0], 'mainJs');
assert(res[0][1].includes('container') && res[0][1].includes('expand'));
});
it('should allow css modules to be shared between targets', async function () {
let b = await bundle([
path.join(__dirname, '/integration/css-module-self-references/a'),
path.join(__dirname, '/integration/css-module-self-references/b'),
]);
assertBundles(b, [
{
name: 'main.css',
assets: ['bar.module.css'],
},
{
name: 'main.css',
assets: ['bar.module.css'],
},
{
name: 'main.js',
assets: ['index.js', 'bar.module.css'],
},
{
name: 'main.js',
assets: ['index.js', 'bar.module.css'],
},
{
name: 'module.js',
assets: ['index.js', 'bar.module.css'],
},
{
name: 'module.js',
assets: ['index.js', 'bar.module.css'],
},
]);
});
});