UNPKG

2 kBJavaScriptView Raw
1var path = require("path");
2var concat = require("../source-map-concat");
3var sourceNode = require("../node/source").node;
4var removeSourceMapUrl = require("../remove_source_map_url");
5
6module.exports = function(bundle, options){
7 var sourceProp = options.sourceProp;
8 var excludePlugins = options.excludePlugins;
9
10 var output = fileName(bundle);
11
12 var makeCode = function(name) {
13 return "define('" + name + "', [], function(){ return {}; });";
14 };
15
16 var nodes = bundle.nodes.map(function(node){
17 if (node.load.metadata &&
18 node.load.metadata.hasOwnProperty('bundle') &&
19 node.load.metadata.bundle === false) {
20
21 return { node: node, code: "", map: "" };
22 }
23
24 // Allow some nodes to be completely excluded
25 if(node.load.excludeFromBuild) {
26 return undefined;
27 }
28
29 // for plugins, include them only if they define `includeInBuild`
30 // or if the module's metadata has `includeInBuild` set to `true`
31 if (node.isPlugin &&
32 !node.value.includeInBuild &&
33 !node.load.metadata.includeInBuild) {
34
35 var code = excludePlugins ? "" : makeCode(node.load.name);
36 return { node: node, code: code };
37 }
38
39 var source = sourceNode(node, sourceProp);
40
41 return {
42 node: node,
43 code: removeSourceMapUrl((source.code || "") + ""),
44 map: source.map
45 };
46 }).filter(truthy);
47
48 var concatenated = concat(nodes, {
49 mapPath: output + ".map",
50 delimiter: "\n",
51 process: prependName
52 });
53
54 var result = concatenated.toStringWithSourceMap({
55 file: path.basename(output)
56 });
57
58 bundle.source = result;
59
60 function prependName(node, file) {
61 var load = file.node.load;
62 var prepend = node.prependModuleName !== false && load.name &&
63 load.metadata.prependModuleName !== false;
64
65 if(prepend) {
66 node.prepend("/*"+file.node.load.name+"*/\n");
67 }
68 }
69};
70
71function fileName(bundle) {
72 var name = bundle.name || bundle.bundles[0] || bundle.nodes[0].load.name;
73 return name .replace("bundles/", "").replace(/\..+!/, "") + "." + bundle.buildType;
74}
75
76function truthy(x) { return !!x; }