-
Notifications
You must be signed in to change notification settings - Fork 59
/
validate.ts
1080 lines (974 loc) Β· 32 KB
/
validate.ts
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
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
import fixturez from "fixturez";
import path from "path";
import validate from "../validate";
import {
logMock,
modifyPkg,
getPkg,
testdir,
js,
repoNodeModules,
getFiles,
} from "../../test-utils";
import { confirms as _confirms } from "../messages";
import { JSONValue } from "../utils";
const f = fixturez(__dirname);
jest.mock("../prompt");
afterEach(() => {
jest.resetAllMocks();
});
let confirms = _confirms as jest.Mocked<typeof _confirms>;
test("reports correct result on valid package", async () => {
let tmpPath = f.find("valid-package");
await validate(tmpPath);
expect(logMock.log.mock.calls).toMatchInlineSnapshot(`
[
[
"π info valid-package a valid entry point exists.",
],
[
"π info valid-package main field is valid",
],
[
"π info valid-package module field is valid",
],
[
"π info valid-package umd:main field is valid",
],
[
"π success project is valid!",
],
]
`);
});
test("no main field", async () => {
let tmpPath = await testdir({
"package.json": JSON.stringify({
name: "no-main-field",
license: "MIT",
private: true,
}),
"src/index.js": js`
// @flow
export default "something";
`,
});
try {
await validate(tmpPath);
} catch (e) {
expect(e).toMatchInlineSnapshot(
`[Error: main field was not found, expected \`"dist/no-main-field.cjs.js"\`]`
);
return;
}
expect(true).toBe(false);
});
test("no module", async () => {
let tmpPath = f.find("no-module");
await validate(tmpPath);
expect(logMock.log.mock.calls).toMatchInlineSnapshot(`
[
[
"π info no-module a valid entry point exists.",
],
[
"π info no-module main field is valid",
],
[
"π success project is valid!",
],
]
`);
});
test("invalid browser", async () => {
let tmpPath = f.copy("no-module");
await modifyPkg(tmpPath, (pkg) => {
pkg.browser = "invalid.js";
});
await expect(validate(tmpPath)).rejects.toMatchInlineSnapshot(
`[Error: browser field is invalid, found \`"invalid.js"\`, expected \`{"./dist/no-module.cjs.js":"./dist/no-module.browser.cjs.js"}\`]`
);
});
test("valid browser", async () => {
let tmpPath = f.copy("valid-package");
await modifyPkg(tmpPath, (pkg) => {
pkg.browser = {
["./dist/valid-package.cjs.js"]: "./dist/valid-package.browser.cjs.js",
["./dist/valid-package.esm.js"]: "./dist/valid-package.browser.esm.js",
};
});
await validate(tmpPath);
expect(logMock.log.mock.calls).toMatchInlineSnapshot(`
[
[
"π info valid-package a valid entry point exists.",
],
[
"π info valid-package main field is valid",
],
[
"π info valid-package module field is valid",
],
[
"π info valid-package umd:main field is valid",
],
[
"π info valid-package browser field is valid",
],
[
"π success project is valid!",
],
]
`);
});
test("monorepo single package", async () => {
let tmpPath = f.copy("monorepo-single-package");
await validate(tmpPath);
expect(logMock.log.mock.calls).toMatchInlineSnapshot(`
[
[
"π info @some-scope/package-two-single-package a valid entry point exists.",
],
[
"π info @some-scope/package-two-single-package main field is valid",
],
[
"π success project is valid!",
],
]
`);
});
test("one-entrypoint-with-browser-field-one-without", async () => {
let tmpPath = await testdir({
"package.json": JSON.stringify({
name: "one-entrypoint-with-browser-field-one-without",
main: "dist/one-entrypoint-with-browser-field-one-without.cjs.js",
module: "dist/one-entrypoint-with-browser-field-one-without.esm.js",
preconstruct: {
entrypoints: ["index.js", "multiply.js"],
},
}),
"multiply/package.json": JSON.stringify({
main:
"dist/one-entrypoint-with-browser-field-one-without-multiply.cjs.js",
module:
"dist/one-entrypoint-with-browser-field-one-without-multiply.esm.js",
browser: {
"./dist/one-entrypoint-with-browser-field-one-without-multiply.cjs.js":
"./dist/one-entrypoint-with-browser-field-one-without-multiply.browser.cjs.js",
"./dist/one-entrypoint-with-browser-field-one-without-multiply.esm.js":
"./dist/one-entrypoint-with-browser-field-one-without-multiply.browser.esm.js",
},
}),
"src/identity.js": js`
export let identity = (x) => x;
`,
"src/multiply.js": js`
import { identity } from "./identity";
export let multiply = (a, b) => identity(a * b);
`,
"src/index.js": js`
import { identity } from "./identity";
export let sum = (a, b) => identity(a + b);
`,
});
await expect(validate(tmpPath)).rejects.toMatchInlineSnapshot(
`[Error: one-entrypoint-with-browser-field-one-without/multiply has a browser build but one-entrypoint-with-browser-field-one-without does not have a browser build. Entrypoints in a package must either all have a particular build type or all not have a particular build type.]`
);
});
test("create package.json for an entrypoint", async () => {
let tmpPath = await testdir({
"package.json": JSON.stringify({
name: "entrypoint-pkg-json-missing",
main: "dist/entrypoint-pkg-json-missing.cjs.js",
module: "dist/entrypoint-pkg-json-missing.esm.js",
preconstruct: {
entrypoints: ["index.js", "other.js"],
},
}),
"src/index.js": js`
export default "something";
`,
"src/other.js": js`
export default "something";
`,
});
confirms.createEntrypointPkgJson.mockReturnValue(Promise.resolve(true));
await validate(tmpPath);
expect(confirms.createEntrypointPkgJson).toBeCalledTimes(1);
expect(await getPkg(path.join(tmpPath, "other"))).toMatchInlineSnapshot(`
{
"main": "dist/entrypoint-pkg-json-missing-other.cjs.js",
"module": "dist/entrypoint-pkg-json-missing-other.esm.js",
}
`);
});
test("monorepo umd with dep on other module incorrect peerDeps", async () => {
let tmpPath = await testdir({
"package.json": JSON.stringify({
name: "monorepo-umd-with-dep-incorrect-peerdeps",
main: "index.js",
workspaces: ["packages/*"],
preconstruct: {
packages: ["packages/*"],
globals: {
react: "React",
},
},
}),
"node_modules/@some-scope-incorrect-peerdeps/package-one-umd-with-dep": {
kind: "symlink",
path: "packages/package-one",
},
"node_modules/react": {
kind: "symlink",
path: path.join(repoNodeModules, "react"),
},
"packages/package-four/package.json": JSON.stringify({
name: "@some-scope-incorrect-peerdeps/package-four-umd-with-dep",
main:
"dist/some-scope-incorrect-peerdeps-package-four-umd-with-dep.cjs.js",
"umd:main":
"dist/some-scope-incorrect-peerdeps-package-four-umd-with-dep.umd.min.js",
preconstruct: {
umdName: "packageFour",
},
dependencies: {
"@some-scope-incorrect-peerdeps/package-one-umd-with-dep": "1.0.0",
},
}),
"packages/package-one/package.json": JSON.stringify({
name: "@some-scope-incorrect-peerdeps/package-one-umd-with-dep",
main:
"dist/some-scope-incorrect-peerdeps-package-one-umd-with-dep.cjs.js",
"umd:main":
"dist/some-scope-incorrect-peerdeps-package-one-umd-with-dep.umd.min.js",
preconstruct: {
umdName: "packageOne",
},
peerDependencies: {
react: "^16.6.3",
},
devDependencies: {
react: "^16.6.3",
},
}),
"packages/package-three/package.json": JSON.stringify({
name: "@some-scope-incorrect-peerdeps/package-three-umd-with-dep",
main:
"dist/some-scope-incorrect-peerdeps-package-three-umd-with-dep.cjs.js",
"umd:main":
"dist/some-scope-incorrect-peerdeps-package-three-umd-with-dep.umd.min.js",
preconstruct: {
umdName: "packageThree",
},
peerDependencies: {
"@some-scope-incorrect-peerdeps/package-one-umd-with-dep": "1.0.0",
},
devDependencies: {
"@some-scope-incorrect-peerdeps/package-one-umd-with-dep": "1.0.0",
},
}),
"packages/package-two/package.json": JSON.stringify({
name: "@some-scope-incorrect-peerdeps/package-two-umd-with-dep",
main:
"dist/some-scope-incorrect-peerdeps-package-two-umd-with-dep.cjs.js",
"umd:main":
"dist/some-scope-incorrect-peerdeps-package-two-umd-with-dep.umd.min.js",
preconstruct: {
umdName: "packageTwo",
},
peerDependencies: {
react: "^16.6.3",
},
devDependencies: {
react: "^16.6.3",
},
}),
"packages/package-four/src/index.js": js`
import "@some-scope/package-one-umd-with-dep";
`,
"packages/package-one/src/index.js": js`
import { createElement } from "react";
createElement("div", null);
`,
"packages/package-three/src/index.js": js`
import "@some-scope/package-one-umd-with-dep";
`,
"packages/package-two/src/index.js": js`
import { createElement } from "react";
createElement("h1", null);
`,
});
try {
await validate(tmpPath);
} catch (err) {
expect(err).toMatchInlineSnapshot(
`[Error: the package @some-scope-incorrect-peerdeps/package-four-umd-with-dep depends on @some-scope-incorrect-peerdeps/package-one-umd-with-dep which has a peerDependency on react but react is not specified in the dependencies or peerDependencies of @some-scope-incorrect-peerdeps/package-four-umd-with-dep. please add react to the dependencies or peerDependencies of @some-scope-incorrect-peerdeps/package-four-umd-with-dep]`
);
return;
}
expect(true).toBe(false);
});
test("dist not included in package", async () => {
let tmpPath = await testdir({
"package.json": JSON.stringify({
name: "dist-not-included-in-pkg",
main: "dist/dist-not-included-in-pkg.cjs.js",
module: "dist/dist-not-included-in-pkg.esm.js",
files: ["src"],
}),
"src/index.js": js`
export default "something";
`,
});
try {
await validate(tmpPath);
} catch (err) {
expect(err).toMatchInlineSnapshot(
`[Error: the dist directory isn't included in the published files for this package, please add it to the files field in the package's package.json]`
);
return;
}
expect(true).toBe(false);
});
test("entrypoint not included in package", async () => {
let tmpPath = await testdir({
"package.json": JSON.stringify({
name: "entrypoint-not-included-in-pkg",
main: "dist/entrypoint-not-included-in-pkg.cjs.js",
module: "dist/entrypoint-not-included-in-pkg.esm.js",
preconstruct: {
entrypoints: ["index.js", "multiply.js"],
},
files: ["dist"],
}),
"multiply/package.json": JSON.stringify({
main: "dist/entrypoint-not-included-in-pkg.cjs.js",
module: "dist/entrypoint-not-included-in-pkg.esm.js",
}),
"src/multiply.js": js`
export let multiply = (a, b) => a * b;
`,
"src/index.js": js`
export let sum = (a, b) => a + b;
`,
});
try {
await validate(tmpPath);
} catch (err) {
expect(err).toMatchInlineSnapshot(
`[Error: the entrypoint multiply isn't included in the published files for this package, please add it to the files field in the package's package.json]`
);
return;
}
expect(true).toBe(false);
});
test("root dist directory not included in package without entrypoint at root", async () => {
let tmpPath = await testdir({
"package.json": JSON.stringify({
name: "pkg-a",
files: ["other"],
preconstruct: {
entrypoints: ["other.js"],
},
}),
"other/package.json": JSON.stringify({
main: "dist/pkg-a.cjs.js",
}),
"src/other.js": js`
export let x = true;
`,
});
await expect(validate(tmpPath)).rejects.toMatchInlineSnapshot(`
[Error: the dist directory in the root of the package isn't included in the published files for this package, please add it to the files field in the package's package.json.
though this package does not have an entrypoint at the root of the package, preconstruct will write common chunks to the root dist directory so it must be included.]
`);
});
test("new entrypoints with old config", async () => {
let tmpPath = await testdir({
"package.json": JSON.stringify({
name: "pkg",
main: "dist/pkg.cjs.js",
module: "dist/pkg.esm.js",
preconstruct: {
source: "src/sum",
entrypoints: [".", "multiply"],
},
}),
"multiply/package.json": JSON.stringify({
main: "dist/pkg.cjs.js",
module: "dist/pkg.esm.js",
preconstruct: {
source: "../src/multiply.js",
},
}),
"src/multiply.js": js`
export let multiply = (a, b) => a * b;
`,
"src/sum.js": js`
export let sum = (a, b) => a + b;
`,
});
await expect(validate(tmpPath)).rejects.toMatchInlineSnapshot(
`[Error: this package has no entrypoints but it does have some using v1's entrypoints config, please see the the changelog for how to upgrade]`
);
});
test("multiple source files for same entrypoint", async () => {
let tmpPath = await testdir({
"package.json": JSON.stringify({
name: "pkg-a",
preconstruct: {
entrypoints: ["other.js", "other/index.js"],
},
}),
"other/package.json": JSON.stringify({
main: "dist/pkg-a.cjs.js",
}),
"src/other.js": js`
export let x = true;
`,
"src/other/index.js": js`
export let x = true;
`,
});
await expect(validate(tmpPath)).rejects.toMatchInlineSnapshot(
`[Error: this package has multiple source files for the same entrypoint of pkg-a/other at src/other.js and src/other/index.js]`
);
});
test("unexpected source option", async () => {
let tmpPath = await testdir({
"package.json": JSON.stringify({
name: "pkg-a",
preconstruct: {
source: "src/index.js",
},
}),
"src/index.js": js`
export let x = true;
`,
});
await expect(validate(tmpPath)).rejects.toMatchInlineSnapshot(
`[Error: The source option on entrypoints no longer exists, see the changelog for how to upgrade to the new entrypoints config]`
);
});
test("unexpected experimental flag", async () => {
let tmpPath = await testdir({
"package.json": JSON.stringify({
name: "pkg-a",
main: "dist/pkg-a.cjs.js",
preconstruct: {
___experimentalFlags_WILL_CHANGE_IN_PATCH: {
thisDoesNotExist: true,
},
},
}),
"src/index.js": js`
export let x = true;
`,
});
await expect(validate(tmpPath)).rejects.toMatchInlineSnapshot(
`[Error: The experimental flag "thisDoesNotExist" in your config does not exist]`
);
});
test("unexpected former experimental flag", async () => {
let tmpPath = await testdir({
"package.json": JSON.stringify({
name: "pkg-a",
main: "dist/pkg-a.cjs.js",
preconstruct: {
___experimentalFlags_WILL_CHANGE_IN_PATCH: {
newEntrypoints: true,
},
},
}),
"src/index.js": js`
export let x = true;
`,
});
await expect(validate(tmpPath)).rejects.toMatchInlineSnapshot(
`[Error: The behaviour from the experimental flag "newEntrypoints" is the current behaviour now, the flag should be removed]`
);
});
test("non-existant entrypoint", async () => {
let tmpPath = await testdir({
"package.json": JSON.stringify({
name: "pkg-a",
main: "dist/pkg-a.cjs.js",
preconstruct: {
entrypoints: ["index.js"],
},
}),
});
await expect(validate(tmpPath)).rejects.toMatchInlineSnapshot(
`[Error: π pkg-a specifies a entrypoint "index.js" but the file does not exist, please create it or fix the config]`
);
});
test("negated entrypoint", async () => {
let tmpPath = await testdir({
"package.json": JSON.stringify({
name: "pkg-a",
main: "dist/pkg-a.cjs.js",
preconstruct: {
entrypoints: ["index.js", "!index.js"],
},
}),
"src/index.js": "",
});
await expect(validate(tmpPath)).rejects.toMatchInlineSnapshot(
`[Error: π pkg-a specifies a entrypoint "index.js" but it is negated in the same config so it should be removed or the config should be fixed]`
);
});
test("old dist filenames", async () => {
let tmpPath = await testdir({
"package.json": JSON.stringify({
name: "@something/pkg-a",
main: "dist/pkg-a.cjs.js",
}),
"src/index.js": "",
});
await expect(validate(tmpPath)).rejects.toMatchInlineSnapshot(
`[Error: main field is invalid, found \`"dist/pkg-a.cjs.js"\`, expected \`"dist/something-pkg-a.cjs.js"\`]`
);
expect(logMock.log.mock.calls).toMatchInlineSnapshot(`
[
[
"π info @something/pkg-a a valid entry point exists.",
],
[
"π info it looks like you're using the dist filenames of Preconstruct v1, the default dist filename strategy has changed in v2",
],
[
"π info you can run preconstruct fix to use the new dist filenames",
],
[
"π info if you want to keep the dist filename strategy of v1, add \`"distFilenameStrategy": "unscoped-package-name"\` to the Preconstruct config in your root package.json",
],
]
`);
});
test("just wrong dist filenames doesn't report about the changed dist filename strategy", async () => {
let tmpPath = await testdir({
"package.json": JSON.stringify({
name: "@something/pkg-a",
main: "dist/pkg-a-blah.cjs.js",
}),
"src/index.js": "",
});
await expect(validate(tmpPath)).rejects.toMatchInlineSnapshot(
`[Error: main field is invalid, found \`"dist/pkg-a-blah.cjs.js"\`, expected \`"dist/something-pkg-a.cjs.js"\`]`
);
expect(logMock.log.mock.calls).toMatchInlineSnapshot(`
[
[
"π info @something/pkg-a a valid entry point exists.",
],
]
`);
});
describe("exports field config", () => {
const exportsFieldConfigTestDir = (config: JSONValue) => {
return testdir({
"package.json": JSON.stringify({
name: "pkg-a",
main: "dist/pkg-a.cjs.js",
module: "dist/pkg-a.esm.js",
exports: {
".": {
module: "./dist/pkg-a.esm.js",
default: "./dist/pkg-a.cjs.js",
},
"./package.json": "./package.json",
},
preconstruct: {
exports: config,
},
}),
"src/index.js": "",
});
};
describe("invalid", () => {
test("null", async () => {
const tmpPath = await exportsFieldConfigTestDir(null);
await expect(validate(tmpPath)).rejects.toMatchInlineSnapshot(
`[Error: the "preconstruct.exports" field must be a boolean or an object]`
);
});
test("some string", async () => {
const tmpPath = await exportsFieldConfigTestDir("blah");
await expect(validate(tmpPath)).rejects.toMatchInlineSnapshot(
`[Error: the "preconstruct.exports" field must be a boolean or an object]`
);
});
test("extra not object", async () => {
const tmpPath = await exportsFieldConfigTestDir({ extra: "blah" });
await expect(validate(tmpPath)).rejects.toMatchInlineSnapshot(
`[Error: the "preconstruct.exports.extra" field must be an object if it is present]`
);
});
test("envConditions not array", async () => {
const tmpPath = await exportsFieldConfigTestDir({ envConditions: {} });
await expect(validate(tmpPath)).rejects.toMatchInlineSnapshot(
`[Error: the "preconstruct.exports.envConditions" field must be an array containing zero or more of "worker" and "browser" if it is present]`
);
});
test("envConditions duplicates", async () => {
const tmpPath = await exportsFieldConfigTestDir({
envConditions: ["worker", "worker"],
});
await expect(validate(tmpPath)).rejects.toMatchInlineSnapshot(
`[Error: the "preconstruct.exports.envConditions" field must not have duplicates]`
);
});
test("envConditions invalid condition", async () => {
const tmpPath = await exportsFieldConfigTestDir({
envConditions: ["worker", "asfdasfd"],
});
await expect(validate(tmpPath)).rejects.toMatchInlineSnapshot(
`[Error: the "preconstruct.exports.envConditions" field must be an array containing zero or more of "worker" and "browser" if it is present]`
);
});
test("unknown key", async () => {
const tmpPath = await exportsFieldConfigTestDir({
something: true,
});
await expect(validate(tmpPath)).rejects.toMatchInlineSnapshot(
`[Error: the "preconstruct.exports" field contains an unknown key "something"]`
);
});
test("invalid importConditionDefaultExport", async () => {
const tmpPath = await exportsFieldConfigTestDir({
importConditionDefaultExport: "something",
});
await expect(validate(tmpPath)).rejects.toMatchInlineSnapshot(
`[Error: the "preconstruct.exports.importConditionDefaultExport" field must be set to "default" or "namespace" if it is present]`
);
});
});
describe("true", () => {
const configsEquivalentToTrue = [
{},
{ envConditions: [] },
{ envConditions: [], extra: {} },
{ extra: {} },
{ envConditions: [], importConditionDefaultExport: "namespace" },
{
envConditions: [],
extra: {},
importConditionDefaultExport: "namespace",
},
{ extra: {}, importConditionDefaultExport: "namespace" },
{ importConditionDefaultExport: "namespace" },
true,
];
for (const config of configsEquivalentToTrue) {
test(`${JSON.stringify(config)}`, async () => {
const tmpPath = await exportsFieldConfigTestDir(config);
await validate(tmpPath);
});
}
});
describe("false", () => {
const configsEquivalentToFalse = [false, undefined];
for (const config of configsEquivalentToFalse) {
test(`${JSON.stringify(config)}`, async () => {
const tmpPath = await testdir({
"package.json": JSON.stringify({
name: "pkg-a",
main: "dist/pkg-a.cjs.js",
module: "dist/pkg-a.esm.js",
preconstruct: {
exports: config,
},
}),
"src/index.js": "",
});
await validate(tmpPath);
});
}
});
test('{ "importConditionDefaultExport": "default" }', async () => {
const tmpPath = await testdir({
"package.json": JSON.stringify({
name: "pkg-a",
main: "dist/pkg-a.cjs.js",
module: "dist/pkg-a.esm.js",
exports: {
".": {
module: "./dist/pkg-a.esm.js",
import: "./dist/pkg-a.cjs.mjs",
default: "./dist/pkg-a.cjs.js",
},
"./package.json": "./package.json",
},
preconstruct: {
exports: {
importConditionDefaultExport: "default",
},
},
}),
"src/index.js": "",
});
await validate(tmpPath);
});
});
describe("project level exports field config", () => {
const exportsFieldConfigTestDir = (config: JSONValue) => {
return testdir({
"package.json": JSON.stringify({
name: "repo",
preconstruct: {
exports: config,
packages: ["packages/*"],
},
}),
"packages/pkg-a/package.json": JSON.stringify({
name: "pkg-a",
main: "dist/pkg-a.cjs.js",
module: "dist/pkg-a.esm.js",
exports: {
".": {
module: "./dist/pkg-a.esm.js",
default: "./dist/pkg-a.cjs.js",
},
"./package.json": "./package.json",
},
}),
"packages/pkg-a/src/index.js": "",
});
};
describe("invalid", () => {
test("null", async () => {
const tmpPath = await exportsFieldConfigTestDir(null);
await expect(validate(tmpPath)).rejects.toMatchInlineSnapshot(
`[Error: the "preconstruct.exports" field must be a boolean or an object]`
);
});
test("some string", async () => {
const tmpPath = await exportsFieldConfigTestDir("blah");
await expect(validate(tmpPath)).rejects.toMatchInlineSnapshot(
`[Error: the "preconstruct.exports" field must be a boolean or an object]`
);
});
test("extra", async () => {
const tmpPath = await exportsFieldConfigTestDir({
extra: {
"./blah": "./blah.js",
},
});
await expect(validate(tmpPath)).rejects.toMatchInlineSnapshot(
`[Error: the "preconstruct.exports.extra" field can only be configured at the package level]`
);
});
test("envConditions", async () => {
const tmpPath = await exportsFieldConfigTestDir({
envConditions: ["browser"],
});
await expect(validate(tmpPath)).rejects.toMatchInlineSnapshot(
`[Error: the "preconstruct.exports.envConditions" field can only be configured at the package level]`
);
});
test("unknown key", async () => {
const tmpPath = await exportsFieldConfigTestDir({
something: true,
});
await expect(validate(tmpPath)).rejects.toMatchInlineSnapshot(
`[Error: the "preconstruct.exports" field contains an unknown key "something"]`
);
});
test("invalid importConditionDefaultExport", async () => {
const tmpPath = await exportsFieldConfigTestDir({
importConditionDefaultExport: "something",
});
await expect(validate(tmpPath)).rejects.toMatchInlineSnapshot(
`[Error: the "preconstruct.exports.importConditionDefaultExport" field must be set to "default" or "namespace" if it is present]`
);
});
});
describe("true", () => {
const configsEquivalentToTrue = [
{},
{ importConditionDefaultExport: "namespace" },
true,
];
for (const config of configsEquivalentToTrue) {
test(`${JSON.stringify(config)}`, async () => {
const tmpPath = await exportsFieldConfigTestDir(config);
await validate(tmpPath);
});
}
});
test('{ "importConditionDefaultExport": "default" }', async () => {
const tmpPath = await testdir({
"package.json": JSON.stringify({
name: "repo",
preconstruct: {
exports: {
importConditionDefaultExport: "default",
},
packages: ["packages/*"],
},
}),
"packages/pkg-a/package.json": JSON.stringify({
name: "pkg-a",
main: "dist/pkg-a.cjs.js",
module: "dist/pkg-a.esm.js",
exports: {
".": {
module: "./dist/pkg-a.esm.js",
import: "./dist/pkg-a.cjs.mjs",
default: "./dist/pkg-a.cjs.js",
},
"./package.json": "./package.json",
},
}),
"packages/pkg-a/src/index.js": "",
});
await validate(tmpPath);
});
});
test("no module field with exports field", async () => {
const tmpPath = await testdir({
"package.json": JSON.stringify({
name: "pkg-a",
main: "dist/pkg-a.cjs.js",
preconstruct: {
exports: true,
},
}),
"src/index.js": "",
});
await expect(validate(tmpPath)).rejects.toMatchInlineSnapshot(
`[Error: when using the exports field, the module field must also be specified]`
);
});
test("has browser field but no browser condition", async () => {
const tmpPath = await testdir({
"package.json": JSON.stringify({
name: "pkg-a",
main: "dist/pkg-a.cjs.js",
module: "dist/pkg-a.esm.js",
browser: {
"./dist/pkg-a.cjs.js": "./dist/pkg-a.browser.cjs.js",
"./dist/pkg-a.esm.js": "./dist/pkg-a.browser.esm.js",
},
preconstruct: {
exports: true,
},
}),
"src/index.js": "",
});
await expect(validate(tmpPath)).rejects.toMatchInlineSnapshot(`
[Error: π pkg-a the exports field is configured and the browser field exists in this package but it is not specified in the preconstruct.exports.envConditions field
π pkg-a browser field is invalid, found \`{"./dist/pkg-a.cjs.js":"./dist/pkg-a.browser.cjs.js","./dist/pkg-a.esm.js":"./dist/pkg-a.browser.esm.js"}\`, expected \`{"./dist/pkg-a.esm.js":"./dist/pkg-a.browser.esm.js"}\`]
`);
});
test("has browser condition but no browser field", async () => {
const tmpPath = await testdir({
"package.json": JSON.stringify({
name: "pkg-a",
main: "dist/pkg-a.cjs.js",
module: "dist/pkg-a.esm.js",
preconstruct: {
exports: {
envConditions: ["browser"],
},
},
}),
"src/index.js": "",
});
await expect(validate(tmpPath)).rejects.toMatchInlineSnapshot(
`[Error: the exports field is configured and the browser condition is set in preconstruct.exports.envConditions but the field is not present at the top-level]`
);
});
test("preconstruct.exports: true no exports field", async () => {
const tmpPath = await testdir({
"package.json": JSON.stringify({
name: "pkg-a",
main: "dist/pkg-a.cjs.js",
module: "dist/pkg-a.esm.js",
preconstruct: {
exports: true,
},
}),
"src/index.js": "",
});
await expect(validate(tmpPath)).rejects.toMatchInlineSnapshot(
`[Error: exports field was not found, expected \`{".":{"module":"./dist/pkg-a.esm.js","default":"./dist/pkg-a.cjs.js"},"./package.json":"./package.json"}\`]`
);
});