UNPKG

@lingui/vite-plugin

Version:

Vite plugin to integrate Lingui message catalogs with the CLI and bundler pipeline

121 lines (115 loc) 3.99 kB
import { getConfig } from '@lingui/conf'; import { getCatalogForFile, getCatalogs, getCatalogDependentFiles, createMissingErrorMessage, createCompiledCatalog, createCompilationErrorMessage } from '@lingui/cli/api'; import path from 'path'; const linguiTransformerBabelPreset = (options, linguiConfigConfigOpts = {}) => { const config = getConfig(linguiConfigConfigOpts); const macroIds = /* @__PURE__ */ new Set([ ...config.macro.corePackage, ...config.macro.jsxPackage ]); const macroPattern = Array.from(macroIds).map((id) => id.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")).join("|"); return { preset: { plugins: [["@lingui/babel-plugin-lingui-macro", options]] }, rolldown: { filter: { code: new RegExp(`from ['"](?:${macroPattern})['"]`) } } }; }; const fileRegex = /(\.po|\?lingui)$/; function lingui({ failOnMissing, failOnCompileError, ...linguiConfig } = {}) { let config; return [ { name: "vite-plugin-lingui-get-config", enforce: "pre", configResolved: () => { config = getConfig(linguiConfig); } }, { name: "vite-plugin-lingui-load-catalog", transform: { filter: { id: fileRegex }, async handler(src, id) { if (!fileRegex.test(id)) { return; } id = id.split("?")[0]; const catalogRelativePath = path.relative(config.rootDir, id); const fileCatalog = getCatalogForFile( catalogRelativePath, await getCatalogs(config) ); if (!fileCatalog) { throw new Error( `Requested resource ${catalogRelativePath} is not matched to any of your catalogs paths specified in "lingui.config". Resource: ${id} Your catalogs: ${config.catalogs.map((c) => c.path).join("\n")} Please check that catalogs.path is filled properly. ` ); } const { locale, catalog } = fileCatalog; const dependency = await getCatalogDependentFiles(catalog, locale); dependency.forEach((file) => this.addWatchFile(file)); const { messages, missing: missingMessages } = await catalog.getTranslations(locale, { fallbackLocales: config.fallbackLocales, sourceLocale: config.sourceLocale }); if (failOnMissing && locale !== config.pseudoLocale && missingMessages.length > 0) { const message = createMissingErrorMessage( locale, missingMessages, "loader" ); throw new Error( `${message} You see this error because \`failOnMissing=true\` in Vite Plugin configuration.` ); } const { source: code, errors } = createCompiledCatalog( locale, messages, { namespace: "es", pseudoLocale: config.pseudoLocale } ); if (errors.length) { const message = createCompilationErrorMessage(locale, errors); if (failOnCompileError) { throw new Error( message + `These errors fail build because \`failOnCompileError=true\` in Lingui Vite plugin configuration.` ); } else { this.warn( message + `You can fail the build on these errors by setting \`failOnCompileError=true\` in Lingui Vite Plugin configuration.` ); } } return { code, map: null, // provide source map if available // Vite 8+ (Rolldown) auto-detects module types by file extension. // Since .po files are transformed to JS, we must explicitly declare // the module type to avoid misinterpretation. moduleType: "js" }; } } } ]; } export { lingui as default, lingui, linguiTransformerBabelPreset };