Prevent errors when watcher re-runs and tries to evaluate Elm with Elm value in namespace.

This commit is contained in:
Dillon Kearns 2019-08-20 17:18:23 -07:00
parent 2bdf4da5d7
commit 1310f5cbea
2 changed files with 19 additions and 12 deletions

View File

@ -7,12 +7,15 @@ function runElm(callback) {
const startingDir = process.cwd();
process.chdir(elmBaseDirectory);
compileToString([mainElmFile], {}).then(function(data) {
eval(data.toString());
const app = Elm.Main.init({ flags: { imageAssets: {} } });
(function() {
eval(data.toString());
const app = Elm.Main.init({ flags: { imageAssets: {} } });
app.ports.toJsPort.subscribe(payload => {
process.chdir(startingDir);
callback(payload);
});
app.ports.toJsPort.subscribe(payload => {
process.chdir(startingDir);
callback(payload);
delete Elm;
});
})();
});
}

View File

@ -15,6 +15,7 @@ const generateRecords = require("./generate-records.js");
const contentGlobPath = "content/**/*.emu";
let watcher = null;
let devServerRunning = false;
function unpackFile(path) {
return { path, contents: fs.readFileSync(path).toString() };
@ -67,12 +68,15 @@ function run() {
doCliStuff(staticRoutes, contents.rawContent, function(manifestConfig) {
if (contents.watch) {
startWatchIfNeeded();
develop.start({
routes: contents.routes,
debug: contents.debug,
fileContents: contents.fileContents,
manifestConfig
});
if (!devServerRunning) {
devServerRunning = true;
develop.start({
routes: contents.routes,
debug: contents.debug,
fileContents: contents.fileContents,
manifestConfig
});
}
} else {
develop.run(
{