UNPKG

next-intl

Version:

Internationalization (i18n) for Next.js

62 lines (56 loc) 1.91 kB
import ExtractionCompiler from '../../extractor/ExtractionCompiler.js'; import { once } from '../utils.js'; // Single compiler instance, initialized once per process let compiler; const runOnce = once('_NEXT_INTL_EXTRACT'); function initExtractionCompiler(pluginConfig) { const experimental = pluginConfig.experimental; if (!experimental?.extract) { return; } // Avoid rollup's `replace` plugin to compile this away const isDevelopment = process.env['NODE_ENV'.trim()] === 'development'; // Avoid running for: // - info // - start // - typegen // // Doesn't consult Next.js config anyway: // - telemetry // - lint // // What remains are: // - dev (NODE_ENV=development) // - build (NODE_ENV=production) const shouldRun = isDevelopment || process.argv.includes('build'); if (!shouldRun) return; runOnce(() => { const extractorConfig = { srcPath: experimental.srcPath, sourceLocale: experimental.extract.sourceLocale, messages: experimental.messages }; compiler = new ExtractionCompiler(extractorConfig, { isDevelopment, projectRoot: process.cwd() }); // Fire-and-forget: Start extraction, don't block config return. // In dev mode, this also starts the file watcher. // In prod, ideally we would wait until the extraction is complete, // but we can't `await` anywhere (at least for Turbopack). // The result is ok though, as if we encounter untranslated messages, // we'll simply add empty messages to the catalog. So for actually // running the app, there is no difference. compiler.extractAll(); function cleanup() { if (compiler) { compiler[Symbol.dispose](); compiler = undefined; } } process.on('exit', cleanup); process.on('SIGINT', cleanup); process.on('SIGTERM', cleanup); }); } export { initExtractionCompiler as default };