-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathrollup.ts
More file actions
82 lines (69 loc) · 2.76 KB
/
Copy pathrollup.ts
File metadata and controls
82 lines (69 loc) · 2.76 KB
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
// Unless explicitly stated otherwise all files in this repository are licensed under the MIT License.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2019-Present Datadog, Inc.
import { getHighestPackageJsonDir, getNearestCommonDirectory } from '@dd/core/helpers/paths';
import path from 'path';
import type { InputOptions } from 'rollup';
// Compute the CWD based on a list of directories.
const getCwd = (dirs: Set<string>) => {
const dirsToUse: Set<string> = new Set(dirs);
for (const dir of dirs) {
const highestPackage = getHighestPackageJsonDir(dir);
if (highestPackage && !dirs.has(highestPackage)) {
dirsToUse.add(highestPackage);
}
}
// Fall back to the nearest common directory.
const nearestDir = getNearestCommonDirectory(Array.from(dirsToUse));
if (nearestDir === path.sep) {
return undefined;
}
return nearestDir;
};
export const getOutDirFromOutputs = (options: InputOptions) => {
const hasOutput = 'output' in options && options.output;
if (!hasOutput) {
return undefined;
}
const outputOptions = options.output;
const normalizedOutputOptions = Array.isArray(outputOptions) ? outputOptions : [outputOptions];
// FIXME: This is an oversimplification, we should handle builds with multiple outputs.
// Ideally, `outDir` should only be computed for the build-report.
// And build-report should also handle multiple outputs.
for (const output of normalizedOutputOptions) {
if (output.dir) {
return output.dir;
}
if (output.file) {
return path.dirname(output.file);
}
}
};
export const computeCwd = (options: InputOptions) => {
const directoriesForCwd: Set<string> = new Set();
if (options.input) {
const normalizedInput = Array.isArray(options.input)
? options.input
: typeof options.input === 'object'
? Object.values(options.input)
: [options.input];
for (const input of normalizedInput) {
if (typeof input !== 'string') {
throw new Error('Invalid input type');
}
directoriesForCwd.add(path.dirname(input));
}
}
// In case an absolute path has been provided in the output options,
// we include it in the directories list for CWD computation.
const outDirFromOutputs = getOutDirFromOutputs(options);
if (outDirFromOutputs && path.isAbsolute(outDirFromOutputs)) {
directoriesForCwd.add(outDirFromOutputs);
}
const cwd = getCwd(directoriesForCwd);
if (cwd) {
return cwd;
}
// Fallback to process.cwd() as would Vite and Rollup do in their own process.
return process.cwd();
};