UNPKG

1 kBJavaScriptView Raw
1var through = require("through2");
2var keys = require("lodash/keys");
3var isArray = require("lodash/isArray");
4
5module.exports = function() {
6 return through.obj(function(data, enc, next) {
7 try {
8 next(null, addModuleIds(data));
9 } catch (err) {
10 next(err);
11 }
12 });
13};
14
15function addModuleIds(data) {
16 var graph = data.graph;
17 var options = data.options;
18
19 // extend each node's load object with an `uniqueId` property
20 // transpile will look for this property to generate the slim module format
21 if (isArray(graph)) {
22 data.graph.forEach(function(node, index) {
23 node.load.uniqueId = index;
24 });
25 } else {
26 keys(graph).forEach(function(name, index) {
27 graph[name].load.uniqueId = index;
28 });
29 }
30
31 // set a normalize hook for transpile so the regular module identifiers are
32 // replaced with the short ids set in `node.load.uniqueId`
33 options.normalize = function(name, load) {
34 var node = load ? graph[load.name] : null;
35 return node ? node.load.uniqueId : name;
36 };
37
38 return data;
39}