Send elm compiler errors as JSON.

This commit is contained in:
Dillon Kearns 2021-04-07 17:37:49 -07:00
parent 451534addf
commit 3e2b699c1f
3 changed files with 66 additions and 39 deletions

View File

@ -11,7 +11,7 @@ function connect(refetchContentJson) {
var reloadUrl = evt.data;
var myRequest = new Request(reloadUrl);
myRequest.cache = "no-cache";
fetch(myRequest).then(function (response) {
fetch(myRequest).then(async function (response) {
if (response.ok) {
response.text().then(function (value) {
module.hot.apply();
@ -19,11 +19,12 @@ function connect(refetchContentJson) {
eval(value);
});
} else {
console.error(
"HMR fetch failed:",
response.status,
response.statusText
);
try {
const errorJson = await response.json();
console.error("JSON", errorJson);
} catch (jsonParsingError) {
console.log("Couldn't parse error", jsonParsingError);
}
}
});
}

View File

@ -3,44 +3,65 @@ const fs = require("fs");
const path = require("path");
const debug = true;
function spawnElmMake(elmEntrypointPath, outputPath, cwd) {
async function spawnElmMake(elmEntrypointPath, outputPath, cwd) {
const fullOutputPath = cwd ? path.join(cwd, outputPath) : outputPath;
await runElm(elmEntrypointPath, outputPath, cwd);
const elmFileContent = fs.readFileSync(fullOutputPath, "utf-8");
fs.writeFileSync(
fullOutputPath,
elmFileContent.replace(
/return \$elm\$json\$Json\$Encode\$string\(.REPLACE_ME_WITH_JSON_STRINGIFY.\)/g,
"return " + (debug ? "_Json_wrap(x)" : "x")
)
);
}
/**
* @param {string} elmEntrypointPath
* @param {string} outputPath
* @param {string} [ cwd ]
*/
async function runElm(elmEntrypointPath, outputPath, cwd) {
console.log("Running elm make");
return new Promise((resolve, reject) => {
const fullOutputPath = cwd ? path.join(cwd, outputPath) : outputPath;
const subprocess = runElm(elmEntrypointPath, outputPath, cwd);
const child = spawnCallback(
`elm`,
[
"make",
elmEntrypointPath,
"--output",
outputPath,
"--debug",
"--report",
"json",
],
{ cwd: cwd }
);
subprocess.on("close", (code) => {
const fileOutputExists = fs.existsSync(fullOutputPath);
const elmFileContent = fs.readFileSync(fullOutputPath, "utf-8");
let scriptOutput = "";
fs.writeFileSync(
fullOutputPath,
elmFileContent.replace(
/return \$elm\$json\$Json\$Encode\$string\(.REPLACE_ME_WITH_JSON_STRINGIFY.\)/g,
"return " + (debug ? "_Json_wrap(x)" : "x")
)
);
if (code == 0 && fileOutputExists) {
child.stdout.setEncoding("utf8");
child.stdout.on("data", function (/** @type {string} */ data) {
scriptOutput += data.toString();
});
child.stderr.setEncoding("utf8");
child.stderr.on("data", function (/** @type {string} */ data) {
scriptOutput += data.toString();
});
child.on("close", function (code) {
if (code === 0) {
resolve();
} else {
reject();
reject(scriptOutput);
}
});
});
}
function runElm(elmEntrypointPath, outputPath, cwd) {
console.log("Running elm make");
return spawnCallback(
`elm`,
["make", elmEntrypointPath, "--output", outputPath, "--debug"],
{
// ignore stdout
stdio: ["inherit", "ignore", "inherit"],
cwd: cwd,
}
);
}
module.exports = {
spawnElmMake,
};

View File

@ -40,11 +40,16 @@ spawnElmMake("TemplateModulesBeta.elm", "elm.js", "elm-stuff/elm-pages");
http
.createServer(async function (request, response) {
if (request.url?.startsWith("/elm.js")) {
await clientElmMakeProcess;
response.writeHead(200, { "Content-Type": "text/javascript" });
response.end(
inject(fs.readFileSync(pathToClientElm, { encoding: "utf8" }))
);
try {
await clientElmMakeProcess;
response.writeHead(200, { "Content-Type": "text/javascript" });
response.end(
inject(fs.readFileSync(pathToClientElm, { encoding: "utf8" }))
);
} catch (elmCompilerError) {
response.writeHead(500, { "Content-Type": "application/json" });
response.end(elmCompilerError);
}
} else if (request.url?.startsWith("/stream")) {
handleStream(response);
} else {