UNPKG

5.16 kBJavaScriptView Raw
1var addStealToBundle = require("../bundle/add_steal");
2var addTraceurRuntime = require("../bundle/add_traceur_runtime");
3var assign = require("lodash").assign;
4var bundleDepth = require("../bundle/depth");
5var findBundles = require("../loader/find_bundle");
6var flattenBundle = require("../bundle/flatten");
7var getBundleForOnly = require("../bundle/get_for_only");
8var hasES6 = require("../graph/has_es6");
9var isEmpty = require("lodash").isEmpty;
10var makeBundle = require("../bundle/make_bundle");
11var makeBundlesConfig = require("../bundle/make_bundles_config");
12var makeBundleNameMap = require("../bundle/make_bundle_name_map");
13var markBundlesDirty = require("../bundle/mark_as_dirty");
14var nameBundle = require("../bundle/name");
15var order = require("../graph/order");
16var splitByBuildType = require("../bundle/split_by_build_type");
17var splitGraphByModuleNames = require("../graph/split");
18var through = require("through2");
19var unbundle = require("../graph/unbundle");
20var winston = require("winston");
21
22module.exports = function(){
23 return through.obj(function(data, enc, done){
24 try {
25 var result = bundle(data);
26 done(null, result);
27 } catch(err) {
28 done(err);
29 }
30 });
31};
32
33function bundle(data){
34 var dependencyGraph = data.graph,
35 options = data.options,
36 config = data.config,
37 // the apps we are building
38 mains = data.mains,
39 configuration = data.configuration,
40 configGraph = data.configGraph;
41
42 // Adds an `order` property to each `Node` so we know which modules.
43 // The lower the number the lower on the dependency tree it is.
44 // For example, jQuery might have `order: 0`.
45 mains.forEach(function(moduleName){
46 order(dependencyGraph, moduleName);
47 });
48
49 findBundles(data.loader).forEach(function(moduleName){
50 order(dependencyGraph, moduleName);
51 });
52
53 // Get unbundled graphs, these are graphs of modules that are marked
54 // to not be bundled, they will be put into their own bundles later.
55 var unbundledGraphs = unbundle(dependencyGraph);
56
57 // Split the graph into two smaller graphs. One will contain the
58 // "mains" and their dependencies that need to be loaded right away.
59 // The other will will be the bundles that are progressively loaded.
60 var splitGraphs = splitGraphByModuleNames(dependencyGraph, mains),
61 mainsGraph = splitGraphs["with"],
62 bundledGraph = splitGraphs.without;
63
64 winston.info("Calculating main bundle(s)...");
65 // Put everything into unique bundle graphs that have no waste.
66 var mainBundles = makeBundle(mainsGraph);
67 // Combine bundles to reduce the number of total bundles that will need to be loaded
68 winston.info("Flattening main bundle(s)...");
69 flattenBundle(mainBundles, bundleDepth(config, options, true));
70 // Break up bundles by buildType
71 var splitMainBundles = splitByBuildType(mainBundles);
72
73 var splitBundles = [];
74 if(!isEmpty(bundledGraph)) {
75 winston.info("Calculating progressively loaded bundle(s)...");
76 // Put everything into unique bundle graphs that have no waste.
77 var bundles = makeBundle(bundledGraph);
78 // Combine bundles to reduce the number of total bundles that will need to be loaded
79 winston.info("Flattening progressively loaded bundle(s)...");
80 flattenBundle(bundles, bundleDepth(config, options));
81 // Break up bundles by buildType
82 splitBundles = splitByBuildType(bundles);
83 }
84
85 // Every mainBundle needs to have @config and bundle configuration to know
86 // where everything is. Lets get those main bundles here while there is less to go through.
87
88 var mainJSBundles = mains.map(function(main){
89 return getBundleForOnly(splitMainBundles, main, "js");
90 });
91
92 // Create unbundled bundles, for modules marked as sideBundle: true
93 var unbundledBundles = unbundledGraphs.reduce(function(bundles, graph){
94 bundles.push.apply(bundles, makeBundle(graph));
95 return bundles;
96 }, []);
97 unbundledBundles = splitByBuildType(unbundledBundles);
98
99 // Combine all bundles
100 var allBundles = splitMainBundles.concat(splitBundles);
101 allBundles = allBundles.concat.apply(allBundles, unbundledBundles);
102
103 // Name each bundle so we know what to call the bundle.
104 nameBundle(allBundles);
105
106 // Create a lookup object of the main bundle names so that they are
107 // excluded from the Bundles config
108 var mainJSBundleNames = makeBundleNameMap(mainJSBundles);
109
110 // Add @config and the bundleConfigNode to each main
111 mainJSBundles.forEach(function(mainJSBundle){
112 [].unshift.apply(mainJSBundle.nodes, configGraph);
113 // Make config JS code so System knows where to look for bundles.
114 var configNode = makeBundlesConfig(allBundles, configuration,
115 mainJSBundle, {
116 excludedBundles: mainJSBundleNames
117 });
118 mainJSBundle.nodes.unshift(configNode);
119
120 if(options.bundleSteal) {
121 addStealToBundle({
122 bundle: mainJSBundle,
123 main: mainJSBundle.bundles[0],
124 configuration: configuration,
125 bundlePromisePolyfill: options.bundlePromisePolyfill
126 });
127 }
128 // Traceur code requires a runtime.
129 if(hasES6(dependencyGraph)) {
130 addTraceurRuntime(mainJSBundle);
131 }
132 });
133
134 // Mark bundles that are dirty so that they will be written to the file
135 // system.
136 markBundlesDirty(allBundles, data);
137
138 return assign({}, data, {
139 bundles: allBundles
140 });
141}