1 |
|
2 |
|
3 | import type {FilePath, Glob, PackageName, ConfigResult} from '@parcel/types';
|
4 |
|
5 | import type {Config, Environment} from './types';
|
6 |
|
7 | type ConfigOpts = {|
|
8 | isSource: boolean,
|
9 | searchPath: FilePath,
|
10 | env: Environment,
|
11 | resolvedPath?: FilePath,
|
12 | result?: ConfigResult,
|
13 | includedFiles?: Set<FilePath>,
|
14 | watchGlob?: Glob,
|
15 | devDeps?: Map<PackageName, ?string>,
|
16 | shouldRehydrate?: boolean,
|
17 | shouldReload?: boolean,
|
18 | shouldInvalidateOnStartup?: boolean,
|
19 | |};
|
20 |
|
21 | export function createConfig({
|
22 | isSource,
|
23 | searchPath,
|
24 | env,
|
25 | resolvedPath,
|
26 | result,
|
27 | includedFiles,
|
28 | watchGlob,
|
29 | devDeps,
|
30 | shouldRehydrate,
|
31 | shouldReload,
|
32 | shouldInvalidateOnStartup,
|
33 | }: ConfigOpts): Config {
|
34 | return {
|
35 | isSource,
|
36 | searchPath,
|
37 | env,
|
38 | resolvedPath,
|
39 | result: result ?? null,
|
40 | resultHash: null,
|
41 | includedFiles: includedFiles ?? new Set(),
|
42 | pkg: null,
|
43 | watchGlob,
|
44 | devDeps: devDeps ?? new Map(),
|
45 | shouldRehydrate: shouldRehydrate ?? false,
|
46 | shouldReload: shouldReload ?? false,
|
47 | shouldInvalidateOnStartup: shouldInvalidateOnStartup ?? false,
|
48 | };
|
49 | }
|
50 |
|
51 | export function addDevDependency(
|
52 | config: Config,
|
53 | name: PackageName,
|
54 | version?: string,
|
55 | ) {
|
56 | config.devDeps.set(name, version);
|
57 | }
|
58 |
|
59 |
|
60 | export function getInvalidations(config: Config) {
|
61 | let invalidations = [];
|
62 |
|
63 | if (config.watchGlob != null) {
|
64 | invalidations.push({
|
65 | action: 'add',
|
66 | pattern: config.watchGlob,
|
67 | });
|
68 | }
|
69 |
|
70 | for (let filePath of [config.resolvedPath, ...config.includedFiles]) {
|
71 | invalidations.push({
|
72 | action: 'change',
|
73 | pattern: filePath,
|
74 | });
|
75 |
|
76 | invalidations.push({
|
77 | action: 'unlink',
|
78 | pattern: filePath,
|
79 | });
|
80 | }
|
81 |
|
82 | return invalidations;
|
83 | }
|