Skip to content

Commit 7bb9b5e

Browse files
committed
fixup! fix(@angular/build): improve library watch mode state invalidation and lazy asset matchers
1 parent 961ee0c commit 7bb9b5e

2 files changed

Lines changed: 58 additions & 147 deletions

File tree

‎packages/angular/build/src/builders/library/pipeline/compilation.ts‎

Lines changed: 58 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ export async function compileLibrary(
5454
rootFiles.push(ep.entryFilePath);
5555
}
5656

57-
let compilationInstance =
57+
const compilationInstance =
5858
cached?.compilationInstance ?? (await createAngularCompilation('aot', false));
5959

6060
try {
@@ -114,45 +114,21 @@ export async function compileLibrary(
114114
processWebWorker: () => '',
115115
};
116116

117-
const compilerOptionOverrides = {
118-
sourcemap: true,
119-
preserveSymlinks,
120-
rootFiles,
121-
declarationMap,
122-
compilationMode,
123-
paths: entryPathsMap,
124-
cachePath: cacheOptions.enabled ? cacheOptions.path : undefined,
125-
};
126-
127-
let { referencedFiles, compilerOptions } = await compilationInstance.initialize(
117+
const { referencedFiles, compilerOptions } = await compilationInstance.initialize(
128118
tsConfigPath,
129119
hostOptions,
130-
compilerOptionOverrides,
120+
{
121+
sourcemap: true,
122+
preserveSymlinks,
123+
rootFiles,
124+
declarationMap,
125+
compilationMode,
126+
paths: entryPathsMap,
127+
cachePath: cacheOptions.enabled ? cacheOptions.path : undefined,
128+
},
131129
'library',
132130
);
133131

134-
const effectiveCompilationMode =
135-
compilerOptions.compilationMode === 'full' ? 'full' : 'partial';
136-
const hasCompilationModeChanged =
137-
buildState.compilationMode !== undefined &&
138-
buildState.compilationMode !== effectiveCompilationMode;
139-
140-
if (hasCompilationModeChanged && cached) {
141-
await compilationInstance.close?.();
142-
compilationInstance = await createAngularCompilation('aot', false);
143-
hostOptions.modifiedFiles = undefined;
144-
stylesheetWarnings.length = 0;
145-
stylesheetErrors.length = 0;
146-
failedFiles.clear();
147-
({ referencedFiles, compilerOptions } = await compilationInstance.initialize(
148-
tsConfigPath,
149-
hostOptions,
150-
compilerOptionOverrides,
151-
'library',
152-
));
153-
}
154-
155-
const effectiveDeclarationMap = !!compilerOptions.declarationMap;
156132
const hasStylesheetErrors = stylesheetErrors.length > 0;
157133
const emittedFiles = hasStylesheetErrors ? [] : await compilationInstance.emitAffectedFiles();
158134
const diagnosePromise = runDiagnosticsAndFormat(
@@ -163,14 +139,51 @@ export async function compileLibrary(
163139
buildState,
164140
);
165141

142+
const esmFiles = cached?.esmFiles ?? new Map<string, string>();
143+
const dtsFiles = cached?.dtsFiles ?? new Map<string, string>();
144+
const changedEsmFiles = new Set<string>();
145+
const changedDtsFiles = new Set<string>();
166146
const posixReferencedFiles = new Set(referencedFiles.map(toPosixPath));
167-
const { esmFiles, dtsFiles, changedEsmFiles, changedDtsFiles } = updateEmittedFileMaps(
168-
emittedFiles,
169-
cached,
170-
posixReferencedFiles,
171-
effectiveModifiedFiles,
172-
hasCompilationModeChanged,
173-
);
147+
148+
if (cached && effectiveModifiedFiles) {
149+
for (const modifiedFile of effectiveModifiedFiles) {
150+
if (posixReferencedFiles.has(modifiedFile)) {
151+
continue;
152+
}
153+
154+
const basePathWithoutExt = modifiedFile.replace(/(?:\.d\.[cm]?ts|\.[cm]?[jt]sx?)$/i, '');
155+
if (basePathWithoutExt === modifiedFile || existsSync(modifiedFile)) {
156+
continue;
157+
}
158+
159+
for (const ext of EMITTED_EXTENSIONS) {
160+
const outputPath = `${basePathWithoutExt}${ext}`;
161+
const mapPath = `${outputPath}.map`;
162+
if (esmFiles.delete(outputPath)) {
163+
changedEsmFiles.add(outputPath);
164+
}
165+
if (dtsFiles.delete(outputPath)) {
166+
changedDtsFiles.add(outputPath);
167+
}
168+
esmFiles.delete(mapPath);
169+
dtsFiles.delete(mapPath);
170+
}
171+
}
172+
}
173+
174+
for (const { filename, contents } of emittedFiles) {
175+
const normalized = toPosixPath(filename);
176+
const isMap = normalized.endsWith('.map');
177+
const isDts = isMap ? isDeclarationSourceMapFile(normalized) : isDeclarationFile(normalized);
178+
const targetFiles = isDts ? dtsFiles : esmFiles;
179+
180+
if (targetFiles.get(normalized) !== contents) {
181+
targetFiles.set(normalized, contents);
182+
(isDts ? changedDtsFiles : changedEsmFiles).add(
183+
isMap ? normalized.slice(0, -4) : normalized,
184+
);
185+
}
186+
}
174187

175188
return {
176189
esmFiles,
@@ -186,83 +199,17 @@ export async function compileLibrary(
186199
},
187200
hasStylesheetErrors,
188201
diagnosePromise,
189-
declarationMap: effectiveDeclarationMap,
190-
compilationMode: effectiveCompilationMode,
202+
declarationMap: !!compilerOptions.declarationMap,
203+
compilationMode: compilerOptions.compilationMode === 'full' ? 'full' : 'partial',
191204
};
192205
} catch (error) {
193-
if (!cached || compilationInstance !== cached.compilationInstance) {
206+
if (!cached) {
194207
await compilationInstance.close?.();
195208
}
196209
throw error;
197210
}
198211
}
199212

200-
/**
201-
* Updates cached in-memory ESM and DTS maps with newly emitted files and prunes deleted files.
202-
*/
203-
function updateEmittedFileMaps(
204-
emittedFiles: Iterable<{ filename: string; contents: string }>,
205-
cached: BuilderState['singleProgramCache'],
206-
posixReferencedFiles: ReadonlySet<string>,
207-
effectiveModifiedFiles: ReadonlySet<string> | undefined,
208-
hasCompilationModeChanged: boolean,
209-
): {
210-
esmFiles: Map<string, string>;
211-
dtsFiles: Map<string, string>;
212-
changedEsmFiles: Set<string>;
213-
changedDtsFiles: Set<string>;
214-
} {
215-
const esmFiles = cached?.esmFiles ?? new Map<string, string>();
216-
const dtsFiles = cached?.dtsFiles ?? new Map<string, string>();
217-
if (hasCompilationModeChanged) {
218-
esmFiles.clear();
219-
dtsFiles.clear();
220-
}
221-
222-
const changedEsmFiles = new Set<string>();
223-
const changedDtsFiles = new Set<string>();
224-
225-
if (cached && !hasCompilationModeChanged && effectiveModifiedFiles) {
226-
for (const modifiedFile of effectiveModifiedFiles) {
227-
if (posixReferencedFiles.has(modifiedFile)) {
228-
continue;
229-
}
230-
231-
const basePathWithoutExt = modifiedFile.replace(/(?:\.d\.[cm]?ts|\.[cm]?[jt]sx?)$/i, '');
232-
if (basePathWithoutExt === modifiedFile || existsSync(modifiedFile)) {
233-
continue;
234-
}
235-
236-
for (const ext of EMITTED_EXTENSIONS) {
237-
const outputPath = `${basePathWithoutExt}${ext}`;
238-
const mapPath = `${outputPath}.map`;
239-
if (esmFiles.delete(outputPath)) {
240-
changedEsmFiles.add(outputPath);
241-
}
242-
if (dtsFiles.delete(outputPath)) {
243-
changedDtsFiles.add(outputPath);
244-
}
245-
esmFiles.delete(mapPath);
246-
dtsFiles.delete(mapPath);
247-
}
248-
}
249-
}
250-
251-
for (const { filename, contents } of emittedFiles) {
252-
const normalized = toPosixPath(filename);
253-
const isMap = normalized.endsWith('.map');
254-
const isDts = isMap ? isDeclarationSourceMapFile(normalized) : isDeclarationFile(normalized);
255-
const targetFiles = isDts ? dtsFiles : esmFiles;
256-
257-
if (targetFiles.get(normalized) !== contents) {
258-
targetFiles.set(normalized, contents);
259-
(isDts ? changedDtsFiles : changedEsmFiles).add(isMap ? normalized.slice(0, -4) : normalized);
260-
}
261-
}
262-
263-
return { esmFiles, dtsFiles, changedEsmFiles, changedDtsFiles };
264-
}
265-
266213
/**
267214
* Formats stylesheet bundling diagnostics and TypeScript/Angular diagnostics
268215
* and records them on the build state's execution result.

‎packages/angular/build/src/builders/library/tests/behavior/watch_spec.ts‎

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -905,41 +905,5 @@ describeLibraryBuilder(executeLibraryBuilder, LIBRARY_BUILDER_INFO, (harness) =>
905905
},
906906
]);
907907
});
908-
909-
it('should recompile bundles when compilationMode and declarationMap change in tsconfig in watch mode', async () => {
910-
harness.useTarget('build', {
911-
...BASE_OPTIONS,
912-
watch: true,
913-
});
914-
915-
await harness.executeWithCases([
916-
async ({ result }) => {
917-
expect(result?.success).toBeTrue();
918-
expect(harness.readFile('dist/lib/fesm2022/lib.mjs')).toContain('ɵɵngDeclareComponent');
919-
expect(harness.hasFile('dist/lib/types/lib.d.ts.map')).toBeFalse();
920-
921-
await harness.modifyFile('projects/lib/tsconfig.lib.json', (content) => {
922-
const tsconfig = JSON.parse(content);
923-
tsconfig.compilerOptions = {
924-
...tsconfig.compilerOptions,
925-
declarationMap: true,
926-
};
927-
tsconfig.angularCompilerOptions = {
928-
...tsconfig.angularCompilerOptions,
929-
compilationMode: 'full',
930-
};
931-
932-
return JSON.stringify(tsconfig, null, 2);
933-
});
934-
},
935-
({ result }) => {
936-
expect(result?.success).toBeTrue();
937-
const mjs = harness.readFile('dist/lib/fesm2022/lib.mjs');
938-
expect(mjs).toContain('ɵɵdefineComponent');
939-
expect(mjs).not.toContain('ɵɵngDeclareComponent');
940-
expect(harness.hasFile('dist/lib/types/lib.d.ts.map')).toBeTrue();
941-
},
942-
]);
943-
});
944908
});
945909
});

0 commit comments

Comments
 (0)