aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/index.js
diff options
context:
space:
mode:
Diffstat (limited to 'src/index.js')
-rw-r--r--src/index.js50
1 files changed, 36 insertions, 14 deletions
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) {
137 psModuleMap: cache.psModuleMap, 137 psModuleMap: cache.psModuleMap,
138 warnings: [], 138 warnings: [],
139 errors: [], 139 errors: [],
140 compilationStarted: cache.compilationStarted, 140 compilationStarted: false,
141 compilationFinished: cache.compilationFinished, 141 compilationFinished: false,
142 installed: cache.installed, 142 installed: cache.installed,
143 srcOption: cache.srcOption 143 srcOption: cache.srcOption
144 }; 144 };
@@ -217,8 +217,20 @@ module.exports = function purescriptLoader(source, map) {
217 }; 217 };
218 218
219 const rebuild = () => 219 const rebuild = () =>
220 ide.rebuild(psModule).catch(error => { 220 ide.rebuild(psModule)
221 .then(() =>
222 toJavaScript(psModule)
223 .then(psModule.load)
224 .catch(psModule.reject)
225 )
226 .catch(error => {
221 if (error instanceof ide.UnknownModuleError) { 227 if (error instanceof ide.UnknownModuleError) {
228 // Store the modules that trigger a recompile due to an
229 // unknown module error. We need to wait until compilation is
230 // done before loading these files.
231
232 cache.deferred.push(psModule);
233
222 if (!cache.compilationStarted) { 234 if (!cache.compilationStarted) {
223 cache.compilationStarted = true; 235 cache.compilationStarted = true;
224 236
@@ -233,28 +245,37 @@ module.exports = function purescriptLoader(source, map) {
233 cache.psModuleMap = map; 245 cache.psModuleMap = map;
234 }) 246 })
235 ) 247 )
236 .then(() => ide.load(psModule)) 248 .then(() =>
237 .then(() => psModule) 249 Promise.map(cache.deferred, psModule =>
250 ide.load(psModule)
251 .then(() => toJavaScript(psModule))
252 .then(psModule.load)
253 )
254 )
255 .catch(error => {
256 cache.deferred[0].reject(error);
257
258 cache.deferred.slice(1).forEach(psModule => {
259 psModule.reject(new Error('purs-loader failed'));
260 })
261 })
238 ; 262 ;
239 } 263 }
240 else { 264 else {
241 return Promise.resolve(psModule); 265 // The compilation has started. We must wait until it is
266 // done in order to ensure the module map contains all of
267 // the unknown modules.
242 } 268 }
243 } 269 }
244 else { 270 else {
245 debug('ide rebuild failed due to an unhandled error: %o', error); 271 debug('ide rebuild failed due to an unhandled error: %o', error);
246 272
247 return Promise.reject(error); 273 psModule.reject(error);
248 } 274 }
249 }) 275 })
250 ; 276 ;
251 277
252 connect() 278 connect().then(rebuild);
253 .then(rebuild)
254 .then(toJavaScript)
255 .then(psModule.load)
256 .catch(psModule.reject)
257 ;
258 } 279 }
259 else if (cache.compilationFinished) { 280 else if (cache.compilationFinished) {
260 debugVerbose('compilation is already finished, loading module %s', psModule.name); 281 debugVerbose('compilation is already finished, loading module %s', psModule.name);
@@ -292,7 +313,8 @@ module.exports = function purescriptLoader(source, map) {
292 ) 313 )
293 .then(() => 314 .then(() =>
294 Promise.map(cache.deferred, psModule => 315 Promise.map(cache.deferred, psModule =>
295 toJavaScript(psModule).then(psModule.load) 316 toJavaScript(psModule)
317 .then(psModule.load)
296 ) 318 )
297 ) 319 )
298 .catch(error => { 320 .catch(error => {