diff options
author | eric thul <thul.eric@gmail.com> | 2017-06-27 17:38:16 -0400 |
---|---|---|
committer | eric thul <thul.eric@gmail.com> | 2017-06-27 17:38:16 -0400 |
commit | e01802ad6542dd9ead2ab753dda1ab8c385279f8 (patch) | |
tree | 4e912e5b7797665aa72bce766a62e77444952576 /src | |
parent | e2b840779a66339d32ae7068c36e15a9aa041f61 (diff) | |
download | purs-loader-e01802ad6542dd9ead2ab753dda1ab8c385279f8.tar.gz purs-loader-e01802ad6542dd9ead2ab753dda1ab8c385279f8.tar.zst purs-loader-e01802ad6542dd9ead2ab753dda1ab8c385279f8.zip |
Ensure module map is generated once
Fixes #97
Diffstat (limited to 'src')
-rw-r--r-- | src/index.js | 50 |
1 files changed, 26 insertions, 24 deletions
diff --git a/src/index.js b/src/index.js index cd85c89..b1c69bf 100644 --- a/src/index.js +++ b/src/index.js | |||
@@ -372,34 +372,36 @@ function bundle(options, cache) { | |||
372 | 372 | ||
373 | // map of PS module names to their source path | 373 | // map of PS module names to their source path |
374 | function psModuleMap(options, cache) { | 374 | function psModuleMap(options, cache) { |
375 | if (cache.psModuleMap) return Promise.resolve(cache.psModuleMap) | 375 | if (cache.psModuleMap) { |
376 | 376 | return cache.psModuleMap; | |
377 | const globs = [].concat(options.src).concat(options.ffi) | 377 | } |
378 | 378 | else { | |
379 | return globby(globs).then(paths => { | 379 | const globs = [].concat(options.src).concat(options.ffi); |
380 | return Promise | 380 | |
381 | .props(paths.reduce((map, file) => { | 381 | cache.psModuleMap = globby(globs).then(paths => |
382 | map[file] = fs.readFileAsync(file, 'utf8') | 382 | Promise.props(paths.reduce((map, file) => { |
383 | return map | 383 | map[file] = fs.readFileAsync(file, 'utf8'); |
384 | }, {})) | 384 | return map; |
385 | .then(fileMap => { | 385 | }, {})).then(fileMap => |
386 | cache.psModuleMap = Object.keys(fileMap).reduce((map, file) => { | 386 | Object.keys(fileMap).reduce((map, file) => { |
387 | const source = fileMap[file] | 387 | const source = fileMap[file]; |
388 | const ext = path.extname(file) | 388 | const ext = path.extname(file); |
389 | const isPurs = ext.match(/purs$/i) | 389 | const isPurs = ext.match(/purs$/i); |
390 | const moduleRegex = isPurs ? srcModuleRegex : ffiModuleRegex | 390 | const moduleRegex = isPurs ? srcModuleRegex : ffiModuleRegex; |
391 | const moduleName = match(moduleRegex, source) | 391 | const moduleName = match(moduleRegex, source); |
392 | map[moduleName] = map[moduleName] || {} | 392 | map[moduleName] = map[moduleName] || {}; |
393 | if (isPurs) { | 393 | if (isPurs) { |
394 | map[moduleName].src = path.resolve(file) | 394 | map[moduleName].src = path.resolve(file); |
395 | } else { | 395 | } else { |
396 | map[moduleName].ffi = path.resolve(file) | 396 | map[moduleName].ffi = path.resolve(file); |
397 | } | 397 | } |
398 | return map | 398 | return map; |
399 | }, {}) | 399 | }, {}) |
400 | return cache.psModuleMap | 400 | ) |
401 | }) | 401 | ); |
402 | }) | 402 | |
403 | return cache.psModuleMap; | ||
404 | } | ||
403 | } | 405 | } |
404 | 406 | ||
405 | function connectIdeServer(psModule) { | 407 | function connectIdeServer(psModule) { |