UNPKG

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