UNPKG

1.87 kBJavaScriptView Raw
1var omit = require("lodash/omit");
2var through = require("through2");
3var assign = require("lodash/assign");
4var slimGraph = require("../graph/slim_graph");
5
6/**
7 * @param {Object} options - An options object
8 */
9module.exports = function(options) {
10 var opts = options != null ? options : {};
11
12 return through.obj(function(data, enc, next) {
13 try {
14 next(null, doSlimGrap(data, opts));
15 } catch (err) {
16 next(err);
17 }
18 });
19};
20
21/**
22 * Turns the bundles into their slim version (mutates stream data)
23 * @param {Object} data - The slim stream data object
24 * @param {Object} options - An options object
25 * @return {Object} The mutated data
26 */
27function doSlimGrap(data, options) {
28 var bundles = data.bundles.slice(0);
29
30 var slimmedBundles = slimGraph({
31 graph: data.graph,
32 mains: data.mains,
33 bundles: bundles,
34 target: options.target,
35 baseUrl: data.loader.baseURL,
36 slimConfig: data.loader.slimConfig,
37 splitLoader: data.options.splitLoader,
38 bundlesPath: data.configuration.bundlesPath,
39 configMain: data.loader.configMain || "package.json!npm",
40 progressiveBundles: getProgressiveBundles(data.loader, data.graph)
41 });
42
43 return assign(
44 {},
45 omit(data, ["bundles"]),
46 { bundles: slimmedBundles }
47 );
48}
49
50
51/**
52 * An array of module names/ids to be progressively loaded
53 * @param {Object} loader - The loader instance
54 * @param {Object} graph - The dependency graph
55 * @return {Array.<number, string>} List of module names and ids
56 */
57function getProgressiveBundles(loader, graph) {
58 var config = loader.__loaderConfig || {};
59 var configBundle = config.bundle || [];
60
61 return loader.bundle.map(function(name, index) {
62 return {
63 id: graph[name].load.uniqueId,
64
65 // use the raw module identifiers so the user won't have to use
66 // the full normalized names when importing a module dynamically
67 name: configBundle[index] || name
68 };
69 });
70}