Some checks failed
Build & Deploy / 🔍 Prepare (push) Successful in 20s
Build & Deploy / 🧪 QA (push) Failing after 34s
Build & Deploy / 🏗️ Build (push) Has started running
Build & Deploy / 🚀 Deploy (push) Has been cancelled
Build & Deploy / 🧪 Smoke Test (push) Has been cancelled
Build & Deploy / ⚡ Lighthouse (push) Has been cancelled
Build & Deploy / 🔔 Notify (push) Has been cancelled
60 lines
1.8 KiB
Plaintext
60 lines
1.8 KiB
Plaintext
import ExtractionCompiler from '../../extractor/ExtractionCompiler.js';
|
|
import { isDevelopment, isNextBuild } from '../config.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 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 || isNextBuild;
|
|
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 };
|