[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 | var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
---|
| 10 | if (k2 === undefined) k2 = k;
|
---|
| 11 | Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
---|
| 12 | }) : (function(o, m, k, k2) {
|
---|
| 13 | if (k2 === undefined) k2 = k;
|
---|
| 14 | o[k2] = m[k];
|
---|
| 15 | }));
|
---|
| 16 | var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
---|
| 17 | Object.defineProperty(o, "default", { enumerable: true, value: v });
|
---|
| 18 | }) : function(o, v) {
|
---|
| 19 | o["default"] = v;
|
---|
| 20 | });
|
---|
| 21 | var __importStar = (this && this.__importStar) || function (mod) {
|
---|
| 22 | if (mod && mod.__esModule) return mod;
|
---|
| 23 | var result = {};
|
---|
| 24 | if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
---|
| 25 | __setModuleDefault(result, mod);
|
---|
| 26 | return result;
|
---|
| 27 | };
|
---|
| 28 | var __importDefault = (this && this.__importDefault) || function (mod) {
|
---|
| 29 | return (mod && mod.__esModule) ? mod : { "default": mod };
|
---|
| 30 | };
|
---|
| 31 | Object.defineProperty(exports, "__esModule", { value: true });
|
---|
| 32 | exports.execute = void 0;
|
---|
| 33 | const architect_1 = require("@angular-devkit/architect");
|
---|
| 34 | const build_webpack_1 = require("@angular-devkit/build-webpack");
|
---|
| 35 | const fs = __importStar(require("fs"));
|
---|
| 36 | const path = __importStar(require("path"));
|
---|
| 37 | const webpack_1 = __importDefault(require("webpack"));
|
---|
| 38 | const schema_1 = require("../browser/schema");
|
---|
| 39 | const i18n_options_1 = require("../utils/i18n-options");
|
---|
| 40 | const version_1 = require("../utils/version");
|
---|
| 41 | const webpack_browser_config_1 = require("../utils/webpack-browser-config");
|
---|
| 42 | const configs_1 = require("../webpack/configs");
|
---|
| 43 | const stats_1 = require("../webpack/utils/stats");
|
---|
| 44 | const schema_2 = require("./schema");
|
---|
| 45 | function getI18nOutfile(format) {
|
---|
| 46 | switch (format) {
|
---|
| 47 | case 'xmb':
|
---|
| 48 | return 'messages.xmb';
|
---|
| 49 | case 'xlf':
|
---|
| 50 | case 'xlif':
|
---|
| 51 | case 'xliff':
|
---|
| 52 | case 'xlf2':
|
---|
| 53 | case 'xliff2':
|
---|
| 54 | return 'messages.xlf';
|
---|
| 55 | case 'json':
|
---|
| 56 | case 'legacy-migrate':
|
---|
| 57 | return 'messages.json';
|
---|
| 58 | case 'arb':
|
---|
| 59 | return 'messages.arb';
|
---|
| 60 | default:
|
---|
| 61 | throw new Error(`Unsupported format "${format}"`);
|
---|
| 62 | }
|
---|
| 63 | }
|
---|
| 64 | async function getSerializer(format, sourceLocale, basePath, useLegacyIds, diagnostics) {
|
---|
| 65 | switch (format) {
|
---|
| 66 | case schema_2.Format.Xmb:
|
---|
| 67 | const { XmbTranslationSerializer } = await Promise.resolve().then(() => __importStar(require('@angular/localize/src/tools/src/extract/translation_files/xmb_translation_serializer')));
|
---|
| 68 | // eslint-disable-next-line @typescript-eslint/no-explicit-any
|
---|
| 69 | return new XmbTranslationSerializer(basePath, useLegacyIds);
|
---|
| 70 | case schema_2.Format.Xlf:
|
---|
| 71 | case schema_2.Format.Xlif:
|
---|
| 72 | case schema_2.Format.Xliff:
|
---|
| 73 | const { Xliff1TranslationSerializer } = await Promise.resolve().then(() => __importStar(require('@angular/localize/src/tools/src/extract/translation_files/xliff1_translation_serializer')));
|
---|
| 74 | // eslint-disable-next-line @typescript-eslint/no-explicit-any
|
---|
| 75 | return new Xliff1TranslationSerializer(sourceLocale, basePath, useLegacyIds, {});
|
---|
| 76 | case schema_2.Format.Xlf2:
|
---|
| 77 | case schema_2.Format.Xliff2:
|
---|
| 78 | const { Xliff2TranslationSerializer } = await Promise.resolve().then(() => __importStar(require('@angular/localize/src/tools/src/extract/translation_files/xliff2_translation_serializer')));
|
---|
| 79 | // eslint-disable-next-line @typescript-eslint/no-explicit-any
|
---|
| 80 | return new Xliff2TranslationSerializer(sourceLocale, basePath, useLegacyIds, {});
|
---|
| 81 | case schema_2.Format.Json:
|
---|
| 82 | const { SimpleJsonTranslationSerializer } = await Promise.resolve().then(() => __importStar(require('@angular/localize/src/tools/src/extract/translation_files/json_translation_serializer')));
|
---|
| 83 | return new SimpleJsonTranslationSerializer(sourceLocale);
|
---|
| 84 | case schema_2.Format.LegacyMigrate:
|
---|
| 85 | const { LegacyMessageIdMigrationSerializer } = await Promise.resolve().then(() => __importStar(require('@angular/localize/src/tools/src/extract/translation_files/legacy_message_id_migration_serializer')));
|
---|
| 86 | return new LegacyMessageIdMigrationSerializer(diagnostics);
|
---|
| 87 | case schema_2.Format.Arb:
|
---|
| 88 | const { ArbTranslationSerializer } = await Promise.resolve().then(() => __importStar(require('@angular/localize/src/tools/src/extract/translation_files/arb_translation_serializer')));
|
---|
| 89 | const fileSystem = {
|
---|
| 90 | relative(from, to) {
|
---|
| 91 | return path.relative(from, to);
|
---|
| 92 | },
|
---|
| 93 | };
|
---|
| 94 | // eslint-disable-next-line @typescript-eslint/no-explicit-any
|
---|
| 95 | return new ArbTranslationSerializer(sourceLocale, basePath, fileSystem);
|
---|
| 96 | }
|
---|
| 97 | }
|
---|
| 98 | function normalizeFormatOption(options) {
|
---|
| 99 | let format = options.format;
|
---|
| 100 | switch (format) {
|
---|
| 101 | case schema_2.Format.Xlf:
|
---|
| 102 | case schema_2.Format.Xlif:
|
---|
| 103 | case schema_2.Format.Xliff:
|
---|
| 104 | format = schema_2.Format.Xlf;
|
---|
| 105 | break;
|
---|
| 106 | case schema_2.Format.Xlf2:
|
---|
| 107 | case schema_2.Format.Xliff2:
|
---|
| 108 | format = schema_2.Format.Xlf2;
|
---|
| 109 | break;
|
---|
| 110 | }
|
---|
| 111 | // Default format is xliff1
|
---|
| 112 | return format !== null && format !== void 0 ? format : schema_2.Format.Xlf;
|
---|
| 113 | }
|
---|
| 114 | class NoEmitPlugin {
|
---|
| 115 | apply(compiler) {
|
---|
| 116 | compiler.hooks.shouldEmit.tap('angular-no-emit', () => false);
|
---|
| 117 | }
|
---|
| 118 | }
|
---|
| 119 | /**
|
---|
| 120 | * @experimental Direct usage of this function is considered experimental.
|
---|
| 121 | */
|
---|
| 122 | async function execute(options, context, transforms) {
|
---|
| 123 | var _a;
|
---|
| 124 | // Check Angular version.
|
---|
| 125 | version_1.assertCompatibleAngularVersion(context.workspaceRoot);
|
---|
| 126 | const browserTarget = architect_1.targetFromTargetString(options.browserTarget);
|
---|
| 127 | const browserOptions = await context.validateOptions(await context.getTargetOptions(browserTarget), await context.getBuilderNameForTarget(browserTarget));
|
---|
| 128 | const format = normalizeFormatOption(options);
|
---|
| 129 | // We need to determine the outFile name so that AngularCompiler can retrieve it.
|
---|
| 130 | let outFile = options.outFile || getI18nOutfile(format);
|
---|
| 131 | if (options.outputPath) {
|
---|
| 132 | // AngularCompilerPlugin doesn't support genDir so we have to adjust outFile instead.
|
---|
| 133 | outFile = path.join(options.outputPath, outFile);
|
---|
| 134 | }
|
---|
| 135 | outFile = path.resolve(context.workspaceRoot, outFile);
|
---|
| 136 | if (!context.target || !context.target.project) {
|
---|
| 137 | throw new Error('The builder requires a target.');
|
---|
| 138 | }
|
---|
| 139 | const metadata = await context.getProjectMetadata(context.target);
|
---|
| 140 | const i18n = i18n_options_1.createI18nOptions(metadata);
|
---|
| 141 | let useLegacyIds = true;
|
---|
| 142 | const ivyMessages = [];
|
---|
| 143 | const builderOptions = {
|
---|
| 144 | ...browserOptions,
|
---|
| 145 | optimization: false,
|
---|
| 146 | sourceMap: {
|
---|
| 147 | scripts: true,
|
---|
| 148 | styles: false,
|
---|
| 149 | vendor: true,
|
---|
| 150 | },
|
---|
| 151 | buildOptimizer: false,
|
---|
| 152 | aot: true,
|
---|
| 153 | progress: options.progress,
|
---|
| 154 | budgets: [],
|
---|
| 155 | assets: [],
|
---|
| 156 | scripts: [],
|
---|
| 157 | styles: [],
|
---|
| 158 | deleteOutputPath: false,
|
---|
| 159 | extractLicenses: false,
|
---|
| 160 | subresourceIntegrity: false,
|
---|
| 161 | outputHashing: schema_1.OutputHashing.None,
|
---|
| 162 | namedChunks: true,
|
---|
| 163 | };
|
---|
| 164 | const { config, projectRoot } = await webpack_browser_config_1.generateBrowserWebpackConfigFromContext(builderOptions, context, (wco) => {
|
---|
| 165 | var _a;
|
---|
| 166 | if (wco.tsConfig.options.enableIvy === false) {
|
---|
| 167 | context.logger.warn('Ivy extraction enabled but application is not Ivy enabled. Extraction may fail.');
|
---|
| 168 | }
|
---|
| 169 | // Default value for legacy message ids is currently true
|
---|
| 170 | useLegacyIds = (_a = wco.tsConfig.options.enableI18nLegacyMessageIdFormat) !== null && _a !== void 0 ? _a : true;
|
---|
| 171 | const partials = [
|
---|
| 172 | { plugins: [new NoEmitPlugin()] },
|
---|
| 173 | configs_1.getCommonConfig(wco),
|
---|
| 174 | configs_1.getBrowserConfig(wco),
|
---|
| 175 | configs_1.getTypeScriptConfig(wco),
|
---|
| 176 | configs_1.getWorkerConfig(wco),
|
---|
| 177 | configs_1.getStatsConfig(wco),
|
---|
| 178 | ];
|
---|
| 179 | // Add Ivy application file extractor support
|
---|
| 180 | partials.unshift({
|
---|
| 181 | module: {
|
---|
| 182 | rules: [
|
---|
| 183 | {
|
---|
| 184 | test: /\.[t|j]s$/,
|
---|
| 185 | loader: require.resolve('./ivy-extract-loader'),
|
---|
| 186 | options: {
|
---|
| 187 | messageHandler: (messages) => ivyMessages.push(...messages),
|
---|
| 188 | },
|
---|
| 189 | },
|
---|
| 190 | ],
|
---|
| 191 | },
|
---|
| 192 | });
|
---|
| 193 | // Replace all stylesheets with empty content
|
---|
| 194 | partials.push({
|
---|
| 195 | module: {
|
---|
| 196 | rules: [
|
---|
| 197 | {
|
---|
| 198 | test: /\.(css|scss|sass|styl|less)$/,
|
---|
| 199 | loader: require.resolve('./empty-loader'),
|
---|
| 200 | },
|
---|
| 201 | ],
|
---|
| 202 | },
|
---|
| 203 | });
|
---|
| 204 | return partials;
|
---|
| 205 | });
|
---|
| 206 | try {
|
---|
| 207 | require.resolve('@angular/localize');
|
---|
| 208 | }
|
---|
| 209 | catch {
|
---|
| 210 | return {
|
---|
| 211 | success: false,
|
---|
| 212 | error: `Ivy extraction requires the '@angular/localize' package.`,
|
---|
| 213 | outputPath: outFile,
|
---|
| 214 | };
|
---|
| 215 | }
|
---|
| 216 | const webpackResult = await build_webpack_1.runWebpack((await ((_a = transforms === null || transforms === void 0 ? void 0 : transforms.webpackConfiguration) === null || _a === void 0 ? void 0 : _a.call(transforms, config))) || config, context, {
|
---|
| 217 | logging: stats_1.createWebpackLoggingCallback(builderOptions, context.logger),
|
---|
| 218 | webpackFactory: webpack_1.default,
|
---|
| 219 | }).toPromise();
|
---|
| 220 | // Set the outputPath to the extraction output location for downstream consumers
|
---|
| 221 | webpackResult.outputPath = outFile;
|
---|
| 222 | // Complete if Webpack build failed
|
---|
| 223 | if (!webpackResult.success) {
|
---|
| 224 | return webpackResult;
|
---|
| 225 | }
|
---|
| 226 | const basePath = config.context || projectRoot;
|
---|
| 227 | const { checkDuplicateMessages } = await Promise.resolve().then(() => __importStar(require('@angular/localize/src/tools/src/extract/duplicates')));
|
---|
| 228 | // The filesystem is used to create a relative path for each file
|
---|
| 229 | // from the basePath. This relative path is then used in the error message.
|
---|
| 230 | const checkFileSystem = {
|
---|
| 231 | relative(from, to) {
|
---|
| 232 | return path.relative(from, to);
|
---|
| 233 | },
|
---|
| 234 | };
|
---|
| 235 | const diagnostics = checkDuplicateMessages(
|
---|
| 236 | // eslint-disable-next-line @typescript-eslint/no-explicit-any
|
---|
| 237 | checkFileSystem, ivyMessages, 'warning',
|
---|
| 238 | // eslint-disable-next-line @typescript-eslint/no-explicit-any
|
---|
| 239 | basePath);
|
---|
| 240 | if (diagnostics.messages.length > 0) {
|
---|
| 241 | context.logger.warn(diagnostics.formatDiagnostics(''));
|
---|
| 242 | }
|
---|
| 243 | // Serialize all extracted messages
|
---|
| 244 | const serializer = await getSerializer(format, i18n.sourceLocale, basePath, useLegacyIds, diagnostics);
|
---|
| 245 | const content = serializer.serialize(ivyMessages);
|
---|
| 246 | // Ensure directory exists
|
---|
| 247 | const outputPath = path.dirname(outFile);
|
---|
| 248 | if (!fs.existsSync(outputPath)) {
|
---|
| 249 | fs.mkdirSync(outputPath, { recursive: true });
|
---|
| 250 | }
|
---|
| 251 | // Write translation file
|
---|
| 252 | fs.writeFileSync(outFile, content);
|
---|
| 253 | return webpackResult;
|
---|
| 254 | }
|
---|
| 255 | exports.execute = execute;
|
---|
| 256 | exports.default = architect_1.createBuilder(execute);
|
---|