[6a3a178] | 1 | "use strict";
|
---|
| 2 | /**
|
---|
| 3 | * @license
|
---|
| 4 | * Copyright Google LLC All Rights Reserved.
|
---|
| 5 | *
|
---|
| 6 | * Use of this source code is governed by an MIT-style license that can be
|
---|
| 7 | * found in the LICENSE file at https://angular.io/license
|
---|
| 8 | */
|
---|
| 9 | Object.defineProperty(exports, "__esModule", { value: true });
|
---|
| 10 | exports.updateI18nConfig = void 0;
|
---|
| 11 | const path_1 = require("path");
|
---|
| 12 | const dependencies_1 = require("../../utility/dependencies");
|
---|
| 13 | const latest_versions_1 = require("../../utility/latest-versions");
|
---|
| 14 | const workspace_1 = require("../../utility/workspace");
|
---|
| 15 | const workspace_models_1 = require("../../utility/workspace-models");
|
---|
| 16 | function updateI18nConfig() {
|
---|
| 17 | return (tree, { logger }) => workspace_1.updateWorkspace((workspace) => {
|
---|
| 18 | // Process extraction targets first since they use browser option values
|
---|
| 19 | for (const [, target, , project] of workspace_1.allWorkspaceTargets(workspace)) {
|
---|
| 20 | switch (target.builder) {
|
---|
| 21 | case workspace_models_1.Builders.ExtractI18n:
|
---|
| 22 | addProjectI18NOptions(tree, target, project);
|
---|
| 23 | removeExtracti18nDeprecatedOptions(target);
|
---|
| 24 | break;
|
---|
| 25 | }
|
---|
| 26 | }
|
---|
| 27 | for (const [, target] of workspace_1.allWorkspaceTargets(workspace)) {
|
---|
| 28 | switch (target.builder) {
|
---|
| 29 | case workspace_models_1.Builders.Browser:
|
---|
| 30 | case workspace_models_1.Builders.Server:
|
---|
| 31 | updateBaseHrefs(target);
|
---|
| 32 | removeFormatOption(target);
|
---|
| 33 | addBuilderI18NOptions(target, logger);
|
---|
| 34 | break;
|
---|
| 35 | }
|
---|
| 36 | }
|
---|
| 37 | });
|
---|
| 38 | }
|
---|
| 39 | exports.updateI18nConfig = updateI18nConfig;
|
---|
| 40 | function addProjectI18NOptions(tree, builderConfig, projectConfig) {
|
---|
| 41 | const browserConfig = projectConfig.targets.get('build');
|
---|
| 42 | if (!browserConfig || browserConfig.builder !== workspace_models_1.Builders.Browser) {
|
---|
| 43 | return;
|
---|
| 44 | }
|
---|
| 45 | // browser builder options
|
---|
| 46 | let locales;
|
---|
| 47 | for (const [, options] of workspace_1.allTargetOptions(browserConfig)) {
|
---|
| 48 | const localeId = options.i18nLocale;
|
---|
| 49 | if (typeof localeId !== 'string') {
|
---|
| 50 | continue;
|
---|
| 51 | }
|
---|
| 52 | const localeFile = options.i18nFile;
|
---|
| 53 | if (typeof localeFile !== 'string') {
|
---|
| 54 | continue;
|
---|
| 55 | }
|
---|
| 56 | let baseHref = options.baseHref;
|
---|
| 57 | if (typeof baseHref === 'string') {
|
---|
| 58 | // If the configuration baseHref is already the default locale value, do not include it
|
---|
| 59 | if (baseHref === `/${localeId}/`) {
|
---|
| 60 | baseHref = undefined;
|
---|
| 61 | }
|
---|
| 62 | }
|
---|
| 63 | else {
|
---|
| 64 | // If the configuration does not contain a baseHref, ensure the main option value is used.
|
---|
| 65 | baseHref = '';
|
---|
| 66 | }
|
---|
| 67 | if (!locales) {
|
---|
| 68 | locales = {
|
---|
| 69 | [localeId]: baseHref === undefined
|
---|
| 70 | ? localeFile
|
---|
| 71 | : {
|
---|
| 72 | translation: localeFile,
|
---|
| 73 | baseHref,
|
---|
| 74 | },
|
---|
| 75 | };
|
---|
| 76 | }
|
---|
| 77 | else {
|
---|
| 78 | locales[localeId] =
|
---|
| 79 | baseHref === undefined
|
---|
| 80 | ? localeFile
|
---|
| 81 | : {
|
---|
| 82 | translation: localeFile,
|
---|
| 83 | baseHref,
|
---|
| 84 | };
|
---|
| 85 | }
|
---|
| 86 | }
|
---|
| 87 | if (locales) {
|
---|
| 88 | // Get sourceLocale from extract-i18n builder
|
---|
| 89 | const i18nOptions = [...workspace_1.allTargetOptions(builderConfig)];
|
---|
| 90 | const sourceLocale = i18nOptions
|
---|
| 91 | .map(([, o]) => o.i18nLocale)
|
---|
| 92 | .find((x) => !!x && typeof x === 'string');
|
---|
| 93 | projectConfig.extensions['i18n'] = {
|
---|
| 94 | locales,
|
---|
| 95 | ...(sourceLocale ? { sourceLocale } : {}),
|
---|
| 96 | };
|
---|
| 97 | // Add @angular/localize if not already a dependency
|
---|
| 98 | if (!dependencies_1.getPackageJsonDependency(tree, '@angular/localize')) {
|
---|
| 99 | dependencies_1.addPackageJsonDependency(tree, {
|
---|
| 100 | name: '@angular/localize',
|
---|
| 101 | version: latest_versions_1.latestVersions.Angular,
|
---|
| 102 | type: dependencies_1.NodeDependencyType.Default,
|
---|
| 103 | });
|
---|
| 104 | }
|
---|
| 105 | }
|
---|
| 106 | }
|
---|
| 107 | function addBuilderI18NOptions(builderConfig, logger) {
|
---|
| 108 | for (const [, options] of workspace_1.allTargetOptions(builderConfig)) {
|
---|
| 109 | const localeId = options.i18nLocale;
|
---|
| 110 | const i18nFile = options.i18nFile;
|
---|
| 111 | const outputPath = options.outputPath;
|
---|
| 112 | if (typeof localeId === 'string' && i18nFile && typeof outputPath === 'string') {
|
---|
| 113 | if (outputPath.match(new RegExp(`[/\\\\]${localeId}[/\\\\]?$`))) {
|
---|
| 114 | const newOutputPath = outputPath.replace(new RegExp(`[/\\\\]${localeId}[/\\\\]?$`), '');
|
---|
| 115 | options.outputPath = newOutputPath;
|
---|
| 116 | }
|
---|
| 117 | else {
|
---|
| 118 | logger.warn(`Output path value "${outputPath}" for locale "${localeId}" is not supported with the new localization system. ` +
|
---|
| 119 | `With the current value, the localized output would be written to "${path_1.posix.join(outputPath, localeId)}". ` +
|
---|
| 120 | `Keeping existing options for the target configuration of locale "${localeId}".`);
|
---|
| 121 | continue;
|
---|
| 122 | }
|
---|
| 123 | }
|
---|
| 124 | if (typeof localeId === 'string') {
|
---|
| 125 | // add new localize option
|
---|
| 126 | options.localize = [localeId];
|
---|
| 127 | delete options.i18nLocale;
|
---|
| 128 | }
|
---|
| 129 | if (i18nFile !== undefined) {
|
---|
| 130 | delete options.i18nFile;
|
---|
| 131 | }
|
---|
| 132 | }
|
---|
| 133 | }
|
---|
| 134 | function removeFormatOption(builderConfig) {
|
---|
| 135 | for (const [, options] of workspace_1.allTargetOptions(builderConfig)) {
|
---|
| 136 | // The format is always auto-detected now
|
---|
| 137 | delete options.i18nFormat;
|
---|
| 138 | }
|
---|
| 139 | }
|
---|
| 140 | function updateBaseHrefs(builderConfig) {
|
---|
| 141 | var _a;
|
---|
| 142 | const mainBaseHref = (_a = builderConfig.options) === null || _a === void 0 ? void 0 : _a.baseHref;
|
---|
| 143 | const hasMainBaseHref = !!mainBaseHref && typeof mainBaseHref === 'string' && mainBaseHref !== '/';
|
---|
| 144 | for (const [, options] of workspace_1.allTargetOptions(builderConfig)) {
|
---|
| 145 | const localeId = options.i18nLocale;
|
---|
| 146 | const i18nFile = options.i18nFile;
|
---|
| 147 | // localize base HREF values are controlled by the i18n configuration
|
---|
| 148 | const baseHref = options.baseHref;
|
---|
| 149 | if (localeId !== undefined && i18nFile !== undefined && baseHref !== undefined) {
|
---|
| 150 | // if the main option set has a non-default base href,
|
---|
| 151 | // ensure that the augmented base href has the correct base value
|
---|
| 152 | if (hasMainBaseHref) {
|
---|
| 153 | options.baseHref = '/';
|
---|
| 154 | }
|
---|
| 155 | else {
|
---|
| 156 | delete options.baseHref;
|
---|
| 157 | }
|
---|
| 158 | }
|
---|
| 159 | }
|
---|
| 160 | }
|
---|
| 161 | function removeExtracti18nDeprecatedOptions(builderConfig) {
|
---|
| 162 | for (const [, options] of workspace_1.allTargetOptions(builderConfig)) {
|
---|
| 163 | // deprecated options
|
---|
| 164 | delete options.i18nLocale;
|
---|
| 165 | if (options.i18nFormat !== undefined) {
|
---|
| 166 | // i18nFormat has been changed to format
|
---|
| 167 | options.format = options.i18nFormat;
|
---|
| 168 | delete options.i18nFormat;
|
---|
| 169 | }
|
---|
| 170 | }
|
---|
| 171 | }
|
---|