-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathcompile.ts
More file actions
190 lines (171 loc) · 6.53 KB
/
Copy pathcompile.ts
File metadata and controls
190 lines (171 loc) · 6.53 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
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
181
182
183
184
185
186
187
188
189
190
import * as fsp from "node:fs/promises";
import path from "node:path";
import { BundleIdentity } from "./bundle";
export interface CollectedDiagnostic {
code: number;
message: string;
filename?: string;
location?: { line: number; column: number; start?: number; length?: number };
level: number;
url?: string;
}
// Compile a single LWC file using both open-source and platform compilers.
// Returns a merged, deduplicated list of diagnostics from both paths.
//
// Path 1: @lwc/compiler transformSync — throws CompilerError/CompilerAggregateError (codes 1001-1213)
// Path 2: @lwc/sfdc-lwc-compiler compile() — returns diagnostics on output (codes 1500-1538)
export async function compileAndCollect(
file: string,
bundle: BundleIdentity
): Promise<CollectedDiagnostic[]> {
const source = await fsp.readFile(file, "utf-8");
const bundleFiles = await readBundleFiles(file, bundle.name);
// Sequential — not parallel. Both paths import @lwc/compiler internally;
// concurrent dynamic import() of the same ESM module causes a Node race condition.
const openSourceDiags = await collectFromTransformSync(source, file, bundle);
const platformDiags = await collectFromPlatformCompile(bundleFiles, bundle);
return deduplicateDiagnostics([...openSourceDiags, ...platformDiags]);
}
// Read all sibling files in the bundle directory that belong to this component.
async function readBundleFiles(file: string, name: string): Promise<Record<string, string>> {
const dir = path.dirname(file);
const files: Record<string, string> = {};
try {
const entries = await fsp.readdir(dir);
for (const entry of entries) {
const stem = path.basename(entry, path.extname(entry));
if (stem === name && !entry.includes("__tests__")) {
const content = await fsp.readFile(path.join(dir, entry), "utf-8");
files[entry] = content;
}
}
} catch {
// If we can't read the directory, just use the single file
files[path.basename(file)] = await fsp.readFile(file, "utf-8");
}
return files;
}
// Path 1: open-source @lwc/compiler (throws on error)
async function collectFromTransformSync(
source: string,
file: string,
bundle: BundleIdentity
): Promise<CollectedDiagnostic[]> {
const lwcCompiler = await import("@lwc/compiler");
const lwcErrors = await import("@lwc/errors");
const { transformSync } = lwcCompiler as { transformSync: (src: string, filename: string, opts: { name: string; namespace: string }) => unknown };
const { CompilerError, CompilerAggregateError } = lwcErrors as {
CompilerError: new (...args: unknown[]) => Error & CollectedDiagnostic;
CompilerAggregateError: new (...args: unknown[]) => Error & { errors: (Error & CollectedDiagnostic)[] };
};
try {
transformSync(source, file, { name: bundle.name, namespace: bundle.namespace });
return [];
} catch (e) {
if (e instanceof CompilerError) {
return [unwrap(e)];
}
if (e instanceof CompilerAggregateError) {
return e.errors.map(unwrap);
}
const err = e as Error;
return [{
code: 1001,
message: `Unexpected compilation error: ${err.message}`,
filename: file,
level: 1,
}];
}
}
// Path 2: @lwc/sfdc-lwc-compiler platform compile (returns diagnostics, doesn't throw)
async function collectFromPlatformCompile(
bundleFiles: Record<string, string>,
bundle: BundleIdentity
): Promise<CollectedDiagnostic[]> {
try {
const sfdcCompiler = await import("@lwc/sfdc-lwc-compiler");
const compile = (sfdcCompiler as { compile: (config: unknown) => Promise<PlatformOutput> }).compile;
const output = await compile({
bundle: {
type: "platform" as const,
name: bundle.name,
namespace: bundle.namespace,
files: bundleFiles,
},
});
const diagnostics: CollectedDiagnostic[] = [];
// Top-level diagnostics
if (output.diagnostics) {
for (const d of output.diagnostics) {
if (isPlatformDiagnostic(d)) diagnostics.push(toDiag(d));
}
}
// Per-bundle result diagnostics
if (output.results) {
for (const result of output.results) {
if (result.diagnostics) {
for (const d of result.diagnostics) {
if (isPlatformDiagnostic(d)) diagnostics.push(toDiag(d));
}
}
}
}
return diagnostics;
} catch (_err) {
// If platform compile fails entirely (missing deps, version mismatch), fall back silently.
// Open-source path still provides coverage for codes 1001-1213.
// Platform compile unavailable — open-source path still covers codes 1001-1213.
return [];
}
}
// Only keep diagnostics in the platform range (1500+) to avoid double-counting
// open-source errors that both compilers might emit.
function isPlatformDiagnostic(d: unknown): d is RawDiagnostic {
if (!d || typeof d !== "object") return false;
const obj = d as Record<string, unknown>;
return typeof obj.code === "number" && obj.code >= 1500;
}
function toDiag(d: RawDiagnostic): CollectedDiagnostic {
return {
code: d.code,
message: d.message ?? "",
filename: d.filename,
location: d.location,
level: d.level ?? 1,
url: d.url,
};
}
function unwrap(e: Error & CollectedDiagnostic): CollectedDiagnostic {
return {
code: e.code,
message: e.message,
filename: e.filename,
location: e.location,
level: e.level,
url: e.url,
};
}
function deduplicateDiagnostics(diags: CollectedDiagnostic[]): CollectedDiagnostic[] {
const seen = new Set<string>();
const result: CollectedDiagnostic[] = [];
for (const d of diags) {
const key = `${d.code}|${d.message}|${d.filename ?? ""}|${d.location?.line ?? ""}|${d.location?.column ?? ""}`;
if (!seen.has(key)) {
seen.add(key);
result.push(d);
}
}
return result;
}
interface RawDiagnostic {
code: number;
message?: string;
filename?: string;
location?: { line: number; column: number; start?: number; length?: number };
level?: number;
url?: string;
}
interface PlatformOutput {
diagnostics?: unknown[];
results?: Array<{ diagnostics?: unknown[] }>;
}