-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathi18n-inlining.js
81 lines (81 loc) · 3.05 KB
/
i18n-inlining.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const fs = require("fs");
const path = require("path");
const action_executor_1 = require("./action-executor");
const copy_assets_1 = require("./copy-assets");
function emittedFilesToInlineOptions(emittedFiles, scriptsEntryPointName, emittedPath, outputPath, es5, missingTranslation) {
const options = [];
const originalFiles = [];
for (const emittedFile of emittedFiles) {
if (emittedFile.asset ||
emittedFile.extension !== '.js' ||
(emittedFile.name && scriptsEntryPointName.includes(emittedFile.name))) {
continue;
}
const originalPath = path.join(emittedPath, emittedFile.file);
const action = {
filename: emittedFile.file,
code: fs.readFileSync(originalPath, 'utf8'),
es5,
outputPath,
missingTranslation,
setLocale: emittedFile.name === 'main' || emittedFile.name === 'vendor',
};
originalFiles.push(originalPath);
try {
const originalMapPath = originalPath + '.map';
action.map = fs.readFileSync(originalMapPath, 'utf8');
originalFiles.push(originalMapPath);
}
catch (err) {
if (err.code !== 'ENOENT') {
throw err;
}
}
options.push(action);
}
return { options, originalFiles };
}
async function i18nInlineEmittedFiles(context, emittedFiles, i18n, baseOutputPath, outputPaths, scriptsEntryPointName, emittedPath, es5, missingTranslation) {
const executor = new action_executor_1.BundleActionExecutor({ i18n });
let hasErrors = false;
try {
const { options, originalFiles: processedFiles } = emittedFilesToInlineOptions(emittedFiles, scriptsEntryPointName, emittedPath, baseOutputPath, es5, missingTranslation);
for await (const result of executor.inlineAll(options)) {
for (const diagnostic of result.diagnostics) {
if (diagnostic.type === 'error') {
hasErrors = true;
context.logger.error(diagnostic.message);
}
else {
context.logger.warn(diagnostic.message);
}
}
}
// Copy any non-processed files into the output locations
await copy_assets_1.copyAssets([
{
glob: '**/*',
input: emittedPath,
output: '',
ignore: [...processedFiles].map(f => path.relative(emittedPath, f)),
},
], outputPaths, '');
}
catch (err) {
context.logger.error('Localized bundle generation failed: ' + err.message);
return false;
}
finally {
await executor.stop();
}
if (hasErrors) {
context.logger.error('Localized bundle generation failed.');
}
else {
context.logger.info('Localized bundle generation complete.');
}
return !hasErrors;
}
exports.i18nInlineEmittedFiles = i18nInlineEmittedFiles;