Install dynamic modules with correct meteorInstall options.

This commit is contained in:
Ben Newman
2017-02-05 13:08:59 -05:00
parent 6095204bb3
commit c6e27dc447
2 changed files with 42 additions and 15 deletions

View File

@@ -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!

View File

@@ -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;