1 | const { dirname, extname, relative, isAbsolute } = require("path");
|
2 | const { existsSync, readFileSync, writeFileSync, unlinkSync } = require("fs");
|
3 | const { execSync } = require("child_process");
|
4 | const mkdirp = path => execSync(`mkdir -p ${JSON.stringify(path)}`) && path;
|
5 | const JSONPreamble = "function(exports, require, module) { module.exports = ";
|
6 | const JSONPostamble = "\n}";
|
7 | const MUIDStore = require("./muid-store");
|
8 | const hasOwnProperty = Object.prototype.hasOwnProperty;
|
9 | const bootstrapPath = require.resolve("./bootstrap");
|
10 |
|
11 |
|
12 | module.exports = function concatenate({ bundle, root, destination })
|
13 | {
|
14 | if (existsSync(destination))
|
15 | unlinkSync(destination);
|
16 |
|
17 | mkdirp(dirname(destination));
|
18 |
|
19 | const output = { buffers:[], length:0 };
|
20 | const { entrypoint, compilations } = bundle;
|
21 |
|
22 |
|
23 | append("(function (global) {");
|
24 | append(readFileSync(bootstrapPath));
|
25 | append("(");
|
26 | append(entrypoint + ",");
|
27 |
|
28 | const files = bundle.files
|
29 | .map(({ filename, outputIndex, dependencies }) =>
|
30 | [filename, outputIndex, dependencies]);
|
31 |
|
32 | append(JSON.stringify(files));
|
33 | append(", [");
|
34 |
|
35 | for (const output of bundle.outputs)
|
36 | {
|
37 | const isJSON = extname(output) === ".json";
|
38 |
|
39 | if (isJSON)
|
40 | append(JSONPreamble);
|
41 |
|
42 | append(readFileSync(output));
|
43 |
|
44 | if (isJSON)
|
45 | append(JSONPostamble);
|
46 |
|
47 | append(",");
|
48 | }
|
49 |
|
50 | append("]) })(window)");
|
51 |
|
52 | const concated = Buffer.concat(output.buffers, output.length);
|
53 |
|
54 | writeFileSync(destination, concated);
|
55 |
|
56 |
|
57 |
|
58 | function append(content)
|
59 | {
|
60 | if (typeof content === "string")
|
61 | return append(Buffer.from(content, "utf-8"));
|
62 |
|
63 | output.buffers.push(content);
|
64 | output.length += content.length;
|
65 | }
|
66 |
|
67 | function derooted(path)
|
68 | {
|
69 | return isAbsolute(path) ? "/" + relative(root, path) : path
|
70 | }
|
71 | }
|
72 |
|