UNPKG

@gasket/plugin-intl

Version:
99 lines (98 loc) 4.33 kB
/// <reference types="@gasket/plugin-logger" /> Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, /** * Constructs a manifest of locale file paths and settings which can be loaded * or bundled by the client. Locale paths have content hashes associated with * them which can be used for cache busting. * @param {import('@gasket/core').Gasket} gasket - Gasket API * @param {object} options - Build options * @async */ "default", { enumerable: true, get: function() { return buildManifest; } }); const _path = /*#__PURE__*/ _interop_require_default(require("path")); const _promises = /*#__PURE__*/ _interop_require_default(require("node:fs/promises")); const _configureutils = require("./utils/configure-utils.cjs"); const _glob = require("glob"); const _debug = /*#__PURE__*/ _interop_require_default(require("debug")); function _interop_require_default(obj) { return obj && obj.__esModule ? obj : { default: obj }; } const debugLog = (0, _debug.default)('gasket:plugin:intl:buildManifest'); async function buildManifest(gasket, options = {}) { const { logger } = gasket; const tgtRoot = options.root || gasket.config.root; const { localesDir, managerFilename } = (0, _configureutils.getIntlConfig)(gasket); const tgtLocalesDir = _path.default.join(tgtRoot, localesDir); const tgtFile = _path.default.join(tgtRoot, ...managerFilename.split('/')); const { defaultLocaleFilePath, staticLocaleFilePaths, defaultLocale, locales, localesMap } = (0, _configureutils.getIntlConfig)(gasket); // find all the .json files except the manifest debugLog(`Building manifest ${tgtFile} from JSON files in ${localesDir}`); const files = (await (0, _glob.glob)('**/*.json', { cwd: tgtLocalesDir })).filter((f)=>f !== managerFilename); if (!files.length) { logger.warn(`build:locales: No locale files found (${localesDir}).`); } // generate a content hash for each file const imports = files.map((file)=>{ const managerDir = _path.default.dirname(_path.default.join(tgtRoot, ...managerFilename.split('/'))); const relativePath = _path.default.relative(managerDir, tgtLocalesDir) ?? '.'; let importName = _path.default.join(relativePath, file).replace(/\\/g, '/'); importName = importName.startsWith('.') ? importName : `./${importName}`; const keyName = importName.replace(/\.json$/, '').replace(/^[./]+/, ''); if (gasket.config.intl.experimentalImportAttributes) { return { [keyName]: `%() => import('${importName}', { with: { type: 'json' } })%` }; } return { [keyName]: `%() => import('${importName}')%` }; }).reduce((a, c)=>({ ...a, ...c }), {}); /** * Locale settings and known locale file paths */ const manifest = { '%defaultLocaleFilePath%': defaultLocaleFilePath, '%staticLocaleFilePaths%': staticLocaleFilePaths, '%defaultLocale%': defaultLocale, '%locales%': locales, '%localesMap%': localesMap, '%imports%': imports }; let manifestStr = JSON.stringify(manifest, null, 2); manifestStr = manifestStr.replace(/("%|%")/g, ''); manifestStr = manifestStr.replace(/"/g, `'`); const ts = managerFilename.endsWith('.ts'); const lines = []; lines.push('/* -- GENERATED FILE - DO NOT EDIT -- */'); lines.push(`import { makeIntlManager } from '@gasket/intl';`); if (ts) lines.push(`import type { LocaleManifest } from '@gasket/intl';`); lines.push(''); if (ts) lines.push(`const manifest: LocaleManifest = ${manifestStr};`); else lines.push(`const manifest = ${manifestStr};`); lines.push(''); lines.push(`export default makeIntlManager(manifest);`); lines.push(''); const content = lines.join('\n'); const tgtRelative = _path.default.relative(tgtRoot, tgtFile); try { await _promises.default.writeFile(tgtFile, content, 'utf-8'); if (options.silent !== true) { logger.info(`build:locales: Wrote intl manager ${tgtRelative}.`); } } catch (err) { logger.error(`build:locales: Unable to write intl manager (${tgtRelative}).`); throw err; } }