-
Notifications
You must be signed in to change notification settings - Fork 12k
/
Copy pathesbuild.ts
72 lines (67 loc) · 2.2 KB
/
esbuild.ts
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
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import { BuilderContext } from '@angular-devkit/architect';
import {
BuildFailure,
BuildOptions,
BuildResult,
Message,
OutputFile,
build,
formatMessages,
} from 'esbuild';
/**
* Determines if an unknown value is an esbuild BuildFailure error object thrown by esbuild.
* @param value A potential esbuild BuildFailure error object.
* @returns `true` if the object is determined to be a BuildFailure object; otherwise, `false`.
*/
export function isEsBuildFailure(value: unknown): value is BuildFailure {
return !!value && typeof value === 'object' && 'errors' in value && 'warnings' in value;
}
/**
* Executes the esbuild build function and normalizes the build result in the event of a
* build failure that results in no output being generated.
* All builds use the `write` option with a value of `false` to allow for the output files
* build result array to be populated.
*
* @param options The esbuild options object to use when building.
* @returns If output files are generated, the full esbuild BuildResult; if not, the
* warnings and errors for the attempted build.
*/
export async function bundle(
options: BuildOptions,
): Promise<
(BuildResult & { outputFiles: OutputFile[] }) | (BuildFailure & { outputFiles?: never })
> {
try {
return await build({
...options,
write: false,
});
} catch (failure) {
// Build failures will throw an exception which contains errors/warnings
if (isEsBuildFailure(failure)) {
return failure;
} else {
throw failure;
}
}
}
export async function logMessages(
context: BuilderContext,
{ errors, warnings }: { errors: Message[]; warnings: Message[] },
): Promise<void> {
if (warnings.length) {
const warningMessages = await formatMessages(warnings, { kind: 'warning', color: true });
context.logger.warn(warningMessages.join('\n'));
}
if (errors.length) {
const errorMessages = await formatMessages(errors, { kind: 'error', color: true });
context.logger.error(errorMessages.join('\n'));
}
}