UNPKG

2.32 kBJavaScriptView Raw
1var util = require("util");
2var defaults = require("lodash/defaults");
3
4/**
5 * @param {Object} bundles
6 * @param {Object} configuration
7 * @param {Object} targetBundle the bundle this config is being built for.
8 * @param {Object} options Additional options to select what bundles to include.
9 */
10module.exports = function(bundles, configuration, targetBundle, options) {
11 var paths = getBundlesPaths(configuration);
12
13 if (!options) options = {};
14 defaults(options, {
15 // return the bundle name to be used in the System.bundles object.
16 // provide a custom function if you need to change the bundle name
17 // used during runtime. E.g: development bundles need this when a
18 // custom `dest` is provided; unlike production environments `bundlesPath`
19 // does not work.
20 getBundleName(bundle) {
21 return bundle.name;
22 }
23 });
24
25 var excludedBundles = options.excludedBundles || {};
26 var bundledBundles = bundles.slice(0);
27
28 // whether the bundle name matches the target bundle name
29 function isTargetBundle(bundle) {
30 return bundle.name === targetBundle.name;
31 }
32
33 var bundlesConfig = {};
34 bundledBundles.forEach(function(bundle) {
35 // do not add the target bundle as an entry to System.bundles, this
36 // causes an infinite recursion when the bundle tries to load itself
37 if(!isTargetBundle(bundle) && !excludedBundles[bundle.name]) {
38 var name = options.getBundleName(bundle);
39 bundlesConfig[name] = bundle.nodes.map(function(node){
40 return node.load.name;
41 });
42 }
43 });
44
45 return {
46 load: {
47 name: "[system-bundles-config]",
48 metadata: {
49 bundlesConfig: bundlesConfig
50 }
51 },
52 minifiedSource: paths + "System.bundles = "+JSON.stringify(bundlesConfig)+";"
53 };
54};
55
56// Get the System.paths needed to map bundles, if a different bundlesPath is provided.
57function getBundlesPaths(configuration){
58 // If a bundlesPath is not provided, the paths are not needed because they are
59 // already set up in steal.js
60 if(!configuration.loader.bundlesPath) {
61 return "";
62 }
63 var bundlesPath = configuration.bundlesPathURL;
64
65 // Get the dist directory and set the paths output
66 var paths = util.format('\tSystem.paths["bundles/*.css"] ="%s/*css";\n' +
67 '\tSystem.paths["bundles/*"] = "%s/*.js";\n',
68 bundlesPath, bundlesPath);
69 return "if(!System.bundlesPath) {\n" + paths + "}\n";
70}