/
slim_graph.js
205 lines (178 loc) · 5.04 KB
/
slim_graph.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
var omit = require("lodash/omit");
var first = require("lodash/first");
var negate = require("lodash/negate");
var concat = require("lodash/concat");
var assign = require("lodash/assign");
var partial = require("lodash/partial");
var includes = require("lodash/includes");
var isJavaScriptBundle = require("../bundle/is_js_bundle");
var makeSlimShimNode = require("../node/make_slim_shim_node");
var makeSlimBundleNode = require("../node/make_slim_bundle_node");
var makeSlimConfigNode = require("../node/make_slim_config_node");
module.exports = function(options) {
var slimmedBundles = [];
if (isMultiMain(options.mains) && options.splitLoader) {
throw new Error(
[
`Cannot create slim buid; "splitLoader" is not supported in multi main apps`,
`Please, set "splitLoader" to false and try again`
].join("\n")
);
}
// filter out nodes flagged to be removed from the build and config nodes
options.bundles.forEach(function(bundle) {
bundle.nodes = bundle.nodes.filter(function(node) {
return (
!isConfigNode(options.configMain, node) &&
!isExcludedFromBuild(node)
);
});
});
var jsBundles = options.bundles.filter(isJavaScriptBundle);
var nonJsBundles = options.bundles.filter(negate(isJavaScriptBundle));
var slimConfigNode = makeSlimConfigNode({
target: options.target,
graph: options.graph,
baseUrl: options.baseUrl,
bundles: options.bundles, // the config needs to receive all the bundles
slimConfig: options.slimConfig,
bundlesPath: options.bundlesPath,
progressiveBundles: options.progressiveBundles
});
if (options.splitLoader) {
var loaderBundle = concat(
makeLoaderBundle([
slimConfigNode,
makeSlimShimNode({
nodes: [],
splitLoader: true,
progressive: true,
sharedBundles: [],
target: options.target,
slimConfig: options.slimConfig,
plugins: !!nonJsBundles.length,
mainModuleId: getMainModuleId(options.graph, options.mains[0])
})
])
);
slimmedBundles = concat(
loaderBundle,
jsBundles.map(partial(toSlimBundle, options.target)),
nonJsBundles
);
} else {
var getMainName = function(bundle) {
return first(bundle.bundles);
};
var isEntryPointBundle = function(bundle) {
return (
bundle.bundles.length === 1 &&
includes(options.mains, getMainName(bundle))
);
};
var entryPointBundles = jsBundles.filter(isEntryPointBundle);
var secondaryBundles = jsBundles.filter(negate(isEntryPointBundle));
// each entry point bundle includes the loader code
var slimmedEntryBundles = entryPointBundles.map(function(bundle) {
var mainName = getMainName(bundle);
var sharedBundles = getSharedBundlesOf(
jsBundles.filter(function(b) {
return b.name !== bundle.name;
}),
mainName
);
var nodes = [
slimConfigNode,
makeSlimShimNode({
nodes: bundle.nodes.slice(0),
target: options.target,
sharedBundles: sharedBundles,
plugins: !!nonJsBundles.length,
slimConfig: options.slimConfig,
progressive: !!secondaryBundles.length,
mainModuleId: getMainModuleId(options.graph, mainName)
})
];
return assign({}, omit(bundle, ["nodes"]), {
nodes: nodes
});
});
slimmedBundles = concat(
slimmedEntryBundles,
secondaryBundles.map(partial(toSlimBundle, options.target)),
nonJsBundles
);
}
return slimmedBundles;
};
/**
* Whether there are multiple mains to be built
* @param {Array} mains
* @return {boolean}
*/
function isMultiMain(mains) {
return mains.length > 1;
}
/**
* Return the main module slim id
* @param {Object} graph
* @param {string} mainName
* @return {number} The slim id
*/
function getMainModuleId(graph, mainName) {
return graph[mainName].load.uniqueId;
}
function getSharedBundlesOf(bundles, mainName) {
return bundles.filter(function(b) {
return includes(b.bundles, mainName);
});
}
function toSlimBundle(target, bundle) {
return assign({}, omit(bundle, ["nodes"]), {
nodes: [makeSlimBundleNode(target, bundle)]
});
}
function makeLoaderBundle(nodes) {
var getBundleName = require("../bundle/name").getName;
var bundle = {
bundles: ["loader"],
buildType: "js",
nodes: nodes
};
bundle.name = getBundleName(bundle);
return bundle;
}
/**
* Whether the node is flagged to be removed from the build
* @param {Object} node - A node from a bundle
* @return {boolean} `true` if flagged, `false` otherwise
*/
function isExcludedFromBuild(node) {
if (node.load.excludeFromBuild) {
return true;
}
if (
node.load.metadata &&
node.load.metadata.hasOwnProperty("bundle") &&
node.load.metadata.bundle === false
) {
return true;
}
if (
node.isPlugin &&
!node.value.includeInBuild &&
!node.load.metadata.includeInBuild
) {
return true;
}
return false;
}
/**
* Whether the node is a config module (like package.json)
* @param {string} configMain - The configMain module identifier
* @param {Object} node - A node from a bundle
* @return {boolean} `true` if a config node, `false` otherwise
*/
function isConfigNode(configMain, node) {
return includes([configMain, "[system-bundles-config]"], node.load.name);
}