From 1cdd37e8b40999c91233a8cc9e0b3ab278b5a219 Mon Sep 17 00:00:00 2001 From: Cyril Sobierajewicz <38043722+cyrilfretlink@users.noreply.github.com> Date: Fri, 24 May 2019 14:46:35 +0200 Subject: =?UTF-8?q?Don=E2=80=99t=20unnecessarily=20invalidate=20the=20modu?= =?UTF-8?q?le=20map=20(#124)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Don’t unnecessarily invalidate the module map Additional imports in PureScript sources (compared to their JavaScript output) shouldn’t always invalidate the module map because imports of types are erased and re-exports are followed. Also `Prim.*` modules are internal to the compiler and won’t ever be present in the module map. * Don’t add imports to unused modules Otherwise the following expression ```purs hello :: Effect Unit hello = log "Hello" ``` includes the whole `Prelude` into its chunk whereas only `log` is actually needed at runtime. --- src/to-javascript.js | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/src/to-javascript.js b/src/to-javascript.js index a7d64a2..3663c83 100644 --- a/src/to-javascript.js +++ b/src/to-javascript.js @@ -109,12 +109,21 @@ function makeJS(psModule, psModuleMap, js) { return Promise.resolve(result); } else { - debug('rebuilding module map due to additional imports for %s: %o', name, additionalImports); - - psModule.cache.psModuleMap = null; + const missingImports = additionalImports.filter(moduleName => + !psModuleMap[moduleName] && moduleName.split('.')[0] !== 'Prim' + ); - return updatePsModuleMap(psModule).then(updatedPsModuleMap => { - const additionalImportsResult = additionalImports.map(import_ => { + let updatingPsModuleMap; + if (missingImports.length > 0) { + debug('rebuilding module map due to missing imports for %s: %o', name, missingImports); + psModule.cache.psModuleMap = null; + updatingPsModuleMap = updatePsModuleMap(psModule); + } else { + updatingPsModuleMap = Promise.resolve(psModuleMap); + } + + return updatingPsModuleMap.then(updatedPsModuleMap => { + const missingImportsResult = missingImports.map(import_ => { const moduleValue = updatedPsModuleMap[import_]; if (!moduleValue) { @@ -129,7 +138,7 @@ function makeJS(psModule, psModuleMap, js) { } }).filter(a => a !== null).join('\n'); - return result + '\n' + additionalImportsResult; + return result + '\n' + missingImportsResult; }); } } -- cgit v1.2.3