/
resolveOptions.js
180 lines (163 loc) Β· 5.9 KB
/
resolveOptions.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
// @flow strict-local
import type {
FilePath,
InitialParcelOptions,
DependencySpecifier,
} from '@parcel/types';
import type {FileSystem} from '@parcel/fs';
import type {ParcelOptions} from './types';
import path from 'path';
import {hashString} from '@parcel/hash';
import {NodeFS} from '@parcel/fs';
import {LMDBCache, FSCache} from '@parcel/cache';
import {NodePackageManager} from '@parcel/package-manager';
import {getRootDir, relativePath, resolveConfig} from '@parcel/utils';
import loadDotEnv from './loadDotEnv';
import {toProjectPath} from './projectPath';
import {getResolveFrom} from './requests/ParcelConfigRequest';
// Default cache directory name
const DEFAULT_CACHE_DIRNAME = '.parcel-cache';
const LOCK_FILE_NAMES = ['yarn.lock', 'package-lock.json', 'pnpm-lock.yaml'];
// Generate a unique instanceId, will change on every run of parcel
function generateInstanceId(entries: Array<FilePath>): string {
return hashString(
`${entries.join(',')}-${Date.now()}-${Math.round(Math.random() * 100)}`,
);
}
export default async function resolveOptions(
initialOptions: InitialParcelOptions,
): Promise<ParcelOptions> {
let inputFS = initialOptions.inputFS || new NodeFS();
let outputFS = initialOptions.outputFS || new NodeFS();
let inputCwd = inputFS.cwd();
let outputCwd = outputFS.cwd();
let entries: Array<FilePath>;
if (initialOptions.entries == null || initialOptions.entries === '') {
entries = [];
} else if (Array.isArray(initialOptions.entries)) {
entries = initialOptions.entries.map(entry =>
path.resolve(inputCwd, entry),
);
} else {
entries = [path.resolve(inputCwd, initialOptions.entries)];
}
let entryRoot = getRootDir(entries);
let projectRootFile =
(await resolveConfig(
inputFS,
path.join(entryRoot, 'index'),
[...LOCK_FILE_NAMES, '.git', '.hg'],
path.parse(entryRoot).root,
)) || path.join(inputCwd, 'index'); // ? Should this just be rootDir
let projectRoot = path.dirname(projectRootFile);
let packageManager =
initialOptions.packageManager ||
new NodePackageManager(inputFS, projectRoot);
let cacheDir =
// If a cacheDir is provided, resolve it relative to cwd. Otherwise,
// use a default directory resolved relative to the project root.
initialOptions.cacheDir != null
? path.resolve(outputCwd, initialOptions.cacheDir)
: path.resolve(projectRoot, DEFAULT_CACHE_DIRNAME);
let cache =
initialOptions.cache ??
(outputFS instanceof NodeFS
? new LMDBCache(outputFS, cacheDir)
: new FSCache(outputFS, cacheDir));
let mode = initialOptions.mode ?? 'development';
let shouldOptimize =
initialOptions?.defaultTargetOptions?.shouldOptimize ??
mode === 'production';
let publicUrl = initialOptions?.defaultTargetOptions?.publicUrl ?? '/';
let distDir =
initialOptions?.defaultTargetOptions?.distDir != null
? path.resolve(inputCwd, initialOptions?.defaultTargetOptions?.distDir)
: undefined;
let shouldBuildLazily = initialOptions.shouldBuildLazily ?? false;
let shouldContentHash =
initialOptions.shouldContentHash ?? initialOptions.mode === 'production';
if (shouldBuildLazily && shouldContentHash) {
throw new Error('Lazy bundling does not work with content hashing');
}
return {
config: getRelativeConfigSpecifier(
inputFS,
projectRoot,
initialOptions.config,
),
defaultConfig: getRelativeConfigSpecifier(
inputFS,
projectRoot,
initialOptions.defaultConfig,
),
shouldPatchConsole: initialOptions.shouldPatchConsole ?? false,
env: {
...(await loadDotEnv(
initialOptions.env ?? {},
inputFS,
path.join(projectRoot, 'index'),
projectRoot,
)),
...process.env,
...initialOptions.env,
},
mode,
shouldAutoInstall: initialOptions.shouldAutoInstall ?? false,
hmrOptions: initialOptions.hmrOptions ?? null,
shouldBuildLazily,
shouldContentHash,
serveOptions: initialOptions.serveOptions
? {
...initialOptions.serveOptions,
distDir: distDir ?? path.join(outputCwd, 'dist'),
}
: false,
shouldDisableCache: initialOptions.shouldDisableCache ?? false,
shouldProfile: initialOptions.shouldProfile ?? false,
cacheDir,
entries: entries.map(e => toProjectPath(projectRoot, e)),
targets: initialOptions.targets,
logLevel: initialOptions.logLevel ?? 'info',
projectRoot,
inputFS,
outputFS,
cache,
packageManager,
additionalReporters:
initialOptions.additionalReporters?.map(({packageName, resolveFrom}) => ({
packageName,
resolveFrom: toProjectPath(projectRoot, resolveFrom),
})) ?? [],
instanceId: generateInstanceId(entries),
detailedReport: initialOptions.detailedReport,
defaultTargetOptions: {
shouldOptimize,
shouldScopeHoist: initialOptions?.defaultTargetOptions?.shouldScopeHoist,
sourceMaps: initialOptions?.defaultTargetOptions?.sourceMaps ?? true,
publicUrl,
...(distDir != null
? {distDir: toProjectPath(projectRoot, distDir)}
: {...null}),
engines: initialOptions?.defaultTargetOptions?.engines,
outputFormat: initialOptions?.defaultTargetOptions?.outputFormat,
isLibrary: initialOptions?.defaultTargetOptions?.isLibrary,
},
};
}
function getRelativeConfigSpecifier(
fs: FileSystem,
projectRoot: FilePath,
specifier: ?DependencySpecifier,
) {
if (specifier == null) {
return undefined;
} else if (path.isAbsolute(specifier)) {
let resolveFrom = getResolveFrom(fs, projectRoot);
let relative = relativePath(path.dirname(resolveFrom), specifier);
// If the config is outside the project root, use an absolute path so that if the project root
// moves the path still works. Otherwise, use a relative path so that the cache is portable.
return relative.startsWith('..') ? specifier : relative;
} else {
return specifier;
}
}