|
6 | 6 | * found in the LICENSE file at https://angular.io/license
|
7 | 7 | */
|
8 | 8 | import { BuilderContext, BuilderOutput, createBuilder } from '@angular-devkit/architect';
|
9 |
| -import { |
10 |
| - EmittedFiles, |
11 |
| - WebpackLoggingCallback, |
12 |
| - runWebpack, |
13 |
| -} from '@angular-devkit/build-webpack'; |
| 9 | +import { EmittedFiles, WebpackLoggingCallback, runWebpack } from '@angular-devkit/build-webpack'; |
14 | 10 | import { join, json, logging, normalize, tags, virtualFs } from '@angular-devkit/core';
|
15 | 11 | import { NodeJsSyncHost } from '@angular-devkit/core/node';
|
16 | 12 | import * as findCacheDirectory from 'find-cache-dir';
|
@@ -58,6 +54,7 @@ import { copyAssets } from '../utils/copy-assets';
|
58 | 54 | import { I18nOptions, createI18nOptions } from '../utils/i18n-options';
|
59 | 55 | import { createTranslationLoader } from '../utils/load-translations';
|
60 | 56 | import {
|
| 57 | + InlineOptions, |
61 | 58 | ProcessBundleFile,
|
62 | 59 | ProcessBundleOptions,
|
63 | 60 | ProcessBundleResult,
|
@@ -447,76 +444,103 @@ export function buildWebpackBrowser(
|
447 | 444 | }
|
448 | 445 |
|
449 | 446 | context.logger.info('ES5 bundle generation complete.');
|
450 |
| - } finally { |
451 |
| - executor.stop(); |
452 |
| - } |
453 |
| - |
454 |
| - if (i18n.shouldInline) { |
455 |
| - context.logger.info('Generating localized bundles...'); |
456 |
| - |
457 |
| - const localize = await import('@angular/localize/src/tools/src/translate/main'); |
458 |
| - const localizeDiag = await import('@angular/localize/src/tools/src/diagnostics'); |
459 | 447 |
|
460 |
| - const diagnostics = new localizeDiag.Diagnostics(); |
461 |
| - const translationFilePaths = []; |
462 |
| - let handleSourceLocale = false; |
463 |
| - for (const locale of i18n.inlineLocales) { |
464 |
| - if (locale === i18n.sourceLocale) { |
465 |
| - handleSourceLocale = true; |
466 |
| - continue; |
467 |
| - } |
468 |
| - translationFilePaths.push(i18n.locales[locale].file); |
469 |
| - } |
| 448 | + if (i18n.shouldInline) { |
| 449 | + context.logger.info('Generating localized bundles...'); |
470 | 450 |
|
471 |
| - if (translationFilePaths.length > 0) { |
472 |
| - const sourceFilePaths = []; |
| 451 | + const inlineActions: InlineOptions[] = []; |
| 452 | + const processedFiles = new Set<string>(); |
473 | 453 | for (const result of processResults) {
|
474 | 454 | if (result.original) {
|
475 |
| - sourceFilePaths.push(result.original.filename); |
| 455 | + inlineActions.push({ |
| 456 | + filename: path.basename(result.original.filename), |
| 457 | + code: fs.readFileSync(result.original.filename, 'utf8'), |
| 458 | + outputPath: baseOutputPath, |
| 459 | + es5: false, |
| 460 | + missingTranslation: options.i18nMissingTranslation, |
| 461 | + }); |
| 462 | + processedFiles.add(result.original.filename); |
476 | 463 | }
|
477 | 464 | if (result.downlevel) {
|
478 |
| - sourceFilePaths.push(result.downlevel.filename); |
| 465 | + inlineActions.push({ |
| 466 | + filename: path.basename(result.downlevel.filename), |
| 467 | + code: fs.readFileSync(result.downlevel.filename, 'utf8'), |
| 468 | + outputPath: baseOutputPath, |
| 469 | + es5: true, |
| 470 | + missingTranslation: options.i18nMissingTranslation, |
| 471 | + }); |
| 472 | + processedFiles.add(result.downlevel.filename); |
479 | 473 | }
|
480 | 474 | }
|
| 475 | + |
| 476 | + let hasErrors = false; |
481 | 477 | try {
|
482 |
| - localize.translateFiles({ |
483 |
| - // tslint:disable-next-line: no-non-null-assertion |
484 |
| - sourceRootPath: webpackStats.outputPath!, |
485 |
| - sourceFilePaths, |
486 |
| - translationFilePaths, |
487 |
| - outputPathFn: (locale, relativePath) => |
488 |
| - path.join(baseOutputPath, locale, relativePath), |
489 |
| - diagnostics, |
490 |
| - missingTranslation: options.i18nMissingTranslation || 'warning', |
491 |
| - sourceLocale: handleSourceLocale ? i18n.sourceLocale : undefined, |
492 |
| - }); |
| 478 | + for (const locale of i18n.inlineLocales) { |
| 479 | + const localeOutputPath = path.join(baseOutputPath, locale); |
| 480 | + if (!fs.existsSync(localeOutputPath)) { |
| 481 | + fs.mkdirSync(localeOutputPath, { recursive: true }); |
| 482 | + } |
| 483 | + } |
| 484 | + |
| 485 | + for await (const result of executor.inlineAll(inlineActions)) { |
| 486 | + if (options.verbose) { |
| 487 | + context.logger.info( |
| 488 | + `Localized "${result.file}" [${result.count} translation(s)].`, |
| 489 | + ); |
| 490 | + } |
| 491 | + for (const diagnostic of result.diagnostics) { |
| 492 | + if (diagnostic.type === 'error') { |
| 493 | + hasErrors = true; |
| 494 | + context.logger.error(diagnostic.message); |
| 495 | + } else { |
| 496 | + context.logger.warn(diagnostic.message); |
| 497 | + } |
| 498 | + } |
| 499 | + } |
| 500 | + |
| 501 | + // Copy any non-processed files into the output locations |
| 502 | + const outputPaths = [...i18n.inlineLocales].map(l => |
| 503 | + path.join(baseOutputPath, l), |
| 504 | + ); |
| 505 | + await copyAssets( |
| 506 | + [ |
| 507 | + { |
| 508 | + glob: '**/*', |
| 509 | + // tslint:disable-next-line: no-non-null-assertion |
| 510 | + input: webpackStats.outputPath!, |
| 511 | + output: '', |
| 512 | + ignore: [...processedFiles].map(f => |
| 513 | + // tslint:disable-next-line: no-non-null-assertion |
| 514 | + path.relative(webpackStats.outputPath!, f), |
| 515 | + ), |
| 516 | + }, |
| 517 | + ], |
| 518 | + outputPaths, |
| 519 | + '', |
| 520 | + ); |
493 | 521 | } catch (err) {
|
494 | 522 | context.logger.error('Localized bundle generation failed: ' + err.message);
|
495 | 523 |
|
496 | 524 | return { success: false };
|
497 |
| - } finally { |
498 |
| - try { |
499 |
| - // Remove temporary directory used for i18n processing |
500 |
| - // tslint:disable-next-line: no-non-null-assertion |
501 |
| - await host.delete(normalize(webpackStats.outputPath!)).toPromise(); |
502 |
| - } catch {} |
503 | 525 | }
|
504 |
| - } |
505 | 526 |
|
506 |
| - context.logger.info( |
507 |
| - `Localized bundle generation ${diagnostics.hasErrors ? 'failed' : 'complete'}.`, |
508 |
| - ); |
| 527 | + context.logger.info( |
| 528 | + `Localized bundle generation ${hasErrors ? 'failed' : 'complete'}.`, |
| 529 | + ); |
509 | 530 |
|
510 |
| - for (const message of diagnostics.messages) { |
511 |
| - if (message.type === 'error') { |
512 |
| - context.logger.error(message.message); |
513 |
| - } else { |
514 |
| - context.logger.warn(message.message); |
| 531 | + if (hasErrors) { |
| 532 | + return { success: false }; |
515 | 533 | }
|
516 | 534 | }
|
| 535 | + } finally { |
| 536 | + executor.stop(); |
517 | 537 |
|
518 |
| - if (diagnostics.hasErrors) { |
519 |
| - return { success: false }; |
| 538 | + if (i18n.shouldInline) { |
| 539 | + try { |
| 540 | + // Remove temporary directory used for i18n processing |
| 541 | + // tslint:disable-next-line: no-non-null-assertion |
| 542 | + await host.delete(normalize(webpackStats.outputPath!)).toPromise(); |
| 543 | + } catch {} |
520 | 544 | }
|
521 | 545 | }
|
522 | 546 |
|
|
0 commit comments