From 4538bd6c96923db6e7c6c429767a499865d97436 Mon Sep 17 00:00:00 2001 From: Alex Rickabaugh Date: Mon, 7 Jun 2021 11:58:30 -0700 Subject: [PATCH] refactor(compiler-cli): extract xi18n utility functions to a separate file (#42485) This commit moves some xi18n-related functions in the View Engine ng.Program into a new file. This is necessary in order to depend on them from the Ivy ng.Program while avoiding a cycle. PR Close #42485 --- .../compiler-cli/src/transformers/i18n.ts | 74 +++++++++++++++++++ .../compiler-cli/src/transformers/program.ts | 63 +--------------- 2 files changed, 76 insertions(+), 61 deletions(-) create mode 100644 packages/compiler-cli/src/transformers/i18n.ts diff --git a/packages/compiler-cli/src/transformers/i18n.ts b/packages/compiler-cli/src/transformers/i18n.ts new file mode 100644 index 0000000000..f2db15a0e8 --- /dev/null +++ b/packages/compiler-cli/src/transformers/i18n.ts @@ -0,0 +1,74 @@ +/** + * @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 {MessageBundle, Serializer, Xliff, Xliff2, Xmb} from '@angular/compiler'; +import * as path from 'path'; +import * as ts from 'typescript'; + +import {CompilerOptions} from './api'; + +export function i18nGetExtension(formatName: string): string { + const format = formatName.toLowerCase(); + + switch (format) { + case 'xmb': + return 'xmb'; + case 'xlf': + case 'xlif': + case 'xliff': + case 'xlf2': + case 'xliff2': + return 'xlf'; + } + + throw new Error(`Unsupported format "${formatName}"`); +} + +export function i18nExtract( + formatName: string|null, outFile: string|null, host: ts.CompilerHost, options: CompilerOptions, + bundle: MessageBundle, + pathResolve: (...segments: string[]) => string = path.resolve): string[] { + formatName = formatName || 'xlf'; + // Checks the format and returns the extension + const ext = i18nGetExtension(formatName); + const content = i18nSerialize(bundle, formatName, options); + const dstFile = outFile || `messages.${ext}`; + const dstPath = pathResolve(options.outDir || options.basePath!, dstFile); + host.writeFile(dstPath, content, false, undefined, []); + return [dstPath]; +} + +export function i18nSerialize( + bundle: MessageBundle, formatName: string, options: CompilerOptions): string { + const format = formatName.toLowerCase(); + let serializer: Serializer; + + switch (format) { + case 'xmb': + serializer = new Xmb(); + break; + case 'xliff2': + case 'xlf2': + serializer = new Xliff2(); + break; + case 'xlf': + case 'xliff': + default: + serializer = new Xliff(); + } + + return bundle.write(serializer, getPathNormalizer(options.basePath)); +} + +function getPathNormalizer(basePath?: string) { + // normalize source paths by removing the base path and always using "/" as a separator + return (sourcePath: string) => { + sourcePath = basePath ? path.relative(basePath, sourcePath) : sourcePath; + return sourcePath.split(path.sep).join('/'); + }; +} diff --git a/packages/compiler-cli/src/transformers/program.ts b/packages/compiler-cli/src/transformers/program.ts index c048865101..9e32a2a905 100644 --- a/packages/compiler-cli/src/transformers/program.ts +++ b/packages/compiler-cli/src/transformers/program.ts @@ -7,7 +7,7 @@ * found in the LICENSE file at https://angular.io/license */ -import {AotCompiler, AotCompilerOptions, core, createAotCompiler, FormattedMessageChain, GeneratedFile, getMissingNgModuleMetadataErrorData, getParseErrors, isFormattedError, isSyntaxError, MessageBundle, NgAnalyzedFileWithInjectables, NgAnalyzedModules, ParseSourceSpan, PartialModule, Serializer, Xliff, Xliff2, Xmb} from '@angular/compiler'; +import {AotCompiler, AotCompilerOptions, core, createAotCompiler, FormattedMessageChain, GeneratedFile, getMissingNgModuleMetadataErrorData, getParseErrors, isFormattedError, isSyntaxError, MessageBundle, NgAnalyzedFileWithInjectables, NgAnalyzedModules, ParseSourceSpan, PartialModule} from '@angular/compiler'; import * as fs from 'fs'; import * as path from 'path'; import * as ts from 'typescript'; @@ -22,6 +22,7 @@ import {verifySupportedTypeScriptVersion} from '../typescript_support'; import {CompilerHost, CompilerOptions, CustomTransformers, DEFAULT_ERROR_CODE, Diagnostic, DiagnosticMessageChain, EmitFlags, LazyRoute, LibrarySummary, Program, SOURCE, TsEmitCallback, TsMergeEmitResultsCallback} from './api'; import {CodeGenerator, getOriginalReferences, TsCompilerAotCompilerTypeCheckHostAdapter} from './compiler_host'; import {getDownlevelDecoratorsTransform} from './downlevel_decorators_transform'; +import {i18nExtract} from './i18n'; import {getInlineResourcesTransformFactory, InlineResourcesMetadataTransformer} from './inline_resources'; import {getExpressionLoweringTransformFactory, LowerMetadataTransform} from './lower_expressions'; import {MetadataCache, MetadataTransformer} from './metadata_cache'; @@ -938,66 +939,6 @@ export function createSrcToOutPathMapper( } } -export function i18nExtract( - formatName: string|null, outFile: string|null, host: ts.CompilerHost, options: CompilerOptions, - bundle: MessageBundle): string[] { - formatName = formatName || 'xlf'; - // Checks the format and returns the extension - const ext = i18nGetExtension(formatName); - const content = i18nSerialize(bundle, formatName, options); - const dstFile = outFile || `messages.${ext}`; - const dstPath = path.resolve(options.outDir || options.basePath!, dstFile); - host.writeFile(dstPath, content, false, undefined, []); - return [dstPath]; -} - -export function i18nSerialize( - bundle: MessageBundle, formatName: string, options: CompilerOptions): string { - const format = formatName.toLowerCase(); - let serializer: Serializer; - - switch (format) { - case 'xmb': - serializer = new Xmb(); - break; - case 'xliff2': - case 'xlf2': - serializer = new Xliff2(); - break; - case 'xlf': - case 'xliff': - default: - serializer = new Xliff(); - } - - return bundle.write(serializer, getPathNormalizer(options.basePath)); -} - -function getPathNormalizer(basePath?: string) { - // normalize source paths by removing the base path and always using "/" as a separator - return (sourcePath: string) => { - sourcePath = basePath ? path.relative(basePath, sourcePath) : sourcePath; - return sourcePath.split(path.sep).join('/'); - }; -} - -export function i18nGetExtension(formatName: string): string { - const format = formatName.toLowerCase(); - - switch (format) { - case 'xmb': - return 'xmb'; - case 'xlf': - case 'xlif': - case 'xliff': - case 'xlf2': - case 'xliff2': - return 'xlf'; - } - - throw new Error(`Unsupported format "${formatName}"`); -} - function mergeEmitResults(emitResults: ts.EmitResult[]): ts.EmitResult { const diagnostics: ts.Diagnostic[] = []; let emitSkipped = false;