forked from parcel-bundler/parcel
/
index.js
1826 lines (1700 loc) Β· 55.7 KB
/
index.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
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
// @flow strict-local
import type {Readable} from 'stream';
import type SourceMap from '@parcel/source-map';
import type {FileSystem} from '@parcel/fs';
import type WorkerFarm from '@parcel/workers';
import type {PackageManager} from '@parcel/package-manager';
import type {
Diagnostic,
Diagnostifiable,
DiagnosticWithoutOrigin,
} from '@parcel/diagnostic';
import type {Cache} from '@parcel/cache';
import type {AST as _AST, ConfigResult as _ConfigResult} from './unsafe';
/** Plugin-specific AST, <code>any</code> */
export type AST = _AST;
export type ConfigResult = _ConfigResult;
/** Plugin-specific config result, <code>any</code> */
export type ConfigResultWithFilePath<T> = {|
contents: T,
filePath: FilePath,
|};
/** <code>process.env</code> */
export type EnvMap = typeof process.env;
export type JSONValue =
| null
| void // ? Is this okay?
| boolean
| number
| string
| Array<JSONValue>
| JSONObject;
/** A JSON object (as in "map") */
export type JSONObject = {[key: string]: JSONValue, ...};
export type PackageName = string;
export type FilePath = string;
export type Glob = string;
export type Semver = string;
export type SemverRange = string;
/** See Dependency */
export type DependencySpecifier = string;
/** A pipeline as specified in the config mapping to <code>T</code> */
export type GlobMap<T> = {[Glob]: T, ...};
export type RawParcelConfigPipeline = Array<PackageName>;
export type HMROptions = {port?: number, host?: string, ...};
/** The format of .parcelrc */
export type RawParcelConfig = {|
extends?: PackageName | FilePath | Array<PackageName | FilePath>,
resolvers?: RawParcelConfigPipeline,
transformers?: {[Glob]: RawParcelConfigPipeline, ...},
bundler?: PackageName,
namers?: RawParcelConfigPipeline,
runtimes?: RawParcelConfigPipeline,
packagers?: {[Glob]: PackageName, ...},
optimizers?: {[Glob]: RawParcelConfigPipeline, ...},
compressors?: {[Glob]: RawParcelConfigPipeline, ...},
reporters?: RawParcelConfigPipeline,
validators?: {[Glob]: RawParcelConfigPipeline, ...},
|};
/** A .parcelrc where all package names are resolved */
export type ResolvedParcelConfigFile = {|
...RawParcelConfig,
+filePath: FilePath,
+resolveFrom?: FilePath,
|};
/** Corresponds to <code>pkg#engines</code> */
export type Engines = {
+browsers?: string | Array<string>,
+electron?: SemverRange,
+node?: SemverRange,
+parcel?: SemverRange,
...
};
/** Corresponds to <code>pkg#targets.*.sourceMap</code> */
export type TargetSourceMapOptions = {|
+sourceRoot?: string,
+inline?: boolean,
+inlineSources?: boolean,
|};
/**
* A parsed version of PackageTargetDescriptor
*/
export interface Target {
/** The output filename of the entry */
+distEntry: ?FilePath;
/** The output folder */
+distDir: FilePath;
+env: Environment;
+name: string;
+publicUrl: string;
/** The location that created this Target, e.g. `package.json#main`*/
+loc: ?SourceLocation;
}
/** In which environment the output should run (influces e.g. bundle loaders) */
export type EnvironmentContext =
| 'browser'
| 'web-worker'
| 'service-worker'
| 'worklet'
| 'node'
| 'electron-main'
| 'electron-renderer';
/** The JS module format for the bundle output */
export type OutputFormat = 'esmodule' | 'commonjs' | 'global';
/**
* The format of <code>pkg#targets.*</code>
*
* See Environment and Target.
*/
export type PackageTargetDescriptor = {|
+context?: EnvironmentContext,
+engines?: Engines,
+includeNodeModules?:
| boolean
| Array<PackageName>
| {[PackageName]: boolean, ...},
+outputFormat?: OutputFormat,
+publicUrl?: string,
+distDir?: FilePath,
+sourceMap?: boolean | TargetSourceMapOptions,
+isLibrary?: boolean,
+optimize?: boolean,
+scopeHoist?: boolean,
+source?: FilePath | Array<FilePath>,
|};
/**
* The target format when using the JS API.
*
* (Same as PackageTargetDescriptor, but <code>distDir</code> is required.)
*/
export type TargetDescriptor = {|
...PackageTargetDescriptor,
+distDir: FilePath,
+distEntry?: FilePath,
|};
export type SourceType = 'script' | 'module';
/**
* This is used when creating an Environment (see that).
*/
export type EnvironmentOptions = {|
+context?: EnvironmentContext,
+engines?: Engines,
+includeNodeModules?:
| boolean
| Array<PackageName>
| {[PackageName]: boolean, ...},
+outputFormat?: OutputFormat,
+sourceType?: SourceType,
+isLibrary?: boolean,
+shouldOptimize?: boolean,
+shouldScopeHoist?: boolean,
+sourceMap?: ?TargetSourceMapOptions,
+loc?: ?SourceLocation,
|};
/**
* A resolved browserslist, e.g.:
* <pre><code>
* {
* edge: '76',
* firefox: '67',
* chrome: '63',
* safari: '11.1',
* opera: '50',
* }
* </code></pre>
*/
export type VersionMap = {
[string]: string,
...,
};
export type EnvironmentFeature =
| 'esmodules'
| 'dynamic-import'
| 'worker-module'
| 'service-worker-module'
| 'import-meta-url'
| 'arrow-functions';
/**
* Defines the environment in for the output bundle
*/
export interface Environment {
+id: string;
+context: EnvironmentContext;
+engines: Engines;
/** Whether to include all/none packages \
* (<code>true / false</code>), an array of package names to include, or an object \
* (of a package is not specified, it's included).
*/
+includeNodeModules:
| boolean
| Array<PackageName>
| {[PackageName]: boolean, ...};
+outputFormat: OutputFormat;
+sourceType: SourceType;
/** Whether this is a library build (e.g. less loaders) */
+isLibrary: boolean;
/** Whether the output should be minified. */
+shouldOptimize: boolean;
/** Whether scope hoisting is enabled. */
+shouldScopeHoist: boolean;
+sourceMap: ?TargetSourceMapOptions;
+loc: ?SourceLocation;
/** Whether <code>context</code> specifies a browser context. */
isBrowser(): boolean;
/** Whether <code>context</code> specifies a node context. */
isNode(): boolean;
/** Whether <code>context</code> specifies an electron context. */
isElectron(): boolean;
/** Whether <code>context</code> specifies a worker context. */
isWorker(): boolean;
/** Whether <code>context</code> specifies a worklet context. */
isWorklet(): boolean;
/** Whether <code>context</code> specifies an isolated context (can't access other loaded ancestor bundles). */
isIsolated(): boolean;
matchesEngines(minVersions: VersionMap, defaultValue?: boolean): boolean;
supports(feature: EnvironmentFeature, defaultValue?: boolean): boolean;
}
/**
* Format of <code>pkg#dependencies</code>, <code>pkg#devDependencies</code>, <code>pkg#peerDependencies</code>
*/
type PackageDependencies = {|
[PackageName]: Semver,
|};
/**
* Format of <code>package.json</code>
*/
export type PackageJSON = {
name: PackageName,
version: Semver,
type?: 'module',
main?: FilePath,
module?: FilePath,
types?: FilePath,
browser?: FilePath | {[FilePath]: FilePath | boolean, ...},
source?: FilePath | Array<FilePath>,
alias?: {[PackageName | FilePath | Glob]: PackageName | FilePath, ...},
browserslist?: Array<string> | {[string]: Array<string>},
engines?: Engines,
targets?: {[string]: PackageTargetDescriptor, ...},
dependencies?: PackageDependencies,
devDependencies?: PackageDependencies,
peerDependencies?: PackageDependencies,
sideEffects?: boolean | FilePath | Array<FilePath>,
bin?: string | {|[string]: FilePath|},
...
};
export type LogLevel = 'none' | 'error' | 'warn' | 'info' | 'verbose';
export type BuildMode = 'development' | 'production' | string;
export type DetailedReportOptions = {|
assetsPerBundle?: number,
|};
export type InitialParcelOptions = {|
+entries?: FilePath | Array<FilePath>,
+config?: DependencySpecifier,
+defaultConfig?: DependencySpecifier,
+env?: EnvMap,
+targets?: ?(Array<string> | {+[string]: TargetDescriptor, ...}),
+shouldDisableCache?: boolean,
+cacheDir?: FilePath,
+mode?: BuildMode,
+hmrOptions?: ?HMROptions,
+shouldContentHash?: boolean,
+serveOptions?: InitialServerOptions | false,
+shouldAutoInstall?: boolean,
+logLevel?: LogLevel,
+shouldProfile?: boolean,
+shouldPatchConsole?: boolean,
+shouldBuildLazily?: boolean,
+inputFS?: FileSystem,
+outputFS?: FileSystem,
+cache?: Cache,
+workerFarm?: WorkerFarm,
+packageManager?: PackageManager,
+detailedReport?: ?DetailedReportOptions,
+defaultTargetOptions?: {|
+shouldOptimize?: boolean,
+shouldScopeHoist?: boolean,
+sourceMaps?: boolean,
+publicUrl?: string,
+distDir?: FilePath,
+engines?: Engines,
+outputFormat?: OutputFormat,
+isLibrary?: boolean,
|},
+additionalReporters?: Array<{|
packageName: DependencySpecifier,
resolveFrom: FilePath,
|}>,
// throwErrors
// global?
|};
export type InitialServerOptions = {|
+publicUrl?: string,
+host?: string,
+port: number,
+https?: HTTPSOptions | boolean,
|};
export interface PluginOptions {
+mode: BuildMode;
+env: EnvMap;
+hmrOptions: ?HMROptions;
+serveOptions: ServerOptions | false;
+shouldBuildLazily: boolean;
+shouldAutoInstall: boolean;
+logLevel: LogLevel;
+projectRoot: FilePath;
+cacheDir: FilePath;
+inputFS: FileSystem;
+outputFS: FileSystem;
+packageManager: PackageManager;
+instanceId: string;
+detailedReport: ?DetailedReportOptions;
}
export type ServerOptions = {|
+distDir: FilePath,
+host?: string,
+port: number,
+https?: HTTPSOptions | boolean,
+publicUrl?: string,
|};
export type HTTPSOptions = {|
+cert: FilePath,
+key: FilePath,
|};
/**
* Source locations are 1-based, meaning lines and columns start at 1
*/
export type SourceLocation = {|
+filePath: string,
/** inclusive */
+start: {|
+line: number,
+column: number,
|},
/** exclusive */
+end: {|
+line: number,
+column: number,
|},
|};
/**
* An object that plugins can write arbitatry data to.
*/
export type Meta = JSONObject;
/**
* An identifier in an asset (likely imported/exported).
*/
export type Symbol = string;
/**
* A map of export names to the corresponding asset's local variable names.
*/
export interface AssetSymbols // eslint-disable-next-line no-undef
extends Iterable<
[Symbol, {|local: Symbol, loc: ?SourceLocation, meta?: ?Meta|}],
> {
/**
* The exports of the asset are unknown, rather than just empty.
* This is the default state.
*/
+isCleared: boolean;
get(
exportSymbol: Symbol,
): ?{|
local: Symbol,
loc: ?SourceLocation,
meta?: ?Meta,
|};
hasExportSymbol(exportSymbol: Symbol): boolean;
hasLocalSymbol(local: Symbol): boolean;
exportSymbols(): Iterable<Symbol>;
}
export interface MutableAssetSymbols extends AssetSymbols {
/**
* Initilizes the map, sets isCleared to false.
*/
ensure(): void;
set(
exportSymbol: Symbol,
local: Symbol,
loc: ?SourceLocation,
meta?: ?Meta,
): void;
delete(exportSymbol: Symbol): void;
}
/**
* isWeak means: the symbol is not used by the parent asset itself and is merely reexported
*/
export interface MutableDependencySymbols // eslint-disable-next-line no-undef
extends Iterable<
[
Symbol,
{|local: Symbol, loc: ?SourceLocation, isWeak: boolean, meta?: ?Meta|},
],
> {
/**
* Initilizes the map, sets isCleared to false.
*/
ensure(): void;
/**
* The symbols taht are imports are unknown, rather than just empty.
* This is the default state.
*/
+isCleared: boolean;
get(
exportSymbol: Symbol,
): ?{|
local: Symbol,
loc: ?SourceLocation,
isWeak: boolean,
meta?: ?Meta,
|};
hasExportSymbol(exportSymbol: Symbol): boolean;
hasLocalSymbol(local: Symbol): boolean;
exportSymbols(): Iterable<Symbol>;
set(
exportSymbol: Symbol,
local: Symbol,
loc: ?SourceLocation,
isWeak: ?boolean,
): void;
delete(exportSymbol: Symbol): void;
}
export type DependencyPriority = 'sync' | 'parallel' | 'lazy';
export type SpecifierType = 'commonjs' | 'esm' | 'url' | 'custom';
/**
* Usen when creating a Dependency, see that.
* @section transformer
*/
export type DependencyOptions = {|
/** The specifier used to resolve the dependency. */
+specifier: DependencySpecifier,
/**
* How the specifier should be interpreted.
* - esm: An ES module specifier. It is parsed as a URL, but bare specifiers are treated as node_modules.
* - commonjs: A CommonJS specifier. It is not parsed as a URL.
* - url: A URL that works as in a browser. Bare specifiers are treated as relative URLs.
* - custom: A custom specifier. Must be handled by a custom resolver plugin.
*/
+specifierType: SpecifierType,
/**
* When the dependency should be loaded.
* - sync: The dependency should be resolvable synchronously. The resolved asset will be placed
* in the same bundle as the parent, or another bundle that's already on the page.
* - parallel: The dependency should be placed in a separate bundle that's loaded in parallel
* with the current bundle.
* - lazy: The dependency should be placed in a separate bundle that's loaded later.
* @default 'sync'
*/
+priority?: DependencyPriority,
/**
* Controls the behavior of the bundle the resolved asset is placed into. Use in combination with `priority`
* to determine when the bundle is loaded.
* - inline: The resolved asset will be placed into a new inline bundle. Inline bundles are not written
* to a separate file, but embedded into the parent bundle.
* - isolated: The resolved asset will be isolated from its parents in a separate bundle.
* Shared assets will be duplicated.
*/
+bundleBehavior?: BundleBehavior,
/**
* When the dependency is a bundle entry (priority is "parallel" or "lazy"), this controls the naming
* of that bundle. `needsStableName` indicates that the name should be stable over time, even when the
* content of the bundle changes. This is useful for entries that a user would manually enter the URL
* for, as well as for things like service workers or RSS feeds, where the URL must remain consistent
* over time.
*/
+needsStableName?: boolean,
/** Whether the dependency is optional. If the dependency cannot be resolved, this will not fail the build. */
+isOptional?: boolean,
/** The location within the source file where the dependency was found. */
+loc?: SourceLocation,
/** The environment of the dependency. */
+env?: EnvironmentOptions,
/** Plugin-specific metadata for the dependency. */
+meta?: Meta,
/** The pipeline defined in .parcelrc that the dependency should be processed with. */
+pipeline?: string,
/**
* The file path where the dependency should be resolved from.
* By default, this is the path of the source file where the dependency was specified.
*/
+resolveFrom?: FilePath,
/** The symbols within the resolved module that the source file depends on. */
+symbols?: $ReadOnlyMap<
Symbol,
{|local: Symbol, loc: ?SourceLocation, isWeak: boolean, meta?: Meta|},
>,
|};
/**
* A Dependency denotes a connection between two assets \
* (likely some effect from the importee is expected - be it a side effect or a value is being imported).
*
* @section transformer
*/
export interface Dependency {
/** The id of the dependency. */
+id: string;
/** The specifier used to resolve the dependency. */
+specifier: DependencySpecifier;
/**
* How the specifier should be interpreted.
* - esm: An ES module specifier. It is parsed as a URL, but bare specifiers are treated as node_modules.
* - commonjs: A CommonJS specifier. It is not parsed as a URL.
* - url: A URL that works as in a browser. Bare specifiers are treated as relative URLs.
* - custom: A custom specifier. Must be handled by a custom resolver plugin.
*/
+specifierType: SpecifierType;
/**
* When the dependency should be loaded.
* - sync: The dependency should be resolvable synchronously. The resolved asset will be placed
* in the same bundle as the parent, or another bundle that's already on the page.
* - parallel: The dependency should be placed in a separate bundle that's loaded in parallel
* with the current bundle.
* - lazy: The dependency should be placed in a separate bundle that's loaded later.
* @default 'sync'
*/
+priority: DependencyPriority;
/**
* Controls the behavior of the bundle the resolved asset is placed into. Use in combination with `priority`
* to determine when the bundle is loaded.
* - inline: The resolved asset will be placed into a new inline bundle. Inline bundles are not written
* to a separate file, but embedded into the parent bundle.
* - isolated: The resolved asset will be isolated from its parents in a separate bundle.
* Shared assets will be duplicated.
*/
+bundleBehavior: ?BundleBehavior;
/**
* When the dependency is a bundle entry (priority is "parallel" or "lazy"), this controls the naming
* of that bundle. `needsStableName` indicates that the name should be stable over time, even when the
* content of the bundle changes. This is useful for entries that a user would manually enter the URL
* for, as well as for things like service workers or RSS feeds, where the URL must remain consistent
* over time.
*/
+needsStableName: boolean;
/** Whether the dependency is optional. If the dependency cannot be resolved, this will not fail the build. */
+isOptional: boolean;
/** Whether the dependency is an entry. */
+isEntry: boolean;
/** The location within the source file where the dependency was found. */
+loc: ?SourceLocation;
/** The environment of the dependency. */
+env: Environment;
/** Plugin-specific metadata for the dependency. */
+meta: Meta;
/** If this is an entry, this is the target that is associated with that entry. */
+target: ?Target;
/** The id of the asset with this dependency. */
+sourceAssetId: ?string;
/** The file path of the asset with this dependency. */
+sourcePath: ?FilePath;
/** The type of the asset that referenced this dependency. */
+sourceAssetType: ?string;
/**
* The file path where the dependency should be resolved from.
* By default, this is the path of the source file where the dependency was specified.
*/
+resolveFrom: ?FilePath;
/** The pipeline defined in .parcelrc that the dependency should be processed with. */
+pipeline: ?string;
// TODO make immutable
/** The symbols within the resolved module that the source file depends on. */
+symbols: MutableDependencySymbols;
}
export type File = {|
+filePath: FilePath,
+hash?: string,
|};
/**
* @section transformer
*/
export type ASTGenerator = {|
type: string,
version: Semver,
|};
export type BundleBehavior = 'inline' | 'isolated';
/**
* An asset represents a file or part of a file. It may represent any data type, including source code,
* binary data, etc. Assets may exist in the file system or may be virtual.
*
* @section transformer
*/
export interface BaseAsset {
/** The id of the asset. */
+id: string;
/** The file system where the source is located. */
+fs: FileSystem;
/** The file path of the asset. */
+filePath: FilePath;
/**
* The asset's type. This initially corresponds to the source file extension,
* but it may be changed during transformation.
*/
+type: string;
/** The transformer options for the asset from the dependency query string. */
+query: URLSearchParams;
/** The environment of the asset. */
+env: Environment;
/**
* Whether this asset is part of the project, and not an external dependency (e.g. in node_modules).
* This indicates that transformation using the project's configuration should be applied.
*/
+isSource: boolean;
/** Plugin-specific metadata for the asset. */
+meta: Meta;
/**
* Controls which bundle the asset is placed into.
* - inline: The asset will be placed into a new inline bundle. Inline bundles are not written
* to a separate file, but embedded into the parent bundle.
* - isolated: The asset will be isolated from its parents in a separate bundle. Shared assets
* will be duplicated.
*/
+bundleBehavior: ?BundleBehavior;
/**
* If the asset is used as a bundle entry, this controls whether that bundle can be split
* into multiple, or whether all of the dependencies must be placed in a single bundle.
*/
+isBundleSplittable: boolean;
/**
* Whether this asset can be omitted if none of its exports are being used.
* This is initially set by the resolver, but can be overridden by transformers.
*/
+sideEffects: boolean;
/**
* When a transformer returns multiple assets, it can give them unique keys to identify them.
* This can be used to find assets during packaging, or to create dependencies between multiple
* assets returned by a transformer by using the unique key as the dependency specifier.
*/
+uniqueKey: ?string;
/** The type of the AST. */
+astGenerator: ?ASTGenerator;
/** The pipeline defined in .parcelrc that the asset should be processed with. */
+pipeline: ?string;
/** The symbols that the asset exports. */
+symbols: AssetSymbols;
/** Returns the current AST. */
getAST(): Promise<?AST>;
/** Returns the asset contents as a string. */
getCode(): Promise<string>;
/** Returns the asset contents as a buffer. */
getBuffer(): Promise<Buffer>;
/** Returns the asset contents as a stream. */
getStream(): Readable;
/** Returns the source map for the asset, if available. */
getMap(): Promise<?SourceMap>;
/** Returns a buffer representation of the source map, if available. */
getMapBuffer(): Promise<?Buffer>;
/** Returns a list of dependencies for the asset. */
getDependencies(): $ReadOnlyArray<Dependency>;
}
/**
* A mutable Asset, available during transformation.
* @section transformer
*/
export interface MutableAsset extends BaseAsset {
/**
* The asset's type. This initially corresponds to the source file extension,
* but it may be changed during transformation.
*/
type: string;
/**
* Controls which bundle the asset is placed into.
* - inline: The asset will be placed into a new inline bundle. Inline bundles are not written
* to a separate file, but embedded into the parent bundle.
* - isolated: The asset will be isolated from its parents in a separate bundle. Shared assets
* will be duplicated.
*/
bundleBehavior: ?BundleBehavior;
/**
* If the asset is used as a bundle entry, this controls whether that bundle can be split
* into multiple, or whether all of the dependencies must be placed in a single bundle.
* @default true
*/
isBundleSplittable: boolean;
/**
* Whether this asset can be omitted if none of its exports are being used.
* This is initially set by the resolver, but can be overridden by transformers.
*/
sideEffects: boolean;
/** The symbols that the asset exports. */
+symbols: MutableAssetSymbols;
/** Adds a dependency to the asset. */
addDependency(DependencyOptions): string;
/**
* Adds a url dependency to the asset.
* This is a shortcut for addDependency that sets the specifierType to 'url' and priority to 'lazy'.
*/
addURLDependency(url: string, opts: $Shape<DependencyOptions>): string;
/** Invalidates the transformation when the given file is modified or deleted. */
invalidateOnFileChange(FilePath): void;
/** Invalidates the transformation when matched files are created. */
invalidateOnFileCreate(FileCreateInvalidation): void;
/** Invalidates the transformation when the given environment variable changes. */
invalidateOnEnvChange(string): void;
/** Sets the asset contents as a string. */
setCode(string): void;
/** Sets the asset contents as a buffer. */
setBuffer(Buffer): void;
/** Sets the asset contents as a stream. */
setStream(Readable): void;
/** Sets the asset's AST. */
setAST(AST): void;
/** Returns whether the AST has been modified. */
isASTDirty(): boolean;
/** Sets the asset's source map. */
setMap(?SourceMap): void;
setEnvironment(opts: EnvironmentOptions): void;
}
/**
* An immutable Asset, available after transformation.
* @section transformer
*/
export interface Asset extends BaseAsset {
/** Statistics about the asset. */
+stats: Stats;
}
export type DevDepOptions = {|
specifier: DependencySpecifier,
resolveFrom: FilePath,
range?: ?SemverRange,
/**
* When this dev dependency is invalidated, also invalidate these dependencies.
* This is useful if the parcel plugin or another parent dependency
* has its own cache for this dev dependency other than Node's require cache.
*/
additionalInvalidations?: Array<{|
specifier: DependencySpecifier,
resolveFrom: FilePath,
range?: ?SemverRange,
|}>,
|};
/**
* @section transformer
*/
export interface Config {
/**
* Whether this config is part of the project, and not an external dependency (e.g. in node_modules).
* This indicates that transformation using the project's configuration should be applied.
*/
+isSource: boolean;
/** The path of the file to start searching for config from. */
+searchPath: FilePath;
/** The environment */
+env: Environment;
/** Invalidates the config when the given file is modified or deleted. */
invalidateOnFileChange(FilePath): void;
/** Invalidates the config when matched files are created. */
invalidateOnFileCreate(FileCreateInvalidation): void;
/** Invalidates the config when the given environment variable changes. */
invalidateOnEnvChange(string): void;
/** Invalidates the config when Parcel restarts. */
invalidateOnStartup(): void;
/**
* Adds a dev dependency to the config. If the dev dependency or any of its
* dependencies change, the config will be invalidated.
*/
addDevDependency(DevDepOptions): void;
/**
* Sets the cache key for the config. By default, this is computed as a hash of the
* files passed to invalidateOnFileChange or loaded by getConfig. If none, then a
* hash of the result returned from loadConfig is used. This method can be used to
* override this behavior and explicitly control the cache key. This can be useful
* in cases where only part of a file is used to avoid unnecessary invalidations,
* or when the result is not hashable (i.e. contains non-serializable properties like functions).
*/
setCacheKey(string): void;
/**
* Searches for config files with the given names in all parent directories
* of the config's searchPath.
*/
getConfig<T>(
filePaths: Array<FilePath>,
options: ?{|
packageKey?: string,
parse?: boolean,
exclude?: boolean,
|},
): Promise<?ConfigResultWithFilePath<T>>;
/**
* Searches for config files with the given names in all parent directories
* of the passed searchPath.
*/
getConfigFrom<T>(
searchPath: FilePath,
filePaths: Array<FilePath>,
options: ?{|
packageKey?: string,
parse?: boolean,
exclude?: boolean,
|},
): Promise<?ConfigResultWithFilePath<T>>;
/** Finds the nearest package.json from the config's searchPath. */
getPackage(): Promise<?PackageJSON>;
}
export type Stats = {|
time: number,
size: number,
|};
/**
* @section transformer
*/
export type GenerateOutput = {|
+content: Blob,
+map?: ?SourceMap,
|};
export type Blob = string | Buffer | Readable;
/**
* Transformers can return multiple result objects to create new assets.
* For example, a file may contain multiple parts of different types,
* which should be processed by their respective transformation pipelines.
*
* @section transformer
*/
export type TransformerResult = {|
/** The asset's type. */
+type: string,
/** The content of the asset. Either content or an AST is required. */
+content?: ?Blob,
/** The asset's AST. Either content or an AST is required. */
+ast?: ?AST,
/** The source map for the asset. */
+map?: ?SourceMap,
/** The dependencies of the asset. */
+dependencies?: $ReadOnlyArray<DependencyOptions>,
/** The environment of the asset. The options are merged with the input asset's environment. */
+env?: EnvironmentOptions | Environment,
/**
* Controls which bundle the asset is placed into.
* - inline: The asset will be placed into a new inline bundle. Inline bundles are not written
* to a separate file, but embedded into the parent bundle.
* - isolated: The asset will be isolated from its parents in a separate bundle. Shared assets
* will be duplicated.
*/
+bundleBehavior?: ?BundleBehavior,
/**
* If the asset is used as a bundle entry, this controls whether that bundle can be split
* into multiple, or whether all of the dependencies must be placed in a single bundle.
*/
+isBundleSplittable?: boolean,
/** Plugin-specific metadata for the asset. */
+meta?: Meta,
/** The pipeline defined in .parcelrc that the asset should be processed with. */
+pipeline?: ?string,
/**
* Whether this asset can be omitted if none of its exports are being used.
* This is initially set by the resolver, but can be overridden by transformers.
*/
+sideEffects?: boolean,
/** The symbols that the asset exports. */
+symbols?: $ReadOnlyMap<Symbol, {|local: Symbol, loc: ?SourceLocation|}>,
/**
* When a transformer returns multiple assets, it can give them unique keys to identify them.
* This can be used to find assets during packaging, or to create dependencies between multiple
* assets returned by a transformer by using the unique key as the dependency specifier.
*/
+uniqueKey?: ?string,
|};
export type Async<T> = T | Promise<T>;
export interface PluginLogger {
/** Logs a diagnostic at the verbose log level. */
verbose(
diagnostic: DiagnosticWithoutOrigin | Array<DiagnosticWithoutOrigin>,
): void;
/** Logs a diagnostic at the info log level. */
info(
diagnostic: DiagnosticWithoutOrigin | Array<DiagnosticWithoutOrigin>,
): void;
/** Synonym for logger.info. */
log(
diagnostic: DiagnosticWithoutOrigin | Array<DiagnosticWithoutOrigin>,
): void;
/** Logs a diagnostic at the verbose warning log level. */
warn(
diagnostic: DiagnosticWithoutOrigin | Array<DiagnosticWithoutOrigin>,
): void;
/** Logs a diagnostic at the verbose error log level. */
error(
input:
| Diagnostifiable
| DiagnosticWithoutOrigin
| Array<DiagnosticWithoutOrigin>,
): void;
}
/**
* @section transformer
*/
export type ResolveFn = (from: FilePath, to: string) => Promise<FilePath>;
/**
* @section validator
* @experimental
*/
type ResolveConfigFn = (configNames: Array<FilePath>) => Promise<?FilePath>;
/**
* @section validator
* @experimental
*/
type ResolveConfigWithPathFn = (
configNames: Array<FilePath>,
assetFilePath: string,
) => Promise<?FilePath>;
/**
* @section validator
* @experimental
*/
export type ValidateResult = {|
warnings: Array<Diagnostic>,
errors: Array<Diagnostic>,
|};
/**
* @section validator
* @experimental
*/
export type DedicatedThreadValidator = {|
validateAll: ({|
assets: Asset[],
resolveConfigWithPath: ResolveConfigWithPathFn,
options: PluginOptions,
logger: PluginLogger,
|}) => Async<Array<?ValidateResult>>,
|};
/**
* @section validator
* @experimental
*/
export type MultiThreadValidator = {|
validate: ({|
asset: Asset,
config: ConfigResult | void,
options: PluginOptions,