1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 | Object.defineProperty(exports, "__esModule", { value: true });
|
10 | exports.i18nInlineEmittedFiles = void 0;
|
11 | const fs = require("fs");
|
12 | const path = require("path");
|
13 | const action_executor_1 = require("./action-executor");
|
14 | const copy_assets_1 = require("./copy-assets");
|
15 | const spinner_1 = require("./spinner");
|
16 | function emittedFilesToInlineOptions(emittedFiles, scriptsEntryPointName, emittedPath, outputPath, es5, missingTranslation) {
|
17 | const options = [];
|
18 | const originalFiles = [];
|
19 | for (const emittedFile of emittedFiles) {
|
20 | if (emittedFile.asset ||
|
21 | emittedFile.extension !== '.js' ||
|
22 | (emittedFile.name && scriptsEntryPointName.includes(emittedFile.name))) {
|
23 | continue;
|
24 | }
|
25 | const originalPath = path.join(emittedPath, emittedFile.file);
|
26 | const action = {
|
27 | filename: emittedFile.file,
|
28 | code: fs.readFileSync(originalPath, 'utf8'),
|
29 | es5,
|
30 | outputPath,
|
31 | missingTranslation,
|
32 | setLocale: emittedFile.name === 'main' || emittedFile.name === 'vendor',
|
33 | };
|
34 | originalFiles.push(originalPath);
|
35 | try {
|
36 | const originalMapPath = originalPath + '.map';
|
37 | action.map = fs.readFileSync(originalMapPath, 'utf8');
|
38 | originalFiles.push(originalMapPath);
|
39 | }
|
40 | catch (err) {
|
41 | if (err.code !== 'ENOENT') {
|
42 | throw err;
|
43 | }
|
44 | }
|
45 | options.push(action);
|
46 | }
|
47 | return { options, originalFiles };
|
48 | }
|
49 | async function i18nInlineEmittedFiles(context, emittedFiles, i18n, baseOutputPath, outputPaths, scriptsEntryPointName, emittedPath, es5, missingTranslation) {
|
50 | const executor = new action_executor_1.BundleActionExecutor({ i18n });
|
51 | let hasErrors = false;
|
52 | const spinner = new spinner_1.Spinner();
|
53 | spinner.start('Generating localized bundles...');
|
54 | try {
|
55 | const { options, originalFiles: processedFiles } = emittedFilesToInlineOptions(emittedFiles, scriptsEntryPointName, emittedPath, baseOutputPath, es5, missingTranslation);
|
56 | for await (const result of executor.inlineAll(options)) {
|
57 | for (const diagnostic of result.diagnostics) {
|
58 | spinner.stop();
|
59 | if (diagnostic.type === 'error') {
|
60 | hasErrors = true;
|
61 | context.logger.error(diagnostic.message);
|
62 | }
|
63 | else {
|
64 | context.logger.warn(diagnostic.message);
|
65 | }
|
66 | spinner.start();
|
67 | }
|
68 | }
|
69 |
|
70 | await copy_assets_1.copyAssets([
|
71 | {
|
72 | glob: '**/*',
|
73 | input: emittedPath,
|
74 | output: '',
|
75 | ignore: [...processedFiles].map((f) => path.relative(emittedPath, f)),
|
76 | },
|
77 | ], outputPaths, '');
|
78 | }
|
79 | catch (err) {
|
80 | spinner.fail('Localized bundle generation failed: ' + err.message);
|
81 | return false;
|
82 | }
|
83 | finally {
|
84 | executor.stop();
|
85 | }
|
86 | if (hasErrors) {
|
87 | spinner.fail('Localized bundle generation failed.');
|
88 | }
|
89 | else {
|
90 | spinner.succeed('Localized bundle generation complete.');
|
91 | }
|
92 | return !hasErrors;
|
93 | }
|
94 | exports.i18nInlineEmittedFiles = i18nInlineEmittedFiles;
|