UNPKG

1.21 kBJavaScriptView Raw
1
2var makeNode = require("../node/make_node"),
3 minify = require("../graph/minify"),
4 fs = require("fs"),
5 path = require("path"),
6 prettier = require("prettier");
7
8// makes it so this bundle loads steal
9module.exports = function(bundle, options){
10 var exports = options.exports; // ? exportsForDependencies(bundle.nodes, options.exports) : {};
11 var shim = fs.readFileSync(path.join(__dirname, "shim.js"));
12 var shimEnd = fs.readFileSync(path.join(__dirname, "shim-end.js"));
13 var shimEval = fs.readFileSync(path.join(__dirname, "shim-eval.js"));
14
15 // target global variable, self (Web Workers) or window
16 var g = `typeof self == "object" && self.Object == Object ? self : (typeof process === "object" && Object.prototype.toString.call(process) === "[object process]") ? global : window`;
17
18 var source = prettier.format(
19 `(${shim.toString()})(
20 ${JSON.stringify(exports)},
21 ${g},
22 ${shimEval.toString()}
23 );`,
24 { useTabs: true }
25 );
26
27 var start = makeNode("[global-shim-start]", source);
28 source = `(${shimEnd.toString()})(${g});`;
29 var end = makeNode("[global-shim-end]", source);
30
31 if(options.minify){
32 minify([start]);
33 minify([end]);
34 }
35 bundle.nodes.unshift(start);
36 bundle.nodes.push(end);
37};