/
Environment.js
159 lines (137 loc) Β· 3.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
// @flow
import type {
Environment as IEnvironment,
EnvironmentContext,
Engines,
OutputFormat,
PackageName,
VersionMap
} from '@parcel/types';
import type {Environment as InternalEnvironment} from '../types';
import nullthrows from 'nullthrows';
import browserslist from 'browserslist';
import semver from 'semver';
const BROWSER_ENVS = new Set([
'browser',
'web-worker',
'service-worker',
'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']);
const ISOLATED_ENVS = WORKER_ENVS;
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'
];
const ESMODULE_BROWSERS = {
edge: '16',
firefox: '60',
chrome: '61',
safari: '11',
opera: '48',
ios: '11',
android: '76',
and_chr: '76',
and_ff: '68',
samsung: '8.2'
};
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
constructor(env: InternalEnvironment) {
this.#environment = env;
_environmentToInternalEnvironment.set(this, env);
}
get context(): EnvironmentContext {
return this.#environment.context;
}
get engines(): Engines {
return this.#environment.engines;
}
get includeNodeModules(): boolean | Array<PackageName> {
return this.#environment.includeNodeModules;
}
get outputFormat(): OutputFormat {
return this.#environment.outputFormat;
}
get isLibrary(): boolean {
return this.#environment.isLibrary;
}
isBrowser() {
return BROWSER_ENVS.has(this.#environment.context);
}
isNode() {
return NODE_ENVS.has(this.#environment.context);
}
isElectron() {
return ELECTRON_ENVS.has(this.#environment.context);
}
isIsolated() {
return ISOLATED_ENVS.has(this.#environment.context);
}
isWorker() {
return WORKER_ENVS.has(this.#environment.context);
}
matchesEngines(minVersions: VersionMap) {
// 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(ESMODULE_BROWSERS)];
}
let matchedBrowsers = browserslist(browsers);
let minBrowsers = getExcludedBrowsers(minVersions);
let withoutMinBrowsers = browserslist([...browsers, ...minBrowsers]);
return matchedBrowsers.length === withoutMinBrowsers.length;
} else if (this.isNode() && this.engines.node != null && minVersions.node) {
// $FlowFixMe
return !semver.intersects(`< ${minVersions.node}`, this.engines.node);
}
return false;
}
}
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;
}