From 78e2b0d91f5eb98674c5b05db0ef2c4f417581c0 Mon Sep 17 00:00:00 2001 From: eric thul Date: Sun, 23 Apr 2017 18:17:27 -0400 Subject: Handle multiple forced compiles on rebuild Resolves #90 --- src/bundle.js | 2 +- src/compile.js | 2 +- src/ide.js | 8 ++++---- src/index.js | 50 ++++++++++++++++++++++++++++++++++++-------------- 4 files changed, 42 insertions(+), 20 deletions(-) (limited to 'src') diff --git a/src/bundle.js b/src/bundle.js index 3f55f01..99633f1 100644 --- a/src/bundle.js +++ b/src/bundle.js @@ -27,7 +27,7 @@ module.exports = function bundle(options, bundleModules) { bundleModules.forEach(name => bundleArgs.push('--module', name)) - debug('bundle: %s %o', bundleCommand, bundleArgs); + debug('bundle: %s %O', bundleCommand, bundleArgs); return (new Promise((resolve, reject) => { debug('bundling PureScript...') diff --git a/src/compile.js b/src/compile.js index 707605c..6759bf3 100644 --- a/src/compile.js +++ b/src/compile.js @@ -24,7 +24,7 @@ module.exports = function compile(psModule) { const stderr = []; - debug('compile %s %o', compileCommand, compileArgs) + debug('compile %s %O', compileCommand, compileArgs) return new Promise((resolve, reject) => { debug('compiling PureScript...') diff --git a/src/ide.js b/src/ide.js index ac80789..a2ccbe3 100644 --- a/src/ide.js +++ b/src/ide.js @@ -44,7 +44,7 @@ function spawnIdeClient(body, options) { const stdout = []; - debug('ide client %s %o %o', ideClientCommand, ideClientArgs, body); + debug('ide client %s %o %O', ideClientCommand, ideClientArgs, body); return new Promise((resolve, reject) => { const ideClient = spawn(ideClientCommand, ideClientArgs); @@ -211,7 +211,7 @@ module.exports.rebuild = function rebuild(psModule) { try { const parsed = JSON.parse(response); - debugVerbose('parsed JSON response: %o', parsed); + debugVerbose('parsed JSON response: %O', parsed); return Promise.resolve(parsed); } @@ -224,7 +224,7 @@ module.exports.rebuild = function rebuild(psModule) { const result = Array.isArray(parsed.result) ? parsed.result : []; return Promise.map(result, (item, i) => { - debugVerbose('formatting result %o', item); + debugVerbose('formatting result %O', item); return formatIdeResult(item, options, i, result.length); }).then(formatted => ({ @@ -254,7 +254,7 @@ module.exports.rebuild = function rebuild(psModule) { return isModuleNotFound || isUnknownModule || isUnknownModuleImport; })) { - debug('failed to rebuild because the module is unknown') + debug('module %s was not rebuilt because the module is unknown', psModule.name); return Promise.reject(new UnknownModuleError()); } diff --git a/src/index.js b/src/index.js index 19a549e..f77e5f8 100644 --- a/src/index.js +++ b/src/index.js @@ -137,8 +137,8 @@ module.exports = function purescriptLoader(source, map) { psModuleMap: cache.psModuleMap, warnings: [], errors: [], - compilationStarted: cache.compilationStarted, - compilationFinished: cache.compilationFinished, + compilationStarted: false, + compilationFinished: false, installed: cache.installed, srcOption: cache.srcOption }; @@ -217,8 +217,20 @@ module.exports = function purescriptLoader(source, map) { }; const rebuild = () => - ide.rebuild(psModule).catch(error => { + ide.rebuild(psModule) + .then(() => + toJavaScript(psModule) + .then(psModule.load) + .catch(psModule.reject) + ) + .catch(error => { if (error instanceof ide.UnknownModuleError) { + // Store the modules that trigger a recompile due to an + // unknown module error. We need to wait until compilation is + // done before loading these files. + + cache.deferred.push(psModule); + if (!cache.compilationStarted) { cache.compilationStarted = true; @@ -233,28 +245,37 @@ module.exports = function purescriptLoader(source, map) { cache.psModuleMap = map; }) ) - .then(() => ide.load(psModule)) - .then(() => psModule) + .then(() => + Promise.map(cache.deferred, psModule => + ide.load(psModule) + .then(() => toJavaScript(psModule)) + .then(psModule.load) + ) + ) + .catch(error => { + cache.deferred[0].reject(error); + + cache.deferred.slice(1).forEach(psModule => { + psModule.reject(new Error('purs-loader failed')); + }) + }) ; } else { - return Promise.resolve(psModule); + // The compilation has started. We must wait until it is + // done in order to ensure the module map contains all of + // the unknown modules. } } else { debug('ide rebuild failed due to an unhandled error: %o', error); - return Promise.reject(error); + psModule.reject(error); } }) ; - connect() - .then(rebuild) - .then(toJavaScript) - .then(psModule.load) - .catch(psModule.reject) - ; + connect().then(rebuild); } else if (cache.compilationFinished) { debugVerbose('compilation is already finished, loading module %s', psModule.name); @@ -292,7 +313,8 @@ module.exports = function purescriptLoader(source, map) { ) .then(() => Promise.map(cache.deferred, psModule => - toJavaScript(psModule).then(psModule.load) + toJavaScript(psModule) + .then(psModule.load) ) ) .catch(error => { -- cgit v1.2.3