diff --git a/packages/dynamic-import/README.md b/packages/dynamic-import/README.md index ea7b121df7..e7fe7c85ee 100644 --- a/packages/dynamic-import/README.md +++ b/packages/dynamic-import/README.md @@ -21,4 +21,5 @@ Remaining work: - [ ] Batch multiple __dynamicImport method calls. - [x] Make sure client-only reloads work (revisit _read caching). - [x] Make sure path manipulation is Windows-safe. +- [x] Install dynamic modules with correct `meteorInstall` options. - [ ] Tests! diff --git a/packages/dynamic-import/client.js b/packages/dynamic-import/client.js index 755baad591..e1bb39a548 100644 --- a/packages/dynamic-import/client.js +++ b/packages/dynamic-import/client.js @@ -43,24 +43,50 @@ Module.prototype.dynamicImport = function (id) { } ); - }).then(function (resultsTree) { - function walk(tree) { - if (typeof tree === "string") { - return (0, eval)("(" + tree + ")"); - } - - Object.keys(tree).forEach(function (name) { - tree[name] = walk(tree[name]); - }); - - return tree; - } - - meteorInstall(walk(resultsTree)); // TODO Options! - }).then(get); + }).then(installResults).then(get); }); }; +function installResults(resultsTree) { + var parts = [""]; + var trees = []; + var options = []; + + function walk(tree) { + if (typeof tree === "string") { + var meta = requireMeta(parts.join("/")); + var optionsIndex = options.indexOf(meta.options); + if (optionsIndex < 0) { + options[optionsIndex = options.length] = meta.options; + trees.push(Object.create(null)); + } + + // The results tree is partitioned into separate trees according + // to the meta.options object that governs the tree. Usually the + // number of trees will be approximately one, because options + // are shared by entire bundles. + addToTree( + trees[optionsIndex], + meta.module.id, + (0, eval)("(" + tree + ")") + ); + + } else { + Object.keys(tree).forEach(function (name) { + parts.push(name); + walk(tree[name]); + parts.pop(name); + }); + } + } + + walk(resultsTree); + + trees.forEach(function (tree, i) { + meteorInstall(tree, options[i]); + }); +} + function addToTree(tree, id, value) { var parts = id.split("/"); var lastIndex = parts.length - 1;