-
Notifications
You must be signed in to change notification settings - Fork 382
/
compiler.ts
executable file
·65 lines (54 loc) · 1.68 KB
/
compiler.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
/*
* Copyright (c) 2018, salesforce.com, inc.
* All rights reserved.
* SPDX-License-Identifier: MIT
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
*/
import { CompilerDiagnostic, DiagnosticLevel } from '@lwc/errors';
import { bundle } from '../bundler/bundler';
import {
CompilerOptions,
validateOptions,
normalizeOptions,
NormalizedOutputConfig,
} from './options';
import { version } from '../index';
export { default as templateCompiler } from '@lwc/template-compiler';
export interface CompilerOutput {
success: boolean;
diagnostics: CompilerDiagnostic[];
result?: BundleResult;
version: string;
}
export interface BundleResult {
code: string;
map: SourceMap | null;
outputConfig: NormalizedOutputConfig;
}
export type SourceMap = any;
export async function compile(options: CompilerOptions): Promise<CompilerOutput> {
validateOptions(options);
const normalizedOptions = normalizeOptions(options);
let result: BundleResult | undefined;
const diagnostics: CompilerDiagnostic[] = [];
const { diagnostics: bundleDiagnostics, code, map } = await bundle(normalizedOptions);
diagnostics.push(...bundleDiagnostics);
if (!hasError(diagnostics)) {
result = {
code,
map,
outputConfig: normalizedOptions.outputConfig,
};
}
return {
version,
success: !hasError(diagnostics),
diagnostics,
result,
};
}
function hasError(diagnostics: CompilerDiagnostic[]) {
return diagnostics.some(d => {
return d.level === DiagnosticLevel.Error || d.level === DiagnosticLevel.Fatal;
});
}