/
utils.js
236 lines (208 loc) Β· 5.64 KB
/
utils.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
// @flow strict-local
import type {AbortSignal} from 'abortcontroller-polyfill/dist/cjs-ponyfill';
import type {
FilePath,
FileCreateInvalidation,
SourceLocation,
} from '@parcel/types';
import type {
BundleGroup,
ParcelOptions,
InternalFileCreateInvalidation,
InternalSourceLocation,
InternalDevDepOptions,
} from './types';
import type {PackageManager} from '@parcel/package-manager';
import invariant from 'assert';
import baseX from 'base-x';
import {Graph} from '@parcel/graph';
import {hashObject} from '@parcel/utils';
import {registerSerializableClass} from './serializer';
import AssetGraph from './AssetGraph';
import BundleGraph from './BundleGraph';
import ParcelConfig from './ParcelConfig';
import {RequestGraph} from './RequestTracker';
import Config from './public/Config';
import {fromProjectPath, toProjectPath} from './projectPath';
// flowlint-next-line untyped-import:off
import packageJson from '../package.json';
const base62 = baseX(
'0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
);
export function getBundleGroupId(bundleGroup: BundleGroup): string {
return 'bundle_group:' + bundleGroup.entryAssetId;
}
export function assertSignalNotAborted(signal: ?AbortSignal): void {
if (signal && signal.aborted) {
throw new BuildAbortError();
}
}
export class BuildAbortError extends Error {
name: string = 'BuildAbortError';
}
let coreRegistered;
export function registerCoreWithSerializer() {
if (coreRegistered) {
return;
}
const packageVersion: mixed = packageJson.version;
if (typeof packageVersion !== 'string') {
throw new Error('Expected package version to be a string');
}
// $FlowFixMe[incompatible-cast]
for (let [name, ctor] of (Object.entries({
AssetGraph,
Config,
BundleGraph,
Graph,
ParcelConfig,
RequestGraph,
// $FlowFixMe[unclear-type]
}): Array<[string, Class<any>]>)) {
registerSerializableClass(packageVersion + ':' + name, ctor);
}
coreRegistered = true;
}
export function getPublicId(
id: string,
alreadyExists: string => boolean,
): string {
let encoded = base62.encode(Buffer.from(id, 'hex'));
for (let end = 5; end <= encoded.length; end++) {
let candidate = encoded.slice(0, end);
if (!alreadyExists(candidate)) {
return candidate;
}
}
throw new Error('Original id was not unique');
}
// These options don't affect compilation and should cause invalidations
const ignoreOptions = new Set([
'env', // handled by separate invalidateOnEnvChange
'inputFS',
'outputFS',
'workerFarm',
'packageManager',
'detailedReport',
'shouldDisableCache',
'cacheDir',
'shouldAutoInstall',
'logLevel',
'shouldProfile',
'shouldPatchConsole',
'projectRoot',
'additionalReporters',
]);
export function optionsProxy(
options: ParcelOptions,
invalidateOnOptionChange: string => void,
addDevDependency?: (devDep: InternalDevDepOptions) => void,
): ParcelOptions {
let packageManager = addDevDependency
? proxyPackageManager(
options.projectRoot,
options.packageManager,
addDevDependency,
)
: options.packageManager;
return new Proxy(options, {
get(target, prop) {
if (prop === 'packageManager') {
return packageManager;
}
if (!ignoreOptions.has(prop)) {
invalidateOnOptionChange(prop);
}
return target[prop];
},
});
}
function proxyPackageManager(
projectRoot: FilePath,
packageManager: PackageManager,
addDevDependency: (devDep: InternalDevDepOptions) => void,
): PackageManager {
let require = (id: string, from: string, opts) => {
addDevDependency({
specifier: id,
resolveFrom: toProjectPath(projectRoot, from),
range: opts?.range,
});
return packageManager.require(id, from, opts);
};
return new Proxy(packageManager, {
get(target, prop) {
if (prop === 'require') {
return require;
}
// $FlowFixMe
return target[prop];
},
});
}
export function hashFromOption(value: mixed): string {
if (typeof value === 'object' && value != null) {
return hashObject(value);
}
return String(value);
}
export function invalidateOnFileCreateToInternal(
projectRoot: FilePath,
invalidation: FileCreateInvalidation,
): InternalFileCreateInvalidation {
if (invalidation.glob != null) {
return {glob: toProjectPath(projectRoot, invalidation.glob)};
} else if (invalidation.filePath != null) {
return {
filePath: toProjectPath(projectRoot, invalidation.filePath),
};
} else {
invariant(
invalidation.aboveFilePath != null && invalidation.fileName != null,
);
return {
fileName: invalidation.fileName,
aboveFilePath: toProjectPath(projectRoot, invalidation.aboveFilePath),
};
}
}
export function fromInternalSourceLocation(
projectRoot: FilePath,
loc: ?InternalSourceLocation,
): ?SourceLocation {
if (!loc) return loc;
return {
filePath: fromProjectPath(projectRoot, loc.filePath),
start: loc.start,
end: loc.end,
};
}
export function toInternalSourceLocation(
projectRoot: FilePath,
loc: ?SourceLocation,
): ?InternalSourceLocation {
if (!loc) return loc;
return {
filePath: toProjectPath(projectRoot, loc.filePath),
start: loc.start,
end: loc.end,
};
}
export function toInternalSymbols<T: {|loc: ?SourceLocation|}>(
projectRoot: FilePath,
symbols: ?Map<Symbol, T>,
): ?Map<
Symbol,
{|loc: ?InternalSourceLocation, ...$Rest<T, {|loc: ?SourceLocation|}>|},
> {
if (!symbols) return symbols;
return new Map(
[...symbols].map(([k, {loc, ...v}]) => [
k,
{
...v,
loc: toInternalSourceLocation(projectRoot, loc),
},
]),
);
}