|
| 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 { BuilderContext } from '@angular-devkit/architect'; |
| 10 | +import { join } from 'node:path'; |
| 11 | +import { InitialFileRecord } from '../../tools/esbuild/bundler-context'; |
| 12 | +import { ExecutionResult } from '../../tools/esbuild/bundler-execution-result'; |
| 13 | +import { I18nInliner } from '../../tools/esbuild/i18n-inliner'; |
| 14 | +import { generateIndexHtml } from '../../tools/esbuild/index-html-generator'; |
| 15 | +import { createOutputFileFromText } from '../../tools/esbuild/utils'; |
| 16 | +import { maxWorkers } from '../../utils/environment-options'; |
| 17 | +import { loadTranslations } from '../../utils/i18n-options'; |
| 18 | +import { createTranslationLoader } from '../../utils/load-translations'; |
| 19 | +import { urlJoin } from '../../utils/url'; |
| 20 | +import { NormalizedApplicationBuildOptions } from './options'; |
| 21 | + |
| 22 | +/** |
| 23 | + * Inlines all active locales as specified by the application build options into all |
| 24 | + * application JavaScript files created during the build. |
| 25 | + * @param options The normalized application builder options used to create the build. |
| 26 | + * @param executionResult The result of an executed build. |
| 27 | + * @param initialFiles A map containing initial file information for the executed build. |
| 28 | + */ |
| 29 | +export async function inlineI18n( |
| 30 | + options: NormalizedApplicationBuildOptions, |
| 31 | + executionResult: ExecutionResult, |
| 32 | + initialFiles: Map<string, InitialFileRecord>, |
| 33 | +): Promise<void> { |
| 34 | + // Create the multi-threaded inliner with common options and the files generated from the build. |
| 35 | + const inliner = new I18nInliner( |
| 36 | + { |
| 37 | + missingTranslation: options.i18nOptions.missingTranslationBehavior ?? 'warning', |
| 38 | + outputFiles: executionResult.outputFiles, |
| 39 | + shouldOptimize: options.optimizationOptions.scripts, |
| 40 | + }, |
| 41 | + maxWorkers, |
| 42 | + ); |
| 43 | + |
| 44 | + // For each active locale, use the inliner to process the output files of the build. |
| 45 | + const updatedOutputFiles = []; |
| 46 | + const updatedAssetFiles = []; |
| 47 | + try { |
| 48 | + for (const locale of options.i18nOptions.inlineLocales) { |
| 49 | + // A locale specific set of files is returned from the inliner. |
| 50 | + const localeOutputFiles = await inliner.inlineForLocale( |
| 51 | + locale, |
| 52 | + options.i18nOptions.locales[locale].translation, |
| 53 | + ); |
| 54 | + |
| 55 | + // Generate locale specific index HTML files |
| 56 | + if (options.indexHtmlOptions) { |
| 57 | + const { content, errors, warnings } = await generateIndexHtml( |
| 58 | + initialFiles, |
| 59 | + localeOutputFiles, |
| 60 | + { |
| 61 | + ...options, |
| 62 | + baseHref: |
| 63 | + getLocaleBaseHref(options.baseHref, options.i18nOptions, locale) ?? options.baseHref, |
| 64 | + }, |
| 65 | + locale, |
| 66 | + ); |
| 67 | + |
| 68 | + localeOutputFiles.push(createOutputFileFromText(options.indexHtmlOptions.output, content)); |
| 69 | + } |
| 70 | + |
| 71 | + // Update directory with locale base |
| 72 | + if (options.i18nOptions.flatOutput !== true) { |
| 73 | + localeOutputFiles.forEach((file) => { |
| 74 | + file.path = join(locale, file.path); |
| 75 | + }); |
| 76 | + |
| 77 | + for (const assetFile of executionResult.assetFiles) { |
| 78 | + updatedAssetFiles.push({ |
| 79 | + source: assetFile.source, |
| 80 | + destination: join(locale, assetFile.destination), |
| 81 | + }); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + updatedOutputFiles.push(...localeOutputFiles); |
| 86 | + } |
| 87 | + } finally { |
| 88 | + await inliner.close(); |
| 89 | + } |
| 90 | + |
| 91 | + // Update the result with all localized files |
| 92 | + executionResult.outputFiles = updatedOutputFiles; |
| 93 | + |
| 94 | + // Assets are only changed if not using the flat output option |
| 95 | + if (options.i18nOptions.flatOutput !== true) { |
| 96 | + executionResult.assetFiles = updatedAssetFiles; |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +function getLocaleBaseHref( |
| 101 | + baseHref: string | undefined, |
| 102 | + i18n: NormalizedApplicationBuildOptions['i18nOptions'], |
| 103 | + locale: string, |
| 104 | +): string | undefined { |
| 105 | + if (i18n.flatOutput) { |
| 106 | + return undefined; |
| 107 | + } |
| 108 | + |
| 109 | + if (i18n.locales[locale] && i18n.locales[locale].baseHref !== '') { |
| 110 | + return urlJoin(baseHref || '', i18n.locales[locale].baseHref ?? `/${locale}/`); |
| 111 | + } |
| 112 | + |
| 113 | + return undefined; |
| 114 | +} |
| 115 | + |
| 116 | +/** |
| 117 | + * Loads all active translations using the translation loaders from the `@angular/localize` package. |
| 118 | + * @param context The architect builder context for the current build. |
| 119 | + * @param i18n The normalized i18n options to use. |
| 120 | + */ |
| 121 | +export async function loadActiveTranslations( |
| 122 | + context: BuilderContext, |
| 123 | + i18n: NormalizedApplicationBuildOptions['i18nOptions'], |
| 124 | +) { |
| 125 | + // Load locale data and translations (if present) |
| 126 | + let loader; |
| 127 | + for (const [locale, desc] of Object.entries(i18n.locales)) { |
| 128 | + if (!i18n.inlineLocales.has(locale) && locale !== i18n.sourceLocale) { |
| 129 | + continue; |
| 130 | + } |
| 131 | + |
| 132 | + if (!desc.files.length) { |
| 133 | + continue; |
| 134 | + } |
| 135 | + |
| 136 | + loader ??= await createTranslationLoader(); |
| 137 | + |
| 138 | + loadTranslations( |
| 139 | + locale, |
| 140 | + desc, |
| 141 | + context.workspaceRoot, |
| 142 | + loader, |
| 143 | + { |
| 144 | + warn(message) { |
| 145 | + context.logger.warn(message); |
| 146 | + }, |
| 147 | + error(message) { |
| 148 | + throw new Error(message); |
| 149 | + }, |
| 150 | + }, |
| 151 | + undefined, |
| 152 | + i18n.duplicateTranslationBehavior, |
| 153 | + ); |
| 154 | + } |
| 155 | +} |
0 commit comments