1 |
|
2 |
|
3 | import type {FilePath, InitialParcelOptions} from '@parcel/types';
|
4 | import type {ParcelOptions} from './types';
|
5 |
|
6 | import {getRootDir} from '@parcel/utils';
|
7 | import loadDotEnv from './loadDotEnv';
|
8 | import path from 'path';
|
9 | import {resolveConfig} from '@parcel/utils';
|
10 | import {NodeFS} from '@parcel/fs';
|
11 | import Cache from '@parcel/cache';
|
12 | import {NodePackageManager} from '@parcel/package-manager';
|
13 |
|
14 |
|
15 | const DEFAULT_CACHE_DIRNAME = '.parcel-cache';
|
16 | const LOCK_FILE_NAMES = ['yarn.lock', 'package-lock.json', 'pnpm-lock.yaml'];
|
17 |
|
18 | export default async function resolveOptions(
|
19 | initialOptions: InitialParcelOptions,
|
20 | ): Promise<ParcelOptions> {
|
21 | let entries: Array<FilePath>;
|
22 | if (initialOptions.entries == null || initialOptions.entries === '') {
|
23 | entries = [];
|
24 | } else if (Array.isArray(initialOptions.entries)) {
|
25 | entries = initialOptions.entries.map(entry => path.resolve(entry));
|
26 | } else {
|
27 | entries = [path.resolve(initialOptions.entries)];
|
28 | }
|
29 |
|
30 | let inputFS = initialOptions.inputFS || new NodeFS();
|
31 | let outputFS = initialOptions.outputFS || new NodeFS();
|
32 |
|
33 | let packageManager =
|
34 | initialOptions.packageManager || new NodePackageManager(inputFS);
|
35 |
|
36 | let rootDir =
|
37 | initialOptions.rootDir != null
|
38 | ? path.resolve(initialOptions.rootDir)
|
39 | : getRootDir(entries);
|
40 |
|
41 | let projectRootFile =
|
42 | (await resolveConfig(inputFS, path.join(rootDir, 'index'), [
|
43 | ...LOCK_FILE_NAMES,
|
44 | '.git',
|
45 | '.hg',
|
46 | ])) || path.join(inputFS.cwd(), 'index');
|
47 |
|
48 | let lockFile = null;
|
49 | let rootFileName = path.basename(projectRootFile);
|
50 | if (LOCK_FILE_NAMES.includes(rootFileName)) {
|
51 | lockFile = projectRootFile;
|
52 | }
|
53 | let projectRoot = path.dirname(projectRootFile);
|
54 |
|
55 | let outputCwd = outputFS.cwd();
|
56 | let cacheDir =
|
57 |
|
58 |
|
59 | initialOptions.cacheDir != null
|
60 | ? path.resolve(outputCwd, initialOptions.cacheDir)
|
61 | : path.resolve(projectRoot, DEFAULT_CACHE_DIRNAME);
|
62 |
|
63 | let cache = new Cache(outputFS, cacheDir);
|
64 |
|
65 | let mode = initialOptions.mode ?? 'development';
|
66 | let minify = initialOptions.minify ?? mode === 'production';
|
67 |
|
68 | return {
|
69 | config: initialOptions.config,
|
70 | defaultConfig: initialOptions.defaultConfig,
|
71 | patchConsole:
|
72 | initialOptions.patchConsole ?? process.env.NODE_ENV !== 'test',
|
73 | env: {
|
74 | ...initialOptions.env,
|
75 |
|
76 | ...(await loadDotEnv(
|
77 | initialOptions.env ?? {},
|
78 | inputFS,
|
79 | path.join(projectRoot, 'index'),
|
80 | )),
|
81 | },
|
82 | mode,
|
83 | minify,
|
84 | autoinstall: initialOptions.autoinstall ?? true,
|
85 | hot: initialOptions.hot ?? false,
|
86 | serve: initialOptions.serve ?? false,
|
87 | disableCache: initialOptions.disableCache ?? false,
|
88 | killWorkers: initialOptions.killWorkers ?? true,
|
89 | profile: initialOptions.profile ?? false,
|
90 | cacheDir,
|
91 | entries,
|
92 | rootDir,
|
93 | defaultEngines: initialOptions.defaultEngines,
|
94 | targets: initialOptions.targets,
|
95 | sourceMaps: initialOptions.sourceMaps ?? true,
|
96 | scopeHoist:
|
97 | initialOptions.scopeHoist ?? initialOptions.mode === 'production',
|
98 | logLevel: initialOptions.logLevel ?? 'info',
|
99 | projectRoot,
|
100 | lockFile,
|
101 | inputFS,
|
102 | outputFS,
|
103 | cache,
|
104 | packageManager,
|
105 | };
|
106 | }
|