/
zod.ts
1180 lines (1038 loc) · 34.4 KB
/
zod.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 { RawFuncUseCtx } from "@rspack/binding";
import { z } from "zod";
import { Compilation, Compiler } from "..";
import type * as oldBuiltins from "../builtin-plugin";
import type * as webpackDevServer from "webpack-dev-server";
import { deprecatedWarn, termlink } from "../util";
import { Module } from "../Module";
import { Chunk } from "../Chunk";
//#region Name
const name = z.string();
export type Name = z.infer<typeof name>;
//#endregion
//#region Dependencies
const dependencies = z.array(name);
export type Dependencies = z.infer<typeof dependencies>;
//#endregion
//#region Context
const context = z.string();
export type Context = z.infer<typeof context>;
//#endregion
//#region Mode
const mode = z.enum(["development", "production", "none"]);
export type Mode = z.infer<typeof mode>;
//#endregion
//#region Falsy
const falsy = z.union([
z.literal(false),
z.literal(0),
z.literal(""),
z.null(),
z.undefined()
]);
export type Falsy = z.infer<typeof falsy>;
//#endregion
//#region Entry
const rawPublicPath = z.string();
export type RawPublicPath = z.infer<typeof rawPublicPath>;
const publicPath = z.literal("auto").or(rawPublicPath);
export type PublicPath = z.infer<typeof publicPath>;
const baseUri = z.string();
export type BaseUri = z.infer<typeof baseUri>;
const chunkLoadingType = z
.enum(["jsonp", "import-scripts", "require", "async-node", "import"])
.or(z.string());
export type ChunkLoadingType = z.infer<typeof chunkLoadingType>;
const chunkLoading = z.literal(false).or(chunkLoadingType);
export type ChunkLoading = z.infer<typeof chunkLoading>;
const asyncChunks = z.boolean();
export type AsyncChunks = z.infer<typeof asyncChunks>;
const wasmLoadingType = z
.enum(["fetch-streaming", "fetch", "async-node"])
.or(z.string());
export type WasmLoadingType = z.infer<typeof wasmLoadingType>;
const wasmLoading = z.literal(false).or(wasmLoadingType);
export type WasmLoading = z.infer<typeof wasmLoading>;
const scriptType = z.enum(["text/javascript", "module"]).or(z.literal(false));
export type ScriptType = z.infer<typeof scriptType>;
const libraryCustomUmdObject = z.strictObject({
amd: z.string().optional(),
commonjs: z.string().optional(),
root: z.string().or(z.array(z.string())).optional()
});
export type LibraryCustomUmdObject = z.infer<typeof libraryCustomUmdObject>;
const libraryName = z
.string()
.or(z.array(z.string()))
.or(libraryCustomUmdObject);
export type LibraryName = z.infer<typeof libraryName>;
const libraryCustomUmdCommentObject = z.strictObject({
amd: z.string().optional(),
commonjs: z.string().optional(),
commonjs2: z.string().optional(),
root: z.string().optional()
});
export type LibraryCustomUmdCommentObject = z.infer<
typeof libraryCustomUmdCommentObject
>;
const amdContainer = z.string();
export type AmdComtainer = z.infer<typeof amdContainer>;
const auxiliaryComment = z.string().or(libraryCustomUmdCommentObject);
export type AuxiliaryComment = z.infer<typeof auxiliaryComment>;
const libraryExport = z.string().or(z.array(z.string()));
export type LibraryExport = z.infer<typeof libraryExport>;
const libraryType = z
.enum([
"var",
"module",
"assign",
"assign-properties",
"this",
"window",
"self",
"global",
"commonjs",
"commonjs2",
"commonjs-module",
"commonjs-static",
"amd",
"amd-require",
"umd",
"umd2",
"jsonp",
"system"
])
.or(z.string());
export type LibraryType = z.infer<typeof libraryType>;
const umdNamedDefine = z.boolean();
export type UmdNamedDefine = z.infer<typeof umdNamedDefine>;
const libraryOptions = z.strictObject({
amdContainer: amdContainer.optional(),
auxiliaryComment: auxiliaryComment.optional(),
export: libraryExport.optional(),
name: libraryName.optional(),
type: libraryType,
umdNamedDefine: umdNamedDefine.optional()
});
export type LibraryOptions = z.infer<typeof libraryOptions>;
const library = libraryName.or(libraryOptions).optional();
export type Library = z.infer<typeof library>;
const filenameTemplate = z.string();
export type FilenameTemplate = z.infer<typeof filenameTemplate>;
const filename = filenameTemplate;
export type Filename = z.infer<typeof filename>;
const entryFilename = filenameTemplate;
export type EntryFilename = z.infer<typeof entryFilename>;
const entryRuntime = z.literal(false).or(z.string());
export type EntryRuntime = z.infer<typeof entryRuntime>;
const entryItem = z.string().or(z.array(z.string()));
export type EntryItem = z.infer<typeof entryItem>;
const entryDescription = z.strictObject({
import: entryItem,
runtime: entryRuntime.optional(),
publicPath: publicPath.optional(),
baseUri: baseUri.optional(),
chunkLoading: chunkLoading.optional(),
asyncChunks: asyncChunks.optional(),
wasmLoading: wasmLoading.optional(),
filename: entryFilename.optional(),
library: libraryOptions.optional()
});
export type EntryDescription = z.infer<typeof entryDescription>;
const entryUnnamed = entryItem;
export type EntryUnnamed = z.infer<typeof entryUnnamed>;
const entryObject = z.record(entryItem.or(entryDescription));
export type EntryObject = z.infer<typeof entryObject>;
const entryStatic = entryObject.or(entryUnnamed);
export type EntryStatic = z.infer<typeof entryStatic>;
const entry = entryStatic;
export type Entry = z.infer<typeof entry>;
//#endregion
//#region Output
const path = z.string();
export type Path = z.infer<typeof path>;
const assetModuleFilename = z.string();
export type AssetModuleFilename = z.infer<typeof assetModuleFilename>;
const webassemblyModuleFilename = z.string();
export type WebassemblyModuleFilename = z.infer<
typeof webassemblyModuleFilename
>;
const chunkFilename = filenameTemplate;
export type ChunkFilename = z.infer<typeof chunkFilename>;
const crossOriginLoading = z
.literal(false)
.or(z.enum(["anonymous", "use-credentials"]));
export type CrossOriginLoading = z.infer<typeof crossOriginLoading>;
const cssFilename = filenameTemplate;
export type CssFilename = z.infer<typeof cssFilename>;
const cssChunkFilename = filenameTemplate;
export type CssChunkFilename = z.infer<typeof cssChunkFilename>;
const hotUpdateChunkFilename = filenameTemplate;
export type HotUpdateChunkFilename = z.infer<typeof hotUpdateChunkFilename>;
const hotUpdateMainFilename = filenameTemplate;
export type HotUpdateMainFilename = z.infer<typeof hotUpdateMainFilename>;
const hotUpdateGlobal = z.string();
export type HotUpdateGlobal = z.infer<typeof hotUpdateGlobal>;
const uniqueName = z.string();
export type UniqueName = z.infer<typeof uniqueName>;
const chunkLoadingGlobal = z.string();
export type ChunkLoadingGlobal = z.infer<typeof chunkLoadingGlobal>;
const enabledLibraryTypes = z.array(libraryType);
export type EnabledLibraryTypes = z.infer<typeof enabledLibraryTypes>;
const clean = z.boolean();
export type Clean = z.infer<typeof clean>;
const outputModule = z.boolean();
export type OutputModule = z.infer<typeof outputModule>;
const strictModuleExceptionHandling = z.boolean();
export type StrictModuleExceptionHandling = z.infer<
typeof strictModuleExceptionHandling
>;
const strictModuleErrorHandling = z.boolean();
export type StrictModuleErrorHandling = z.infer<
typeof strictModuleErrorHandling
>;
const globalObject = z.string();
export type GlobalObject = z.infer<typeof globalObject>;
const enabledWasmLoadingTypes = z.array(wasmLoadingType);
export type EnabledWasmLoadingTypes = z.infer<typeof enabledWasmLoadingTypes>;
const importFunctionName = z.string();
export type ImportFunctionName = z.infer<typeof importFunctionName>;
const iife = z.boolean();
export type Iife = z.infer<typeof iife>;
const enabledChunkLoadingTypes = z.array(chunkLoadingType);
export type EnabledChunkLoadingTypes = z.infer<typeof enabledChunkLoadingTypes>;
const chunkFormat = z.literal(false).or(z.string());
export type ChunkFormat = z.infer<typeof chunkFormat>;
const workerPublicPath = z.string();
export type WorkerPublicPath = z.infer<typeof workerPublicPath>;
const trustedTypes = z.strictObject({
policyName: z.string().optional()
});
export type TrustedTypes = z.infer<typeof trustedTypes>;
const hashDigest = z.string();
export type HashDigest = z.infer<typeof hashDigest>;
const hashDigestLength = z.number();
export type HashDigestLength = z.infer<typeof hashDigestLength>;
const hashFunction = z.string();
export type HashFunction = z.infer<typeof hashFunction>;
const hashSalt = z.string();
export type HashSalt = z.infer<typeof hashSalt>;
const sourceMapFilename = z.string();
export type SourceMapFilename = z.infer<typeof sourceMapFilename>;
const output = z.strictObject({
path: path.optional(),
clean: clean.optional(),
publicPath: publicPath.optional(),
filename: filename.optional(),
chunkFilename: chunkFilename.optional(),
crossOriginLoading: crossOriginLoading.optional(),
cssFilename: cssFilename.optional(),
cssChunkFilename: cssChunkFilename.optional(),
hotUpdateMainFilename: hotUpdateMainFilename.optional(),
hotUpdateChunkFilename: hotUpdateChunkFilename.optional(),
hotUpdateGlobal: hotUpdateGlobal.optional(),
assetModuleFilename: assetModuleFilename.optional(),
uniqueName: uniqueName.optional(),
chunkLoadingGlobal: chunkLoadingGlobal.optional(),
enabledLibraryTypes: enabledLibraryTypes.optional(),
library: library.optional(),
libraryExport: libraryExport.optional(),
libraryTarget: libraryType.optional(),
umdNamedDefine: umdNamedDefine.optional(),
amdContainer: amdContainer.optional(),
auxiliaryComment: auxiliaryComment.optional(),
module: outputModule.optional(),
strictModuleExceptionHandling: strictModuleExceptionHandling.optional(),
strictModuleErrorHandling: strictModuleErrorHandling.optional(),
globalObject: globalObject.optional(),
importFunctionName: importFunctionName.optional(),
iife: iife.optional(),
wasmLoading: wasmLoading.optional(),
enabledWasmLoadingTypes: enabledWasmLoadingTypes.optional(),
webassemblyModuleFilename: webassemblyModuleFilename.optional(),
chunkFormat: chunkFormat.optional(),
chunkLoading: chunkLoading.optional(),
enabledChunkLoadingTypes: enabledChunkLoadingTypes.optional(),
trustedTypes: z.literal(true).or(z.string()).or(trustedTypes).optional(),
sourceMapFilename: sourceMapFilename.optional(),
hashDigest: hashDigest.optional(),
hashDigestLength: hashDigestLength.optional(),
hashFunction: hashFunction.optional(),
hashSalt: hashSalt.optional(),
asyncChunks: asyncChunks.optional(),
workerChunkLoading: chunkLoading.optional(),
workerWasmLoading: wasmLoading.optional(),
workerPublicPath: workerPublicPath.optional(),
scriptType: scriptType.optional()
});
export type Output = z.infer<typeof output>;
//#endregion
//#region Resolve
const resolveAlias = z.record(
z
.literal(false)
.or(z.string())
.or(z.array(z.string().or(z.literal(false))))
);
export type ResolveAlias = z.infer<typeof resolveAlias>;
const resolveTsconfig = z.strictObject({
configFile: z.string(),
references: z.array(z.string()).or(z.literal("auto")).optional()
});
export type ResolveTsconfig = z.infer<typeof resolveTsconfig>;
const baseResolveOptions = z.strictObject({
alias: resolveAlias.optional(),
/**
* This is `aliasField: ["browser"]` in webpack, because no one
* uses aliasField other than "browser". ---@bvanjoi
*/
browserField: z.boolean().optional(),
conditionNames: z.array(z.string()).optional(),
extensions: z.array(z.string()).optional(),
fallback: resolveAlias.optional(),
mainFields: z.array(z.string()).optional(),
mainFiles: z.array(z.string()).optional(),
modules: z.array(z.string()).optional(),
preferRelative: z.boolean().optional(),
tsConfigPath: z.string().optional(),
tsConfig: resolveTsconfig.optional(),
fullySpecified: z.boolean().optional(),
exportsFields: z.array(z.string()).optional(),
extensionAlias: z.record(z.string().or(z.array(z.string()))).optional()
});
export type ResolveOptions = z.infer<typeof baseResolveOptions> & {
byDependency?: Record<string, ResolveOptions>;
};
const resolveOptions: z.ZodType<ResolveOptions> = baseResolveOptions.extend({
byDependency: z.lazy(() => z.record(resolveOptions)).optional()
});
const resolve = resolveOptions;
export type Resolve = z.infer<typeof resolve>;
//#endregion
//#region Module
const baseRuleSetCondition = z
.instanceof(RegExp)
.or(z.string())
.or(z.function().args(z.string()).returns(z.boolean()));
export type RuleSetCondition =
| z.infer<typeof baseRuleSetCondition>
| RuleSetConditions
| RuleSetLogicalConditions;
const ruleSetCondition: z.ZodType<RuleSetCondition> = baseRuleSetCondition
.or(z.lazy(() => ruleSetConditions))
.or(z.lazy(() => ruleSetLogicalConditions));
export type RuleSetConditions = RuleSetCondition[];
const ruleSetConditions: z.ZodType<RuleSetConditions> = z.lazy(() =>
z.array(ruleSetCondition)
);
export type RuleSetLogicalConditions = {
and?: RuleSetConditions;
or?: RuleSetConditions;
not?: RuleSetConditions;
};
const ruleSetLogicalConditions: z.ZodType<RuleSetLogicalConditions> =
z.strictObject({
and: ruleSetConditions.optional(),
or: ruleSetConditions.optional(),
not: ruleSetConditions.optional()
});
const ruleSetLoader = z.string();
export type RuleSetLoader = z.infer<typeof ruleSetLoader>;
const ruleSetLoaderOptions = z.string().or(z.record(z.any()));
export type RuleSetLoaderOptions = z.infer<typeof ruleSetLoaderOptions>;
const ruleSetLoaderWithOptions = z.strictObject({
ident: z.string().optional(),
loader: ruleSetLoader,
options: ruleSetLoaderOptions.optional()
});
export type RuleSetLoaderWithOptions = z.infer<typeof ruleSetLoaderWithOptions>;
const ruleSetUseItem = ruleSetLoader.or(ruleSetLoaderWithOptions);
export type RuleSetUseItem = z.infer<typeof ruleSetUseItem>;
const ruleSetUse = ruleSetUseItem
.or(ruleSetUseItem.array())
.or(
z.function().args(z.custom<RawFuncUseCtx>()).returns(ruleSetUseItem.array())
);
export type RuleSetUse = z.infer<typeof ruleSetUse>;
const baseRuleSetRule = z.strictObject({
test: ruleSetCondition.optional(),
exclude: ruleSetCondition.optional(),
include: ruleSetCondition.optional(),
issuer: ruleSetCondition.optional(),
dependency: ruleSetCondition.optional(),
resource: ruleSetCondition.optional(),
resourceFragment: ruleSetCondition.optional(),
resourceQuery: ruleSetCondition.optional(),
scheme: ruleSetCondition.optional(),
mimetype: ruleSetCondition.optional(),
descriptionData: z.record(ruleSetCondition).optional(),
type: z.string().optional(),
loader: ruleSetLoader.optional(),
options: ruleSetLoaderOptions.optional(),
use: ruleSetUse.optional(),
parser: z.record(z.any()).optional(),
generator: z.record(z.any()).optional(),
resolve: resolveOptions.optional(),
sideEffects: z.boolean().optional(),
enforce: z.literal("pre").or(z.literal("post")).optional()
});
export type RuleSetRule = z.infer<typeof baseRuleSetRule> & {
oneOf?: RuleSetRule[];
rules?: RuleSetRule[];
};
const ruleSetRule: z.ZodType<RuleSetRule> = baseRuleSetRule.extend({
oneOf: z.lazy(() => ruleSetRule.array()).optional(),
rules: z.lazy(() => ruleSetRule.array()).optional()
});
const ruleSetRules = z.array(z.literal("...").or(ruleSetRule).or(falsy));
export type RuleSetRules = z.infer<typeof ruleSetRules>;
const assetParserDataUrlOptions = z.strictObject({
maxSize: z.number().optional()
});
export type AssetParserDataUrlOptions = z.infer<
typeof assetParserDataUrlOptions
>;
const assetParserDataUrl = assetParserDataUrlOptions;
export type AssetParserDataUrl = z.infer<typeof assetParserDataUrl>;
const assetParserOptions = z.strictObject({
dataUrlCondition: assetParserDataUrl.optional()
});
export type AssetParserOptions = z.infer<typeof assetParserOptions>;
//TODO: "weak", "lazy-once"
const dynamicImportMode = z.enum(["eager", "lazy"]);
const javascriptParserUrl = z.union([z.literal("relative"), z.boolean()]);
const javascriptParserOptions = z.strictObject({
dynamicImportMode: dynamicImportMode.optional(),
url: javascriptParserUrl.optional()
});
export type JavascriptParserOptions = z.infer<typeof javascriptParserOptions>;
const parserOptionsByModuleTypeKnown = z.strictObject({
asset: assetParserOptions.optional(),
javascript: javascriptParserOptions.optional()
});
export type ParserOptionsByModuleTypeKnown = z.infer<
typeof parserOptionsByModuleTypeKnown
>;
const parserOptionsByModuleTypeUnknown = z.record(z.record(z.any()));
export type ParserOptionsByModuleTypeUnknown = z.infer<
typeof parserOptionsByModuleTypeUnknown
>;
const parserOptionsByModuleType = parserOptionsByModuleTypeKnown.or(
parserOptionsByModuleTypeUnknown
);
export type ParserOptionsByModuleType = z.infer<
typeof parserOptionsByModuleType
>;
const assetGeneratorDataUrlOptions = z.strictObject({
encoding: z.literal(false).or(z.literal("base64")).optional(),
mimetype: z.string().optional()
});
export type AssetGeneratorDataUrlOptions = z.infer<
typeof assetGeneratorDataUrlOptions
>;
const assetGeneratorDataUrl = assetGeneratorDataUrlOptions;
export type AssetGeneratorDataUrl = z.infer<typeof assetGeneratorDataUrl>;
const assetInlineGeneratorOptions = z.strictObject({
dataUrl: assetGeneratorDataUrl.optional()
});
export type AssetInlineGeneratorOptions = z.infer<
typeof assetInlineGeneratorOptions
>;
const assetResourceGeneratorOptions = z.strictObject({
filename: filenameTemplate.optional(),
publicPath: publicPath.optional()
});
export type AssetResourceGeneratorOptions = z.infer<
typeof assetResourceGeneratorOptions
>;
const assetGeneratorOptions = assetInlineGeneratorOptions.merge(
assetResourceGeneratorOptions
);
export type AssetGeneratorOptions = z.infer<typeof assetGeneratorOptions>;
const generatorOptionsByModuleTypeKnown = z.strictObject({
asset: assetGeneratorOptions.optional(),
"asset/inline": assetInlineGeneratorOptions.optional(),
"asset/resource": assetResourceGeneratorOptions.optional()
});
export type GeneratorOptionsByModuleTypeKnown = z.infer<
typeof generatorOptionsByModuleTypeKnown
>;
const generatorOptionsByModuleTypeUnknown = z.record(z.record(z.any()));
export type GeneratorOptionsByModuleTypeUnknown = z.infer<
typeof generatorOptionsByModuleTypeUnknown
>;
const generatorOptionsByModuleType = generatorOptionsByModuleTypeKnown.or(
generatorOptionsByModuleTypeUnknown
);
export type GeneratorOptionsByModuleType = z.infer<
typeof generatorOptionsByModuleType
>;
const moduleOptions = z.strictObject({
defaultRules: ruleSetRules.optional(),
rules: ruleSetRules.optional(),
parser: parserOptionsByModuleType.optional(),
generator: generatorOptionsByModuleType.optional()
});
export type ModuleOptions = z.infer<typeof moduleOptions>;
//#endregion
//#region Target
const allowTarget = z
.enum([
"web",
"webworker",
"es3",
"es5",
"es2015",
"es2016",
"es2017",
"es2018",
"es2019",
"es2020",
"es2021",
"es2022",
"browserslist"
])
.or(z.literal("node"))
.or(z.literal("async-node"))
.or(
z.custom<`node${number}`>(
value => typeof value === "string" && /^node\d+$/.test(value)
)
)
.or(
z.custom<`async-node${number}`>(
value => typeof value === "string" && /^async-node\d+$/.test(value)
)
)
.or(
z.custom<`node${number}.${number}`>(
value => typeof value === "string" && /^node\d+\.\d+$/.test(value)
)
)
.or(
z.custom<`async-node${number}.${number}`>(
value => typeof value === "string" && /^async-node\d+\.\d+$/.test(value)
)
)
.or(z.literal("electron-main"))
.or(
z.custom<`electron${number}-main`>(
value => typeof value === "string" && /^electron\d+-main$/.test(value)
)
)
.or(
z.custom<`electron${number}.${number}-main`>(
value =>
typeof value === "string" && /^electron\d+\.\d+-main$/.test(value)
)
)
.or(z.literal("electron-renderer"))
.or(
z.custom<`electron${number}-renderer`>(
value => typeof value === "string" && /^electron\d+-renderer$/.test(value)
)
)
.or(
z.custom<`electron${number}.${number}-renderer`>(
value =>
typeof value === "string" && /^electron\d+\.\d+-renderer$/.test(value)
)
)
.or(z.literal("electron-preload"))
.or(
z.custom<`electron${number}-preload`>(
value => typeof value === "string" && /^electron\d+-preload$/.test(value)
)
)
.or(
z.custom<`electron${number}.${number}-preload`>(
value =>
typeof value === "string" && /^electron\d+\.\d+-preload$/.test(value)
)
);
const target = z.literal(false).or(allowTarget).or(allowTarget.array());
export type Target = z.infer<typeof target>;
//#endregion
//#region ExternalsType
export const externalsType = z.enum([
"var",
"module",
"assign",
"this",
"window",
"self",
"global",
"commonjs",
"commonjs2",
"commonjs-module",
"commonjs-static",
"amd",
"amd-require",
"umd",
"umd2",
"jsonp",
"system",
"promise",
"import",
"script",
"node-commonjs"
]);
export type ExternalsType = z.infer<typeof externalsType>;
//#endregion
//#region Externals
const externalItemValue = z
.string()
.or(z.boolean())
.or(z.string().array().min(1))
.or(z.record(z.string().or(z.string().array())));
export type ExternalItemValue = z.infer<typeof externalItemValue>;
const externalItemObjectUnknown = z.record(externalItemValue);
export type ExternalItemObjectUnknown = z.infer<
typeof externalItemObjectUnknown
>;
const externalItemFunctionData = z.strictObject({
context: z.string().optional(),
dependencyType: z.string().optional(),
request: z.string().optional()
});
export type ExternalItemFunctionData = z.infer<typeof externalItemFunctionData>;
const externalItem = z
.string()
.or(z.instanceof(RegExp))
.or(externalItemObjectUnknown)
.or(
z
.function()
.args(
externalItemFunctionData,
z
.function()
.args(
z.instanceof(Error).optional(),
externalItemValue.optional(),
externalsType.optional()
)
.returns(z.void())
)
)
.or(
z
.function()
.args(externalItemFunctionData)
.returns(z.promise(externalItemValue))
);
export type ExternalItem = z.infer<typeof externalItem>;
const externals = externalItem.array().or(externalItem);
export type Externals = z.infer<typeof externals>;
//#endregion
//#region ExternalsPresets
const externalsPresets = z.strictObject({
node: z.boolean().optional(),
web: z.boolean().optional(),
webAsync: z.boolean().optional(),
electron: z.boolean().optional(),
electronMain: z.boolean().optional(),
electronPreload: z.boolean().optional(),
electronRenderer: z.boolean().optional()
});
export type ExternalsPresets = z.infer<typeof externalsPresets>;
//#endregion
//#region InfrastructureLogging
const filterItemTypes = z
.instanceof(RegExp)
.or(z.string())
.or(z.function().args(z.string()).returns(z.boolean()));
export type FilterItemTypes = z.infer<typeof filterItemTypes>;
const filterTypes = filterItemTypes.array().or(filterItemTypes);
export type FilterTypes = z.infer<typeof filterTypes>;
const infrastructureLogging = z.strictObject({
appendOnly: z.boolean().optional(),
colors: z.boolean().optional(),
console: z.custom<Console>().optional(),
debug: z.boolean().or(filterTypes).optional(),
level: z.enum(["none", "error", "warn", "info", "log", "verbose"]).optional(),
stream: z.custom<NodeJS.WritableStream>().optional()
});
export type InfrastructureLogging = z.infer<typeof infrastructureLogging>;
//#endregion
//#region DevTool
const devTool = z
.literal(false)
.or(
z.enum([
"cheap-source-map",
"cheap-module-source-map",
"source-map",
"inline-cheap-source-map",
"inline-cheap-module-source-map",
"inline-source-map",
"inline-nosources-cheap-module-source-map",
"inline-nosources-source-map",
"nosources-cheap-source-map",
"nosources-cheap-module-source-map",
"nosources-source-map",
"hidden-nosources-cheap-source-map",
"hidden-nosources-cheap-module-source-map",
"hidden-nosources-source-map",
"hidden-cheap-source-map",
"hidden-cheap-module-source-map",
"hidden-source-map",
"eval-cheap-source-map",
"eval-cheap-module-source-map",
"eval-source-map",
"eval-nosources-cheap-source-map",
"eval-nosources-cheap-module-source-map",
"eval-nosources-source-map"
])
);
export type DevTool = z.infer<typeof devTool>;
//#endregion
//#region Node
const nodeOptions = z.strictObject({
__dirname: z
.boolean()
.or(z.enum(["warn-mock", "mock", "eval-only"]))
.optional(),
__filename: z
.boolean()
.or(z.enum(["warn-mock", "mock", "eval-only"]))
.optional(),
global: z.boolean().or(z.literal("warn")).optional()
});
export type NodeOptions = z.infer<typeof nodeOptions>;
const node = z.literal(false).or(nodeOptions);
export type Node = z.infer<typeof node>;
//#endregion
//#region Snapshot
const snapshotOptions = z.strictObject({
module: z
.strictObject({
hash: z.boolean().optional(),
timestamp: z.boolean().optional()
})
.optional(),
resolve: z
.strictObject({
hash: z.boolean().optional(),
timestamp: z.boolean().optional()
})
.optional()
});
export type SnapshotOptions = z.infer<typeof snapshotOptions>;
//#endregion
//#region Cache
const cacheOptions = z.boolean();
export type CacheOptions = z.infer<typeof cacheOptions>;
//#endregion
//#region Stats
const statsOptions = z.strictObject({
all: z.boolean().optional(),
preset: z
.enum(["normal", "none", "verbose", "errors-only", "errors-warnings"])
.optional(),
assets: z.boolean().optional(),
chunks: z.boolean().optional(),
modules: z.boolean().optional(),
entrypoints: z.boolean().optional(),
chunkGroups: z.boolean().optional(),
warnings: z.boolean().optional(),
warningsCount: z.boolean().optional(),
errors: z.boolean().optional(),
errorsCount: z.boolean().optional(),
colors: z.boolean().optional(),
hash: z.boolean().optional(),
version: z.boolean().optional(),
reasons: z.boolean().optional(),
publicPath: z.boolean().optional(),
outputPath: z.boolean().optional(),
chunkModules: z.boolean().optional(),
chunkRelations: z.boolean().optional(),
ids: z.boolean().optional(),
timings: z.boolean().optional(),
builtAt: z.boolean().optional(),
moduleAssets: z.boolean().optional(),
modulesSpace: z.number().optional(),
nestedModules: z.boolean().optional(),
source: z.boolean().optional(),
logging: z
.enum(["none", "error", "warn", "info", "log", "verbose"])
.or(z.boolean())
.optional(),
loggingDebug: z.boolean().or(filterTypes).optional(),
loggingTrace: z.boolean().optional(),
runtimeModules: z.boolean().optional(),
children: z.boolean().optional()
});
export type StatsOptions = z.infer<typeof statsOptions>;
const statsValue = z
.enum(["none", "errors-only", "errors-warnings", "normal", "verbose"])
.or(z.boolean())
.or(statsOptions);
export type StatsValue = z.infer<typeof statsValue>;
//#endregion
//#region Plugins
export interface RspackPluginInstance {
apply: (compiler: Compiler) => void;
[k: string]: any;
}
export type RspackPluginFunction = (this: Compiler, compiler: Compiler) => void;
const plugin = z.union([
z.custom<RspackPluginInstance>(),
z.custom<RspackPluginFunction>(),
falsy
]);
const plugins = plugin.array();
export type Plugins = z.infer<typeof plugins>;
//#endregion
//#region Optimization
const optimizationRuntimeChunk = z
.enum(["single", "multiple"])
.or(z.boolean())
.or(
z.strictObject({
name: z
.string()
.or(z.function().returns(z.string().or(z.undefined())))
.optional()
})
);
export type OptimizationRuntimeChunk = z.infer<typeof optimizationRuntimeChunk>;
const optimizationSplitChunksNameFunction = z.function().args(
z.instanceof(Module).optional()
// FIXME: z.array(z.instanceof(Chunk)).optional(), z.string()
// FIXME: Chunk[], cacheChunkKey
);
export type OptimizationSplitChunksNameFunction = z.infer<
typeof optimizationSplitChunksNameFunction
>;
const optimizationSplitChunksName = z
.string()
.or(z.literal(false))
.or(optimizationSplitChunksNameFunction);
const optimizationSplitChunksChunks = z
.enum(["initial", "async", "all"])
.or(z.instanceof(RegExp))
.or(z.function().args(z.instanceof(Chunk)).returns(z.boolean()));
const optimizationSplitChunksSizes = z.number();
const sharedOptimizationSplitChunksCacheGroup = {
chunks: optimizationSplitChunksChunks.optional(),
minChunks: z.number().optional(),
name: optimizationSplitChunksName.optional(),
minSize: optimizationSplitChunksSizes.optional(),
maxSize: optimizationSplitChunksSizes.optional(),
maxAsyncSize: optimizationSplitChunksSizes.optional(),
maxInitialSize: optimizationSplitChunksSizes.optional(),
automaticNameDelimiter: z.string().optional()
};
const optimizationSplitChunksCacheGroup = z.strictObject({
test: z
.string()
.or(z.instanceof(RegExp))
.or(
z
.function()
.args(z.instanceof(Module) /** FIXME: lack of CacheGroupContext */)
)
.optional(),
priority: z.number().optional(),
enforce: z.boolean().optional(),
filename: z.string().optional(),
reuseExistingChunk: z.boolean().optional(),
type: z.string().or(z.instanceof(RegExp)).optional(),
idHint: z.string().optional(),
...sharedOptimizationSplitChunksCacheGroup
});
export type OptimizationSplitChunksCacheGroup = z.infer<
typeof optimizationSplitChunksCacheGroup
>;
const optimizationSplitChunksOptions = z.strictObject({
cacheGroups: z
.record(z.literal(false).or(optimizationSplitChunksCacheGroup))
.optional(),
maxAsyncRequests: z.number().optional(),
maxInitialRequests: z.number().optional(),
fallbackCacheGroup: z
.strictObject({
chunks: optimizationSplitChunksChunks.optional(),
minSize: z.number().optional(),
maxSize: z.number().optional(),
maxAsyncSize: z.number().optional(),
maxInitialSize: z.number().optional(),
automaticNameDelimiter: z.string().optional()
})
.optional(),
hidePathInfo: z.boolean().optional(),
...sharedOptimizationSplitChunksCacheGroup
});
export type OptimizationSplitChunksOptions = z.infer<
typeof optimizationSplitChunksOptions
>;