diff --git a/packages/core/src/code-loading/webRequire.js b/packages/core/src/code-loading/webRequire.js index cb1c33c86..d4e1bb3ab 100644 --- a/packages/core/src/code-loading/webRequire.js +++ b/packages/core/src/code-loading/webRequire.js @@ -72,6 +72,7 @@ const makeWebRequire = (filesAndFolders, options) => { // console.log('*****\n',filesAndFolders,'\n*****') const extensions = {} + const moduleCache = {} /* Require (obtain) the exports for the given require path, relative to the given current path. * The logic is based on the original NODE require() function. @@ -105,6 +106,7 @@ const makeWebRequire = (filesAndFolders, options) => { if (entry.children) return null // directory if (extensions[baseExt]) { + if(moduleCache[requirePath]) return moduleCache[requirePath] // evaluate the content const matchingModule = { exports: {}, @@ -121,7 +123,7 @@ const makeWebRequire = (filesAndFolders, options) => { } } extensions[baseExt](matchingModule, entry.fullPath) - return matchingModule.exports + return moduleCache[requirePath] = matchingModule.exports } return null } diff --git a/packages/core/src/code-loading/webRequire.test.js b/packages/core/src/code-loading/webRequire.test.js index 422b8187a..599415fee 100644 --- a/packages/core/src/code-loading/webRequire.test.js +++ b/packages/core/src/code-loading/webRequire.test.js @@ -13,10 +13,14 @@ test('webRequire: should support require, from a single file', (t) => { let requireFn = makeWebRequire(singleFileJs, { apiMainPath }) let designRootModule = requireFn(singleFileJs[0].fullPath) + const designRootModule2 = requireFn(singleFileJs[0].fullPath) t.true('main' in designRootModule) t.true(designRootModule.main instanceof Function) + t.is(designRootModule, designRootModule2) + t.deepEqual(designRootModule, designRootModule2) + // NOTE: 'jscad' must be registered as an extension const fakeFs = makeFakeFs(singleFileJscad) requireFn = makeWebRequire(singleFileJscad, { apiMainPath }) @@ -32,7 +36,6 @@ test('webRequire: should support require, from a directory with index.js', (t) = const requireFn = makeWebRequire(directoryWithIndexJs, { apiMainPath }) const designRootModule = requireFn('/project') - t.true('main' in designRootModule) t.true(designRootModule.main instanceof Function) })