/
Dependency.js
100 lines (95 loc) 路 2.65 KB
/
Dependency.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
// @flow
import type {
FilePath,
Meta,
DependencySpecifier,
SourceLocation,
Symbol,
BundleBehavior as IBundleBehavior,
} from '@parcel/types';
import type {Dependency, Environment, Target} from './types';
import {hashString} from '@parcel/hash';
import {SpecifierType, Priority, BundleBehavior} from './types';
import {toInternalSourceLocation} from './utils';
import {toProjectPath} from './projectPath';
type DependencyOpts = {|
id?: string,
sourcePath?: FilePath,
sourceAssetId?: string,
specifier: DependencySpecifier,
specifierType: $Keys<typeof SpecifierType>,
priority?: $Keys<typeof Priority>,
needsStableName?: boolean,
bundleBehavior?: ?IBundleBehavior,
isEntry?: boolean,
isOptional?: boolean,
loc?: SourceLocation,
env: Environment,
meta?: Meta,
resolveFrom?: FilePath,
target?: Target,
symbols?: Map<
Symbol,
{|local: Symbol, loc: ?SourceLocation, isWeak: boolean, meta?: ?Meta|},
>,
pipeline?: ?string,
|};
export function createDependency(
projectRoot: FilePath,
opts: DependencyOpts,
): Dependency {
let id =
opts.id ||
hashString(
(opts.sourceAssetId ?? '') +
opts.specifier +
opts.env.id +
(opts.target ? JSON.stringify(opts.target) : '') +
(opts.pipeline ?? '') +
opts.specifierType +
(opts.bundleBehavior ?? '') +
(opts.priority ?? 'sync'),
);
return {
...opts,
resolveFrom: toProjectPath(projectRoot, opts.resolveFrom),
sourcePath: toProjectPath(projectRoot, opts.sourcePath),
id,
loc: toInternalSourceLocation(projectRoot, opts.loc),
specifierType: SpecifierType[opts.specifierType],
priority: Priority[opts.priority ?? 'sync'],
needsStableName: opts.needsStableName ?? false,
bundleBehavior: opts.bundleBehavior
? BundleBehavior[opts.bundleBehavior]
: null,
isEntry: opts.isEntry ?? false,
isOptional: opts.isOptional ?? false,
meta: opts.meta || {},
symbols:
opts.symbols &&
new Map(
[...opts.symbols].map(([k, v]) => [
k,
{
local: v.local,
meta: v.meta,
isWeak: v.isWeak,
loc: toInternalSourceLocation(projectRoot, v.loc),
},
]),
),
};
}
export function mergeDependencies(a: Dependency, b: Dependency): void {
let {meta, symbols, needsStableName, isEntry, isOptional, ...other} = b;
Object.assign(a, other);
Object.assign(a.meta, meta);
if (a.symbols && symbols) {
for (let [k, v] of symbols) {
a.symbols.set(k, v);
}
}
if (needsStableName) a.needsStableName = true;
if (isEntry) a.isEntry = true;
if (!isOptional) a.isOptional = false;
}