@jsverse/transloco
Version:
The internationalization (i18n) library for Angular
103 lines • 5.01 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const node_path_1 = require("node:path");
const schematics_1 = require("@angular-devkit/schematics");
const ng_ast_utils_1 = require("@schematics/angular/utility/ng-ast-utils");
const util_1 = require("@schematics/angular/utility/standalone/util");
const rules_1 = require("@schematics/angular/utility/standalone/rules");
const app_config_1 = require("@schematics/angular/utility/standalone/app_config");
const array_1 = require("../utils/array");
const projects_1 = require("../utils/projects");
const translations_1 = require("../utils/translations");
const transloco_1 = require("../utils/transloco");
const schema_1 = require("./schema");
const http_loader_gen_1 = require("./generators/http-loader.gen");
const translation_files_gen_1 = require("./generators/translation-files.gen");
const root_module_gen_1 = require("./generators/root-module.gen");
function updateEnvironmentBaseUrl(host, sourceRoot, defaultValue) {
const template = `$1{
baseUrl: '${defaultValue}',`;
(0, projects_1.setEnvironments)(host, sourceRoot, (env) => env.indexOf('baseUrl') === -1
? env.replace(/(environment.*=*)\{/, template)
: env);
}
function resolveLoaderPath({ host, mainPath, isStandalone, modulePath }) {
let resolved = modulePath;
if (isStandalone) {
const bootstrapCall = (0, util_1.findBootstrapApplicationCall)(host, mainPath);
resolved =
(0, app_config_1.findAppConfig)(bootstrapCall, host, mainPath)?.filePath || mainPath;
resolved = (0, node_path_1.dirname)(resolved);
}
return resolved;
}
function default_1(options) {
return async (host, context) => {
const langs = options.langs.split(',').map((l) => l.trim());
const project = (0, projects_1.getProject)(host, options.project);
const sourceRoot = project.sourceRoot ?? 'src';
const isLib = project.projectType === 'library';
const assetsPath = `${sourceRoot}/${options.path}`;
const mainPath = await (0, util_1.getMainFilePath)(host, options.project);
const isStandalone = (0, ng_ast_utils_1.isStandaloneApp)(host, mainPath);
const modulePath = isStandalone
? ''
: (0, node_path_1.dirname)((0, ng_ast_utils_1.getAppModulePath)(host, mainPath));
const actions = [];
if (options.loader === schema_1.Loaders.Http) {
const loaderPath = resolveLoaderPath({
host,
mainPath,
isStandalone,
modulePath,
});
if (isStandalone) {
actions.push((0, rules_1.addRootProvider)(options.project, ({ code, external }) => code `${external('provideHttpClient', '@angular/common/http')}()`));
}
else {
actions.push((0, rules_1.addRootImport)(options.project, ({ code, external }) => code `${external('HttpClientModule', '@angular/common/http')}`));
}
actions.push((0, schematics_1.mergeWith)((0, http_loader_gen_1.createLoaderFile)({
ssr: options.ssr,
loaderPath,
})));
}
const hasTranslationFiles = (0, translations_1.checkIfTranslationFilesExist)(assetsPath, langs, '.json', true);
if (!hasTranslationFiles) {
actions.push((0, schematics_1.mergeWith)((0, translation_files_gen_1.createTranslateFiles)(langs, translation_files_gen_1.jsonTranslationFileCreator, assetsPath)));
}
if (isStandalone) {
actions.push((0, rules_1.addRootProvider)(options.project, ({ code, external }) => {
external('isDevMode', '@angular/core');
external('TranslocoHttpLoader', './transloco-loader');
return code `${external('provideTransloco', '@jsverse/transloco')}({
config: {
availableLangs: [${(0, array_1.stringifyList)(langs)}],
defaultLang: '${langs[0]}',
// Remove this option if your application doesn't support changing language in runtime.
reRenderOnLangChange: true,
prodMode: !isDevMode(),
},
loader: TranslocoHttpLoader
})`;
}));
}
else {
actions.push((0, rules_1.addRootImport)(options.project, ({ code, external }) => code `${external('TranslocoRootModule', './transloco-root.module')}`), (0, schematics_1.mergeWith)((0, root_module_gen_1.createTranslocoModule)({
sourceRoot,
isLib,
ssr: options.ssr,
langs,
modulePath,
host,
})));
}
if (options.ssr) {
updateEnvironmentBaseUrl(host, sourceRoot, 'http://localhost:4200');
}
(0, transloco_1.createConfig)(host, langs, assetsPath);
return (0, schematics_1.chain)(actions)(host, context);
};
}
exports.default = default_1;
//# sourceMappingURL=index.js.map