From d073990a3afd4f532b1d09692df9ccf8bcc0ea52 Mon Sep 17 00:00:00 2001 From: Ben Newman Date: Thu, 16 Nov 2017 10:40:09 -0500 Subject: [PATCH] Prune dynamic imports result tree rather than failing on error. --- packages/dynamic-import/server.js | 32 ++++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/packages/dynamic-import/server.js b/packages/dynamic-import/server.js index 82e07eb3ce..a08c3a3cfb 100644 --- a/packages/dynamic-import/server.js +++ b/packages/dynamic-import/server.js @@ -41,11 +41,25 @@ function readTree(tree, platform) { function walk(node) { if (node && typeof node === "object") { + let empty = true; Object.keys(node).forEach(name => { pathParts.push(name); - node[name] = walk(node[name]); + const result = walk(node[name]); + if (result === null) { + // If the read function returns null, omit this module from the + // resulting tree. + delete node[name]; + } else { + node[name] = result; + empty = false; + } assert.strictEqual(pathParts.pop(), name); }); + if (empty) { + // If every recursive call to walk(node[name]) returned null, + // remove this node from the resulting tree by returning null. + return null; + } } else { return read(pathParts, platform); } @@ -63,13 +77,21 @@ function read(pathParts, platform) { )); if (! absPath.startsWith(dynamicRoot)) { - throw new Meteor.Error("bad dynamic module path"); + console.error("bad dynamic import path:", absPath); + return null; } const cache = getCache(platform); - return hasOwn.call(cache, absPath) - ? cache[absPath] - : cache[absPath] = readFileSync(absPath, "utf8"); + if (hasOwn.call(cache, absPath)) { + return cache[absPath]; + } + + try { + return cache[absPath] = readFileSync(absPath, "utf8"); + } catch (e) { + console.error(e.stack || e); + return null; + } } const cachesByPlatform = Object.create(null);