UNPKG

1.06 kBJavaScriptView Raw
1var omit = require("lodash/omit");
2var through = require("through2");
3var assign = require("lodash/assign");
4
5module.exports = function() {
6 return through.obj(function(data, enc, done) {
7 convertSlimConfig(data)
8 .then(function(mutatedData) {
9 done(null, mutatedData);
10 })
11 .catch(done);
12 });
13};
14
15function convertSlimConfig(data) {
16 var map = {};
17 var graph = data.graph;
18 var loader = data.loader;
19 var config = data.loader.slimConfig;
20
21 function getSlimId(id) {
22 if (graph[id]) {
23 return graph[id].load.uniqueId;
24 } else {
25 throw new Error("Cannot find module: " + id + " in the graph.");
26 }
27 }
28
29 return new Promise(function(resolve) {
30 var idsToBeMapped = config.toMap.map(function(id) {
31 return loader.normalize(id).then(function(name) {
32 map[id] = getSlimId(name);
33 });
34 });
35
36 Promise.all(idsToBeMapped).then(function() {
37 // mutates the slimConfig property!
38 loader.slimConfig = assign({}, omit(config, ["toMap"]), {
39 map: map,
40 extensions: config.extensions.map(getSlimId)
41 });
42
43 resolve(data);
44 });
45 });
46}