/
multi.js
135 lines (114 loc) · 3.38 KB
/
multi.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*jshint funcscope:true*/
/*
# lib/build/multi.js
The bundled build works by loading the _main_ module and all of its
dependencies and then all of the `System.bundle` modules
and their dependencies. It makes a dependency graph that looks like:
```js
{
moduleName: Node({
load: Load({
name: moduleName,
source: SOURCE
}),
dependencies: [moduleName],
bundles: [bundleName],
})
}
```
Here's an example:
```js
{
"jquery": {
load: {
name: "jquery",
source: "jQuery = function(){ ... }"
},
dependencies: [],
bundles: ["profile","settings","login", ...]
},
"can/util": {
load: {
name: "can/util",
source: "define(['jquery'], function($){ ... })"
},
dependencies: ["jquery"],
bundles: ["profile","login"]
}
}
```
A `Load` is a ES6 load record.
A `Node` is an object that contains the load and other
useful information. The build tools only write to `Node` to keep `Load` from being changed.
It manipulates this graph and eventually creates "bundle" graphs. Bundle graphs look like:
{
size: 231231,
nodes: [node1, node2, ...],
bundles: [bundleName1, bundleName2]
}
The nodes in those bundles are written to the filesystem.
*/
var pump = require("pump");
var assignDefaultOptions = require("../assign_default_options");
var bundle = require("../stream/bundle");
var createBundleGraphStream = require("../graph/make_graph_with_bundles").createBundleGraphStream;
var createWriteStream = require("../bundle/write_bundles").createWriteStream;
var stealWriteStream = require("../stream/steal");
var continuousBuild = require("./continuous");
var concat = require("../bundle/concat_stream");
var minify = require("../stream/minify");
var transpile = require("../stream/transpile");
var writeBundleManifest = require("../stream/write_bundle_manifest");
var buildType = require("../stream/build_type");
module.exports = function(config, options){
// Use the build-development environment.
if(!options) options = {};
// Watch mode, return a continously building stream.
if(options.watch) {
options = assignDefaultOptions(config, options);
return continuousBuild(config, options);
} else {
// Minification is on by default for stealTools.build
options.minify = options.minify == null ? true : options.minify;
try {
options = assignDefaultOptions(config, options);
} catch(err) {
return Promise.reject(err);
}
// Return a Promise that will resolve after bundles have been written;
return new Promise(function(resolve, reject) {
var writeStream = pump(
createBundleGraphStream(config, options),
buildType("build"),
transpile(),
minify(),
bundle(),
concat(),
createWriteStream(),
stealWriteStream(),
writeBundleManifest(),
function(err) {
// reject the promise if any of the streams fail
if (err) reject(err);
}
);
writeStream.on("data", function(builtResult){
// run external steal-tool plugins after the build
if(options){
var p = Promise.resolve(builtResult);
if(options.bundleAssets){
var bundleAssets = require("steal-bundler");
p = p.then(function(builtResult) {
return bundleAssets(builtResult, options.bundleAssets);
});
}
}
p.then(function (builtResult) {
resolve(builtResult);
}).catch(function (error) {
reject(error);
});
});
});
}
};