|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google LLC All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.io/license |
| 7 | + */ |
| 8 | + |
| 9 | +import type { ɵParsedMessage as LocalizeMessage } from '@angular/localize'; |
| 10 | +import type { MessageExtractor } from '@angular/localize/tools'; |
| 11 | +import type { BuilderContext, BuilderOutput } from '@angular-devkit/architect'; |
| 12 | +import assert from 'node:assert'; |
| 13 | +import nodePath from 'node:path'; |
| 14 | +import { buildApplicationInternal } from '../application'; |
| 15 | +import type { ApplicationBuilderInternalOptions } from '../application/options'; |
| 16 | +import { buildEsbuildBrowser } from '../browser-esbuild'; |
| 17 | +import type { NormalizedExtractI18nOptions } from './options'; |
| 18 | + |
| 19 | +export async function extractMessages( |
| 20 | + options: NormalizedExtractI18nOptions, |
| 21 | + builderName: string, |
| 22 | + context: BuilderContext, |
| 23 | + extractorConstructor: typeof MessageExtractor, |
| 24 | +): Promise<{ |
| 25 | + builderResult: BuilderOutput; |
| 26 | + basePath: string; |
| 27 | + messages: LocalizeMessage[]; |
| 28 | + useLegacyIds: boolean; |
| 29 | +}> { |
| 30 | + const messages: LocalizeMessage[] = []; |
| 31 | + |
| 32 | + // Setup the build options for the application based on the browserTarget option |
| 33 | + const buildOptions = (await context.validateOptions( |
| 34 | + await context.getTargetOptions(options.browserTarget), |
| 35 | + builderName, |
| 36 | + )) as unknown as ApplicationBuilderInternalOptions; |
| 37 | + buildOptions.optimization = false; |
| 38 | + buildOptions.sourceMap = { scripts: true, vendor: true }; |
| 39 | + |
| 40 | + let build; |
| 41 | + if (builderName === '@angular-devkit/build-angular:application') { |
| 42 | + build = buildApplicationInternal; |
| 43 | + |
| 44 | + buildOptions.ssr = false; |
| 45 | + buildOptions.appShell = false; |
| 46 | + buildOptions.prerender = false; |
| 47 | + } else { |
| 48 | + build = buildEsbuildBrowser; |
| 49 | + } |
| 50 | + |
| 51 | + // Build the application with the build options |
| 52 | + let builderResult; |
| 53 | + try { |
| 54 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 55 | + for await (const result of build(buildOptions as any, context, { write: false })) { |
| 56 | + builderResult = result; |
| 57 | + break; |
| 58 | + } |
| 59 | + |
| 60 | + assert(builderResult !== undefined, 'Application builder did not provide a result.'); |
| 61 | + } catch (err) { |
| 62 | + builderResult = { |
| 63 | + success: false, |
| 64 | + error: (err as Error).message, |
| 65 | + }; |
| 66 | + } |
| 67 | + |
| 68 | + // Extract messages from each output JavaScript file. |
| 69 | + // Output files are only present on a successful build. |
| 70 | + if (builderResult.outputFiles) { |
| 71 | + // Store the JS and JS map files for lookup during extraction |
| 72 | + const files = new Map<string, string>(); |
| 73 | + for (const outputFile of builderResult.outputFiles) { |
| 74 | + if (outputFile.path.endsWith('.js')) { |
| 75 | + files.set(outputFile.path, outputFile.text); |
| 76 | + } else if (outputFile.path.endsWith('.js.map')) { |
| 77 | + files.set(outputFile.path, outputFile.text); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + // Setup the localize message extractor based on the in-memory files |
| 82 | + const extractor = setupLocalizeExtractor(extractorConstructor, files, context); |
| 83 | + |
| 84 | + // Attempt extraction of all output JS files |
| 85 | + for (const filePath of files.keys()) { |
| 86 | + if (!filePath.endsWith('.js')) { |
| 87 | + continue; |
| 88 | + } |
| 89 | + |
| 90 | + const fileMessages = extractor.extractMessages(filePath); |
| 91 | + messages.push(...fileMessages); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + return { |
| 96 | + builderResult, |
| 97 | + basePath: context.workspaceRoot, |
| 98 | + messages, |
| 99 | + // Legacy i18n identifiers are not supported with the new application builder |
| 100 | + useLegacyIds: false, |
| 101 | + }; |
| 102 | +} |
| 103 | + |
| 104 | +function setupLocalizeExtractor( |
| 105 | + extractorConstructor: typeof MessageExtractor, |
| 106 | + files: Map<string, string>, |
| 107 | + context: BuilderContext, |
| 108 | +): MessageExtractor { |
| 109 | + // Setup a virtual file system instance for the extractor |
| 110 | + // * MessageExtractor itself uses readFile, relative and resolve |
| 111 | + // * Internal SourceFileLoader (sourcemap support) uses dirname, exists, readFile, and resolve |
| 112 | + const filesystem = { |
| 113 | + readFile(path: string): string { |
| 114 | + // Output files are stored as relative to the workspace root |
| 115 | + const requestedPath = nodePath.relative(context.workspaceRoot, path); |
| 116 | + |
| 117 | + const content = files.get(requestedPath); |
| 118 | + if (content === undefined) { |
| 119 | + throw new Error('Unknown file requested: ' + requestedPath); |
| 120 | + } |
| 121 | + |
| 122 | + return content; |
| 123 | + }, |
| 124 | + relative(from: string, to: string): string { |
| 125 | + return nodePath.relative(from, to); |
| 126 | + }, |
| 127 | + resolve(...paths: string[]): string { |
| 128 | + return nodePath.resolve(...paths); |
| 129 | + }, |
| 130 | + exists(path: string): boolean { |
| 131 | + // Output files are stored as relative to the workspace root |
| 132 | + const requestedPath = nodePath.relative(context.workspaceRoot, path); |
| 133 | + |
| 134 | + return files.has(requestedPath); |
| 135 | + }, |
| 136 | + dirname(path: string): string { |
| 137 | + return nodePath.dirname(path); |
| 138 | + }, |
| 139 | + }; |
| 140 | + |
| 141 | + const logger = { |
| 142 | + // level 2 is warnings |
| 143 | + level: 2, |
| 144 | + debug(...args: string[]): void { |
| 145 | + // eslint-disable-next-line no-console |
| 146 | + console.debug(...args); |
| 147 | + }, |
| 148 | + info(...args: string[]): void { |
| 149 | + context.logger.info(args.join('')); |
| 150 | + }, |
| 151 | + warn(...args: string[]): void { |
| 152 | + context.logger.warn(args.join('')); |
| 153 | + }, |
| 154 | + error(...args: string[]): void { |
| 155 | + context.logger.error(args.join('')); |
| 156 | + }, |
| 157 | + }; |
| 158 | + |
| 159 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 160 | + const extractor = new extractorConstructor(filesystem as any, logger, { |
| 161 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 162 | + basePath: context.workspaceRoot as any, |
| 163 | + useSourceMaps: true, |
| 164 | + }); |
| 165 | + |
| 166 | + return extractor; |
| 167 | +} |
0 commit comments