1 | var winston = require('winston'),
|
2 | order = require("../graph/order"),
|
3 | transpile = require("../graph/transpile"),
|
4 | minifyGraph = require("../graph/minify"),
|
5 | pluck = require("../graph/pluck"),
|
6 | makeBundle = require("../bundle/make_bundle"),
|
7 | nameBundle = require("../bundle/name"),
|
8 | flattenBundle = require("../bundle/flatten"),
|
9 | makeBundlesConfig = require("../bundle/make_bundles_config"),
|
10 | splitByBuildType = require("../bundle/split_by_build_type"),
|
11 | addStealToBundle = require("../bundle/add_steal"),
|
12 | _ = require("lodash"),
|
13 | hasES6 = require("../graph/has_es6"),
|
14 | clean = require("../graph/clean"),
|
15 | addTraceurRuntime = require("../bundle/add_traceur_runtime"),
|
16 | makeConfiguration = require("../configuration/make"),
|
17 | splitGraphByModuleNames = require("../graph/split"),
|
18 | getBundleForOnly = require("../bundle/get_for_only"),
|
19 | findBundles = require("../loader/find_bundle"),
|
20 | through = require("through2"),
|
21 | bundleDepth = require("../bundle/depth"),
|
22 | markBundlesDirty = require("../bundle/mark_as_dirty"),
|
23 | unbundle = require("../graph/unbundle");
|
24 |
|
25 |
|
26 | module.exports = function(){
|
27 |
|
28 | return through.obj(function(data, enc, done){
|
29 | try {
|
30 | var buildResult = multiBuild(data);
|
31 | done(null, buildResult);
|
32 | } catch(err) {
|
33 | done(err);
|
34 | }
|
35 | });
|
36 |
|
37 | function multiBuild(data){
|
38 | var dependencyGraph = data.graph,
|
39 | options = data.options,
|
40 | config = data.config,
|
41 |
|
42 | mains = Array.isArray(config.main) ? config.main.slice(0) : [data.loader.main],
|
43 | configuration = makeConfiguration(data.loader, data.buildLoader, options);
|
44 |
|
45 |
|
46 |
|
47 | var stealconfig = pluck(dependencyGraph,data.loader.configMain ||"@config");
|
48 |
|
49 |
|
50 | options.sourceMapPath = configuration.bundlesPath;
|
51 | transpile(stealconfig, "amd", options, data);
|
52 |
|
53 |
|
54 | pluck(dependencyGraph,"@dev");
|
55 |
|
56 |
|
57 |
|
58 |
|
59 | mains.forEach(function(moduleName){
|
60 | order(dependencyGraph, moduleName);
|
61 | });
|
62 |
|
63 | findBundles(data.loader).forEach(function(moduleName){
|
64 | order(dependencyGraph, moduleName);
|
65 | });
|
66 |
|
67 |
|
68 | if(options.removeDevelopmentCode) {
|
69 | clean(dependencyGraph, options);
|
70 | }
|
71 |
|
72 |
|
73 |
|
74 | winston.info('Transpiling...');
|
75 | transpile(dependencyGraph, "amd", options, data);
|
76 |
|
77 |
|
78 | if(options.minify) {
|
79 | winston.info('Minifying...');
|
80 | minifyGraph(dependencyGraph, options);
|
81 | minifyGraph(stealconfig, options);
|
82 | }
|
83 |
|
84 |
|
85 |
|
86 | var unbundledGraphs = unbundle(dependencyGraph);
|
87 |
|
88 |
|
89 |
|
90 |
|
91 | var splitGraphs = splitGraphByModuleNames(dependencyGraph, mains),
|
92 | mainsGraph = splitGraphs["with"],
|
93 | bundledGraph = splitGraphs.without;
|
94 |
|
95 | winston.info('Calculating main bundle(s)...');
|
96 |
|
97 | var mainBundles = makeBundle(mainsGraph);
|
98 |
|
99 | winston.info('Flattening main bundle(s)...');
|
100 | flattenBundle(mainBundles, bundleDepth(config, options, true));
|
101 |
|
102 | var splitMainBundles = splitByBuildType(mainBundles);
|
103 |
|
104 | var splitBundles = [];
|
105 | if(!_.isEmpty(bundledGraph)) {
|
106 | winston.info('Calculating progressively loaded bundle(s)...');
|
107 |
|
108 | var bundles = makeBundle(bundledGraph);
|
109 |
|
110 | winston.info('Flattening progressively loaded bundle(s)...');
|
111 | flattenBundle(bundles, bundleDepth(config, options));
|
112 |
|
113 | splitBundles = splitByBuildType(bundles);
|
114 | }
|
115 |
|
116 |
|
117 |
|
118 |
|
119 | var mainJSBundles = mains.map(function(main){
|
120 | return getBundleForOnly(splitMainBundles,main,"js");
|
121 | });
|
122 |
|
123 |
|
124 | var unbundledBundles = unbundledGraphs.reduce(function(bundles, graph){
|
125 | bundles.push.apply(bundles, makeBundle(graph));
|
126 | return bundles;
|
127 | }, []);
|
128 | unbundledBundles = splitByBuildType(unbundledBundles);
|
129 |
|
130 |
|
131 | var allBundles = splitMainBundles.concat(splitBundles);
|
132 | allBundles = allBundles.concat.apply(allBundles, unbundledBundles);
|
133 |
|
134 |
|
135 | nameBundle(allBundles);
|
136 |
|
137 |
|
138 |
|
139 | var mainJSBundleNames = {};
|
140 | mainJSBundles.forEach(function(mainJSBundle){
|
141 | mainJSBundleNames[mainJSBundle.name] = true;
|
142 | });
|
143 |
|
144 |
|
145 | mainJSBundles.forEach(function(mainJSBundle){
|
146 | [].unshift.apply(mainJSBundle.nodes, stealconfig );
|
147 |
|
148 | var configNode = makeBundlesConfig(allBundles, configuration, mainJSBundle, {
|
149 | excludedBundles: mainJSBundleNames
|
150 | });
|
151 | mainJSBundle.nodes.unshift(configNode);
|
152 |
|
153 | if(options.bundleSteal) {
|
154 | addStealToBundle({
|
155 | bundle: mainJSBundle,
|
156 | main: mainJSBundle.bundles[0],
|
157 | configuration: configuration,
|
158 | bundlePromisePolyfill: options.bundlePromisePolyfill
|
159 | });
|
160 | }
|
161 |
|
162 | var hasES6modules = hasES6(dependencyGraph);
|
163 | if( hasES6modules ) {
|
164 | addTraceurRuntime(mainJSBundle);
|
165 | }
|
166 | });
|
167 |
|
168 |
|
169 |
|
170 | markBundlesDirty(allBundles, data);
|
171 |
|
172 | return _.extend({}, data, {
|
173 | bundles: allBundles,
|
174 | configuration: configuration
|
175 | });
|
176 | }
|
177 | };
|