/
Environment.js
149 lines (137 loc) Β· 3.04 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
// @flow
import type {
EnvironmentOptions,
Environment as IEnvironment,
FilePath,
} from '@parcel/types';
import type {Environment, InternalSourceLocation} from './types';
import {hashString} from '@parcel/hash';
import {toInternalSourceLocation} from './utils';
import PublicEnvironment from './public/Environment';
import {environmentToInternalEnvironment} from './public/Environment';
const DEFAULT_ENGINES = {
browsers: ['> 0.25%'],
node: '>= 8.0.0',
};
type EnvironmentOpts = {|
...EnvironmentOptions,
loc?: ?InternalSourceLocation,
|};
export function createEnvironment({
context,
engines,
includeNodeModules,
outputFormat,
sourceType = 'module',
shouldOptimize = false,
isLibrary = false,
shouldScopeHoist = false,
sourceMap,
loc,
}: EnvironmentOpts = {}): Environment {
if (context == null) {
if (engines?.node) {
context = 'node';
} else if (engines?.browsers) {
context = 'browser';
} else {
context = 'browser';
}
}
if (engines == null) {
switch (context) {
case 'node':
case 'electron-main':
engines = {
node: DEFAULT_ENGINES.node,
};
break;
case 'browser':
case 'web-worker':
case 'service-worker':
case 'electron-renderer':
engines = {
browsers: DEFAULT_ENGINES.browsers,
};
break;
default:
engines = {};
}
}
if (includeNodeModules == null) {
switch (context) {
case 'node':
case 'electron-main':
case 'electron-renderer':
includeNodeModules = false;
break;
case 'browser':
case 'web-worker':
case 'service-worker':
default:
includeNodeModules = true;
break;
}
}
if (outputFormat == null) {
switch (context) {
case 'node':
case 'electron-main':
case 'electron-renderer':
outputFormat = 'commonjs';
break;
default:
outputFormat = 'global';
break;
}
}
let res: Environment = {
id: '',
context,
engines,
includeNodeModules,
outputFormat,
sourceType,
isLibrary,
shouldOptimize,
shouldScopeHoist,
sourceMap,
loc,
};
res.id = getEnvironmentHash(res);
return res;
}
export function mergeEnvironments(
projectRoot: FilePath,
a: Environment,
b: ?(EnvironmentOptions | IEnvironment),
): Environment {
// If merging the same object, avoid copying.
if (a === b || !b) {
return a;
}
if (b instanceof PublicEnvironment) {
return environmentToInternalEnvironment(b);
}
// $FlowFixMe - ignore the `id` that is already on a
return createEnvironment({
...a,
...b,
loc: b.loc ? toInternalSourceLocation(projectRoot, b.loc) : a.loc,
});
}
function getEnvironmentHash(env: Environment): string {
return hashString(
JSON.stringify([
env.context,
env.engines,
env.includeNodeModules,
env.outputFormat,
env.sourceType,
env.isLibrary,
env.shouldOptimize,
env.shouldScopeHoist,
env.sourceMap,
]),
);
}