Add conditionals in preparation for beta build CLI flag for beta (currently hardcoded to false).

This commit is contained in:
Dillon Kearns 2021-02-06 08:36:45 -08:00
parent d702764be2
commit 4a93623b7a

View File

@ -15,6 +15,7 @@ const terser = require("terser");
const DIR_PATH = path.join(process.cwd()); const DIR_PATH = path.join(process.cwd());
const OUTPUT_FILE_NAME = "elm.js"; const OUTPUT_FILE_NAME = "elm.js";
const debug = false;
let foundErrors = false; let foundErrors = false;
process.on("unhandledRejection", (error) => { process.on("unhandledRejection", (error) => {
@ -161,8 +162,12 @@ async function compileElm() {
await spawnElmMake("src/Main.elm", outputPath); await spawnElmMake("src/Main.elm", outputPath);
const elmEsmContent = await elmToEsm(path.join(process.cwd(), outputPath)); const elmEsmContent = await elmToEsm(path.join(process.cwd(), outputPath));
const elmFileOutput = await runTerserNew(elmEsmContent); if (debug) {
await fs.writeFile(path.join(process.cwd(), outputPath), elmFileOutput); await fs.writeFile(path.join(process.cwd(), outputPath), elmEsmContent);
} else {
const elmFileOutput = await runTerserNew(elmEsmContent);
await fs.writeFile(path.join(process.cwd(), outputPath), elmFileOutput);
}
} }
function spawnElmMake(elmEntrypointPath, outputPath, cwd) { function spawnElmMake(elmEntrypointPath, outputPath, cwd) {
@ -173,15 +178,7 @@ function spawnElmMake(elmEntrypointPath, outputPath, cwd) {
force: true /* ignore errors if file doesn't exist */, force: true /* ignore errors if file doesn't exist */,
}); });
} }
const subprocess = spawnCallback( const subprocess = runElm(elmEntrypointPath, outputPath, cwd)
`elm-optimize-level-2`,
[elmEntrypointPath, "--output", outputPath],
{
// ignore stdout
stdio: ["inherit", "ignore", "inherit"],
cwd: cwd,
}
);
subprocess.on("close", (code) => { subprocess.on("close", (code) => {
const fileOutputExists = fs.existsSync(fullOutputPath); const fileOutputExists = fs.existsSync(fullOutputPath);
@ -195,6 +192,36 @@ function spawnElmMake(elmEntrypointPath, outputPath, cwd) {
}); });
} }
/**
* @param {string} elmEntrypointPath
* @param {string} outputPath
* @param {string} cwd
*/
function runElm(elmEntrypointPath, outputPath, cwd) {
if (debug) {
return spawnCallback(
`elm`,
["make", elmEntrypointPath, "--output", outputPath, "--debug"],
{
// ignore stdout
stdio: ["inherit", "ignore", "inherit"],
cwd: cwd,
}
);
} else {
return spawnCallback(
`elm-optimize-level-2`,
[elmEntrypointPath, "--output", outputPath],
{
// ignore stdout
stdio: ["inherit", "ignore", "inherit"],
cwd: cwd,
}
);
}
}
/** /**
* @param {string} fileContents * @param {string} fileContents
* @returns string * @returns string
@ -255,7 +282,7 @@ async function compileCliApp() {
ELM_FILE_PATH, ELM_FILE_PATH,
elmFileContent.replace( elmFileContent.replace(
/return \$elm\$json\$Json\$Encode\$string\(.REPLACE_ME_WITH_JSON_STRINGIFY.\)/g, /return \$elm\$json\$Json\$Encode\$string\(.REPLACE_ME_WITH_JSON_STRINGIFY.\)/g,
"return x" ('return ' + (debug ? '_Json_wrap(x)' : 'x'))
) )
); );
} }