/
Environment.js
298 lines (263 loc) Β· 6.83 KB
/
Environment.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
// @flow strict-local
import type {
Environment as IEnvironment,
EnvironmentContext,
EnvironmentFeature,
Engines,
OutputFormat,
PackageName,
VersionMap,
SourceLocation,
SourceType,
TargetSourceMapOptions,
} from '@parcel/types';
import type {Environment as InternalEnvironment, ParcelOptions} from '../types';
import nullthrows from 'nullthrows';
import browserslist from 'browserslist';
import semver from 'semver';
import {fromInternalSourceLocation} from '../utils';
const inspect = Symbol.for('nodejs.util.inspect.custom');
export const BROWSER_ENVS: Set<string> = new Set<string>([
'browser',
'web-worker',
'service-worker',
'worklet',
'electron-renderer',
]);
const ELECTRON_ENVS = new Set(['electron-main', 'electron-renderer']);
const NODE_ENVS = new Set(['node', ...ELECTRON_ENVS]);
const WORKER_ENVS = new Set(['web-worker', 'service-worker']);
export const ISOLATED_ENVS: Set<string> = new Set([...WORKER_ENVS, 'worklet']);
const ALL_BROWSERS = [
'chrome',
'and_chr',
'edge',
'firefox',
'and_ff',
'safari',
'ios',
'samsung',
'opera',
'ie',
'op_mini',
'blackberry',
'op_mob',
'ie_mob',
'and_uc',
'and_qq',
'baidu',
'kaios',
];
// See require("caniuse-api").getSupport(<feature name>)
const supportData = {
esmodules: {
edge: '16',
firefox: '60',
chrome: '61',
safari: '10.1',
opera: '48',
ios: '10.3',
android: '76',
and_chr: '76',
and_ff: '68',
samsung: '8.2',
and_qq: '10.4',
op_mob: '64',
},
'dynamic-import': {
edge: '76',
firefox: '67',
chrome: '63',
safari: '11.1',
opera: '50',
ios: '11.3',
android: '63',
and_chr: '63',
and_ff: '67',
samsung: '8',
and_qq: '10.4',
op_mob: '64',
},
'worker-module': {
edge: '80',
chrome: '80',
opera: '67',
android: '81',
and_chr: '86',
},
'service-worker-module': {
// TODO: Safari 14.1??
},
'import-meta-url': {
edge: '79',
firefox: '62',
chrome: '64',
safari: '11.1',
opera: '51',
ios: '12',
android: '64',
and_chr: '64',
and_ff: '62',
samsung: '9.2',
and_qq: '10.4',
op_mob: '64',
},
'arrow-functions': {
chrome: '47',
opera: '34',
edge: '13',
firefox: '45',
safari: '10',
node: '6',
ios: '10',
samsung: '5',
electron: '0.36',
android: '50',
qq: '10.4',
baidu: '7.12',
kaios: '2.5',
and_chr: '50',
and_qq: '12.12',
op_mob: '64',
},
};
const internalEnvironmentToEnvironment: WeakMap<
InternalEnvironment,
Environment,
> = new WeakMap();
const _environmentToInternalEnvironment: WeakMap<
IEnvironment,
InternalEnvironment,
> = new WeakMap();
export function environmentToInternalEnvironment(
environment: IEnvironment,
): InternalEnvironment {
return nullthrows(_environmentToInternalEnvironment.get(environment));
}
export default class Environment implements IEnvironment {
#environment /*: InternalEnvironment */;
#options /*: ParcelOptions */;
constructor(env: InternalEnvironment, options: ParcelOptions): Environment {
let existing = internalEnvironmentToEnvironment.get(env);
if (existing != null) {
return existing;
}
this.#environment = env;
this.#options = options;
_environmentToInternalEnvironment.set(this, env);
internalEnvironmentToEnvironment.set(env, this);
return this;
}
get id(): string {
return this.#environment.id;
}
get context(): EnvironmentContext {
return this.#environment.context;
}
get engines(): Engines {
return this.#environment.engines;
}
get includeNodeModules():
| boolean
| Array<PackageName>
| {[PackageName]: boolean, ...} {
return this.#environment.includeNodeModules;
}
get outputFormat(): OutputFormat {
return this.#environment.outputFormat;
}
get sourceType(): SourceType {
return this.#environment.sourceType;
}
get isLibrary(): boolean {
return this.#environment.isLibrary;
}
get shouldOptimize(): boolean {
return this.#environment.shouldOptimize;
}
get shouldScopeHoist(): boolean {
return this.#environment.shouldScopeHoist;
}
get sourceMap(): ?TargetSourceMapOptions {
return this.#environment.sourceMap;
}
get loc(): ?SourceLocation {
return fromInternalSourceLocation(
this.#options.projectRoot,
this.#environment.loc,
);
}
// $FlowFixMe[unsupported-syntax]
[inspect](): string {
return `Env(${this.#environment.context})`;
}
isBrowser(): boolean {
return BROWSER_ENVS.has(this.#environment.context);
}
isNode(): boolean {
return NODE_ENVS.has(this.#environment.context);
}
isElectron(): boolean {
return ELECTRON_ENVS.has(this.#environment.context);
}
isIsolated(): boolean {
return ISOLATED_ENVS.has(this.#environment.context);
}
isWorker(): boolean {
return WORKER_ENVS.has(this.#environment.context);
}
isWorklet(): boolean {
return this.#environment.context === 'worklet';
}
matchesEngines(
minVersions: VersionMap,
defaultValue?: boolean = false,
): boolean {
// Determine if the environment matches some minimum version requirements.
// For browsers, we run a browserslist query with and without the minimum
// required browsers and compare the lists. For node, we just check semver.
if (this.isBrowser() && this.engines.browsers != null) {
let targetBrowsers = this.engines.browsers;
let browsers =
targetBrowsers != null && !Array.isArray(targetBrowsers)
? [targetBrowsers]
: targetBrowsers;
// If outputting esmodules, exclude browsers without support.
if (this.outputFormat === 'esmodule') {
browsers = [...browsers, ...getExcludedBrowsers(supportData.esmodules)];
}
let matchedBrowsers = browserslist(browsers);
if (matchedBrowsers.length === 0) {
return false;
}
let minBrowsers = getExcludedBrowsers(minVersions);
let withoutMinBrowsers = browserslist([...browsers, ...minBrowsers]);
return matchedBrowsers.length === withoutMinBrowsers.length;
} else if (this.isNode() && this.engines.node != null) {
return (
minVersions.node != null &&
!semver.intersects(`< ${minVersions.node}`, this.engines.node)
);
}
return defaultValue;
}
supports(feature: EnvironmentFeature, defaultValue?: boolean): boolean {
let engines = supportData[feature];
if (!engines) {
throw new Error('Unknown environment feature: ' + feature);
}
return this.matchesEngines(engines, defaultValue);
}
}
function getExcludedBrowsers(minVersions: VersionMap) {
let browsers = [];
for (let browser of ALL_BROWSERS) {
let version = minVersions[browser];
if (version) {
browsers.push(`not ${browser} < ${version}`);
} else {
browsers.push(`not ${browser} > 0`);
}
}
return browsers;
}