react-scripts-intlayer
Version:
Integrate Intlayer with Create React App using custom React scripts for internationalization i18n and advanced Webpack configurations
69 lines (67 loc) • 3.12 kB
JavaScript
let _intlayer_config_node = require("@intlayer/config/node");
let _intlayer_config_utils = require("@intlayer/config/utils");
let node_child_process = require("node:child_process");
let _intlayer_config_logger = require("@intlayer/config/logger");
//#region src/cli/react-scripts-intlayer.ts
/**
* To make the setup easier, we are using craco to override the webpack configuration.
* This script is used to run the craco scripts with the custom configuration.
*
* The script is based on the original craco script from create-react-app.
*/
const args = process.argv.slice(2);
const scriptIndex = args.findIndex((index) => index === "build" || index === "start" || index === "test");
const script = scriptIndex === -1 ? args[0] : args[scriptIndex];
/**
* Build the Intlayer dictionaries (i.e. populate the `.intlayer` folder that
* backs the `@intlayer/dictionaries-entry` alias) before craco/webpack starts.
*
* Unlike the webpack `IntlayerPlugin` — whose `beforeCompile` hook fires only
* after webpack has already begun resolving modules — this runs ahead of the
* compiler so the dictionaries entry exists the moment resolution starts.
* Mirrors the async `withIntlayer` flow used by the Next.js integration.
*/
const prepareDictionaries = async (currentScript) => {
const isBuild = currentScript === "build";
const isDev = currentScript === "start";
const { prepareIntlayer } = await import("@intlayer/engine/build");
await prepareIntlayer((0, _intlayer_config_node.getConfiguration)(), {
clean: isBuild,
env: isBuild ? "prod" : "dev",
cacheTimeoutMs: isDev ? 1e3 * 60 * 60 : 1e3 * 30
});
};
const runScript = async () => {
if (script !== "build" && script !== "start" && script !== "test") {
(0, _intlayer_config_logger.logger)(`Unknown script "${script}".`);
(0, _intlayer_config_logger.logger)("Perhaps you need to update craco?");
return;
}
await prepareDictionaries(script);
const nodeArgs = scriptIndex > 0 ? args.slice(0, scriptIndex) : [];
const scriptPath = (0, _intlayer_config_utils.getProjectRequire)().resolve(`@craco/craco/dist/scripts/${script}`);
const scriptArgs = args.slice(scriptIndex + 1);
const child = (0, node_child_process.spawnSync)("node", nodeArgs.concat(scriptPath).concat([
...scriptArgs,
"--config",
"./node_modules/react-scripts-intlayer/dist/cjs/craco.config.cjs"
]), { stdio: "inherit" });
if (child.signal) {
if (child.signal === "SIGKILL") (0, _intlayer_config_logger.logger)(`
The build failed because the process exited too early.
This probably means the system ran out of memory or someone called
\`kill -9\` on the process.
`);
else if (child.signal === "SIGTERM") (0, _intlayer_config_logger.logger)(`
The build failed because the process exited too early.
Someone might have called \`kill\` or \`killall\`, or the system could
be shutting down.
`);
process.exit(1);
}
process.exit(child.status ?? void 0);
};
runScript();
//#endregion
//# sourceMappingURL=react-scripts-intlayer.cjs.map