in packages/angular_devkit/build_angular/src/utils/process-bundle.ts [130:227]
export async function inlineLocales(options: InlineOptions) {
if (!i18n || i18n.inlineLocales.size === 0) {
return { file: options.filename, diagnostics: [], count: 0 };
}
if (i18n.flatOutput && i18n.inlineLocales.size > 1) {
throw new Error('Flat output is only supported when inlining one locale.');
}
const hasLocalizeName = options.code.includes(localizeName);
if (!hasLocalizeName && !options.setLocale) {
return inlineCopyOnly(options);
}
await loadLocalizeTools();
let ast: ParseResult | undefined | null;
try {
ast = parseSync(options.code, {
babelrc: false,
configFile: false,
sourceType: 'script',
filename: options.filename,
});
} catch (error) {
if (error.message) {
// Make the error more readable.
// Same errors will contain the full content of the file as the error message
// Which makes it hard to find the actual error message.
const index = error.message.indexOf(')\n');
const msg = index !== -1 ? error.message.substr(0, index + 1) : error.message;
throw new Error(`${msg}\nAn error occurred inlining file "${options.filename}"`);
}
}
if (!ast) {
throw new Error(`Unknown error occurred inlining file "${options.filename}"`);
}
if (!USE_LOCALIZE_PLUGINS) {
return inlineLocalesDirect(ast, options);
}
const diagnostics = [];
for (const locale of i18n.inlineLocales) {
const isSourceLocale = locale === i18n.sourceLocale;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const translations: any = isSourceLocale ? {} : i18n.locales[locale].translation || {};
let localeDataContent;
if (options.setLocale) {
// If locale data is provided, load it and prepend to file
const localeDataPath = i18n.locales[locale]?.dataPath;
if (localeDataPath) {
localeDataContent = await loadLocaleData(localeDataPath, true, options.es5);
}
}
const { diagnostics: localeDiagnostics, plugins } = await createI18nPlugins(
locale,
translations,
isSourceLocale ? 'ignore' : options.missingTranslation || 'warning',
true,
localeDataContent,
);
const transformResult = await transformFromAstSync(ast, options.code, {
filename: options.filename,
// using false ensures that babel will NOT search and process sourcemap comments (large memory usage)
// The types do not include the false option even though it is valid
// eslint-disable-next-line @typescript-eslint/no-explicit-any
inputSourceMap: false as any,
babelrc: false,
configFile: false,
plugins,
compact: !shouldBeautify,
sourceMaps: !!options.map,
});
diagnostics.push(...localeDiagnostics.messages);
if (!transformResult || !transformResult.code) {
throw new Error(`Unknown error occurred processing bundle for "${options.filename}".`);
}
const outputPath = path.join(
options.outputPath,
i18n.flatOutput ? '' : locale,
options.filename,
);
fs.writeFileSync(outputPath, transformResult.code);
if (options.map && transformResult.map) {
const outputMap = remapping([transformResult.map as SourceMapInput, options.map], () => null);
fs.writeFileSync(outputPath + '.map', JSON.stringify(outputMap));
}
}
return { file: options.filename, diagnostics };
}