MCPcopy Create free account
hub / github.com/angular/angular / serializeOutput

Function serializeOutput

packages/compiler-cli/preprocessor/src/processor.ts:1901–1979  ·  view source on GitHub ↗

* The `SerializeOutput` pass: prints and appends static fields and trailing statements for all * compiled classes, then the imports and shared constants at the top of the file.

(file: FileCompilation)

Source from the content-addressed store, hash-verified

1899 * compiled classes, then the imports and shared constants at the top of the file.
1900 */
1901function serializeOutput(file: FileCompilation): void {
1902 const {
1903 ctx,
1904 filePath,
1905 content,
1906 s,
1907 result,
1908 printer,
1909 out,
1910 sharedPool,
1911 fileDiagnostics,
1912 compiledClasses,
1913 deferredOnlyNames,
1914 eagerlyBoundNames,
1915 eagerlyReferencedNames,
1916 eagerDependencyNames,
1917 } = file;
1918 const {classes, importsEnd, imports: importDeclarations} = result;
1919 for (const compiledClass of compiledClasses) {
1920 appendCompiledClass(compiledClass, out, printer, ctx);
1921 }
1922
1923 for (const local of eagerDependencyNames) {
1924 eagerlyBoundNames.add(local);
1925 eagerlyReferencedNames.add(local);
1926 deferredOnlyNames.delete(local);
1927 }
1928
1929 reportEagerlyImportedDeferredDependencies(
1930 fileDiagnostics,
1931 filePath,
1932 importDeclarations,
1933 classes,
1934 deferredOnlyNames,
1935 eagerlyReferencedNames,
1936 );
1937 const removedDecls = ctx.enableHmr
1938 ? new Set<nga.ImportDeclarationMetadata>()
1939 : removeDeferredImports(s, content, importDeclarations, deferredOnlyNames, eagerlyBoundNames);
1940
1941 // Hoist the import statements written after another statement up to `importsEnd`, like ngtsc's
1942 // `ImportManager`, which rebuilds a file it adds imports to as its existing import statements
1943 // (`import` declarations and `import X = …`), the generated imports, then everything else. This
1944 // also keeps CommonJS / Closure downleveling from emitting a `const ... = require(...)` below a
1945 // statement that reads it. ngtsc adds no imports to a file whose classes are all `jit: true`, but
1946 // this emitter does (`getOrCreateNamespace` above), so the gate is a compiled class.
1947 // TODO: as with ngtsc's `isImportStatement`, `import X = Local.Y` moves above a later local
1948 // `namespace` / `enum` / class `Local` and then reads `undefined.Y` at runtime (for an enum, the
1949 // type-checked output also reports TS2450). Skipping aliases of local declarations would fix both.
1950 // https://github.com/angular/angular/blob/73911c8/packages/compiler-cli/src/ngtsc/translator/src/import_manager/import_typescript_transform.ts#L114-L148
1951 const hoistedImports: string[] = [];
1952 if (!filePath.endsWith('.d.ts') && compiledClasses.length > 0) {
1953 const lateImports = [
1954 ...importDeclarations.filter((decl) => !removedDecls.has(decl)),
1955 ...(result.importEqualsDeclarations ?? []),
1956 ]
1957 .filter((decl) => decl.span.start > importsEnd)
1958 .sort((a, b) => a.span.start - b.span.start);

Callers 1

processFileFunction · 0.85

Calls 15

appendCompiledClassFunction · 0.85
removeDeferredImportsFunction · 0.85
mapMethod · 0.80
printStatementMethod · 0.80
getImportStatementsMethod · 0.80
appendRightMethod · 0.80
addMethod · 0.65
hasMethod · 0.65
sliceMethod · 0.65
removeMethod · 0.65
joinMethod · 0.65

Tested by

no test coverage detected