1 | var addStealToBundle = require("../bundle/add_steal");
|
2 | var addTraceurRuntime = require("../bundle/add_traceur_runtime");
|
3 | var assign = require("lodash").assign;
|
4 | var bundleDepth = require("../bundle/depth");
|
5 | var findBundles = require("../loader/find_bundle");
|
6 | var flattenBundle = require("../bundle/flatten");
|
7 | var getBundleForOnly = require("../bundle/get_for_only");
|
8 | var hasES6 = require("../graph/has_es6");
|
9 | var isEmpty = require("lodash").isEmpty;
|
10 | var makeBundle = require("../bundle/make_bundle");
|
11 | var makeBundlesConfig = require("../bundle/make_bundles_config");
|
12 | var makeBundleNameMap = require("../bundle/make_bundle_name_map");
|
13 | var markBundlesDirty = require("../bundle/mark_as_dirty");
|
14 | var nameBundle = require("../bundle/name");
|
15 | var order = require("../graph/order");
|
16 | var splitByBuildType = require("../bundle/split_by_build_type");
|
17 | var splitGraphByModuleNames = require("../graph/split");
|
18 | var through = require("through2");
|
19 | var unbundle = require("../graph/unbundle");
|
20 | var winston = require("winston");
|
21 |
|
22 | module.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 |
|
33 | function bundle(data){
|
34 | var dependencyGraph = data.graph,
|
35 | options = data.options,
|
36 | config = data.config,
|
37 |
|
38 | mains = data.mains,
|
39 | configuration = data.configuration,
|
40 | configGraph = data.configGraph;
|
41 |
|
42 |
|
43 |
|
44 |
|
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 |
|
54 |
|
55 | var unbundledGraphs = unbundle(dependencyGraph);
|
56 |
|
57 |
|
58 |
|
59 |
|
60 | var splitGraphs = splitGraphByModuleNames(dependencyGraph, mains),
|
61 | mainsGraph = splitGraphs["with"],
|
62 | bundledGraph = splitGraphs.without;
|
63 |
|
64 | winston.info("Calculating main bundle(s)...");
|
65 |
|
66 | var mainBundles = makeBundle(mainsGraph);
|
67 |
|
68 | winston.info("Flattening main bundle(s)...");
|
69 | flattenBundle(mainBundles, bundleDepth(config, options, true));
|
70 |
|
71 | var splitMainBundles = splitByBuildType(mainBundles);
|
72 |
|
73 | var splitBundles = [];
|
74 | if(!isEmpty(bundledGraph)) {
|
75 | winston.info("Calculating progressively loaded bundle(s)...");
|
76 |
|
77 | var bundles = makeBundle(bundledGraph);
|
78 |
|
79 | winston.info("Flattening progressively loaded bundle(s)...");
|
80 | flattenBundle(bundles, bundleDepth(config, options));
|
81 |
|
82 | splitBundles = splitByBuildType(bundles);
|
83 | }
|
84 |
|
85 |
|
86 |
|
87 |
|
88 | var mainJSBundles = mains.map(function(main){
|
89 | return getBundleForOnly(splitMainBundles, main, "js");
|
90 | });
|
91 |
|
92 |
|
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 |
|
100 | var allBundles = splitMainBundles.concat(splitBundles);
|
101 | allBundles = allBundles.concat.apply(allBundles, unbundledBundles);
|
102 |
|
103 |
|
104 | nameBundle(allBundles);
|
105 |
|
106 |
|
107 |
|
108 | var mainJSBundleNames = makeBundleNameMap(mainJSBundles);
|
109 |
|
110 |
|
111 | mainJSBundles.forEach(function(mainJSBundle){
|
112 | [].unshift.apply(mainJSBundle.nodes, configGraph);
|
113 |
|
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 |
|
129 | if(hasES6(dependencyGraph)) {
|
130 | addTraceurRuntime(mainJSBundle);
|
131 | }
|
132 | });
|
133 |
|
134 |
|
135 |
|
136 | markBundlesDirty(allBundles, data);
|
137 |
|
138 | return assign({}, data, {
|
139 | bundles: allBundles
|
140 | });
|
141 | }
|