UNPKG

@jsverse/transloco-optimize

Version:

Transloco optimization process for production environments

53 lines (52 loc) 2.04 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.optimizeFiles = exports.getTranslationFiles = exports.getTranslationsFolder = void 0; const tslib_1 = require("tslib"); const node_fs_1 = tslib_1.__importDefault(require("node:fs")); const node_path_1 = tslib_1.__importDefault(require("node:path")); const glob_1 = require("glob"); const flat_1 = require("flat"); const isWindows = process.platform === "win32"; function removeComments(translation, commentsKey = 'comment') { return Object.keys(translation).reduce((acc, key) => { const lastKey = key.split('.').pop(); if (lastKey !== commentsKey) { acc[key] = translation[key]; } return acc; }, {}); } function getTranslationsFolder(dist) { return node_path_1.default.resolve(process.cwd(), dist); } exports.getTranslationsFolder = getTranslationsFolder; function getTranslationFiles(dist) { const filesMatcher = node_path_1.default.resolve(getTranslationsFolder(dist), '**/*.json'); return (0, glob_1.glob)(filesMatcher, { windowsPathsNoEscape: isWindows }); } exports.getTranslationFiles = getTranslationFiles; function optimizeFiles(translationPaths, commentsKey) { return new Promise((resolve, reject) => { let error; for (const path of translationPaths) { try { const translation = node_fs_1.default.readFileSync(path, { encoding: 'utf8' }); const asObject = JSON.parse(translation); const flatObject = (0, flat_1.flatten)(asObject, { safe: true }); const optimized = JSON.stringify(removeComments(flatObject, commentsKey)); node_fs_1.default.writeFileSync(path, optimized, { encoding: 'utf8' }); } catch (err) { error = err; break; } } if (error) { reject(error); } else { resolve(); } }); } exports.optimizeFiles = optimizeFiles;