-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbundle.js
executable file
·103 lines (91 loc) · 3.12 KB
/
bundle.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/usr/bin/env node
/*eslint-env node*/
"use strict";
var FS = require("fs");
var Path = require("path");
var Location = require("./location");
var Identifier = require("./identifier");
var System = require("./system");
var boilerplate = FS.readFileSync(Path.join(__dirname, "boilerplate.js"), "utf-8");
boilerplate = boilerplate.replace(/\s*$/, "");
exports.bundleSystemId = bundleSystemId;
function bundleSystemId(system, id) {
return system.load(id)
.then(function () {
var names = Object.keys(system.modules).sort();
var bundle = [];
var index = 0;
for (var nameIndex = 0; nameIndex < names.length; nameIndex++) {
var name = names[nameIndex];
var module = system.modules[name];
if (module.error) {
throw module.error;
}
if (module.text != null && !module.bundled) {
module.index = index++;
module.bundled = true;
bundle.push(module);
}
}
var payload = "[" + bundle.map(function (module) {
var dependencies = {};
module.dependencies.forEach(function (dependencyId) {
var dependency = module.system.lookup(dependencyId, module.id);
dependencies[dependencyId] = dependency.index;
});
var title = module.filename;
var lead = "\n// ";
var rule = Array(title.length + 1).join("-");
var heading = lead + title + lead + rule + "\n\n";
var text = heading + module.text;
return "[" +
JSON.stringify(module.id) + "," +
JSON.stringify(module.dirname) + "," +
JSON.stringify(Identifier.basename(module.filename)) + "," +
JSON.stringify(dependencies) + "," +
"function (require, exports, module, __filename, __dirname){\n" + text + "\n}" +
"]";
}).join(",") + "]";
var main = system.lookup(id);
return (
"// @generated\n" +
boilerplate +
"(" + payload + ")" +
"(" + JSON.stringify(main.filename) + ")"
);
});
}
exports.bundleLocationId = bundleLocationId;
function bundleLocationId(location, id) {
return System.load(location, {
node: true
}).then(function (buildSystem) {
return System.load(location, {
browser: true,
buildSystem: buildSystem
}).then(function (system) {
return bundleSystemId(system, id);
});
});
}
exports.bundleDirectoryId = bundleDirectoryId;
function bundleDirectoryId(path, id) {
return bundleLocationId(Location.fromDirectory(path), id);
}
exports.bundleFile = bundleFile;
function bundleFile(path) {
return System.findSystemLocationAndModuleId(path)
.then(function (pair) {
return bundleLocationId(pair.location, pair.id);
});
}
exports.main = main;
function main() {
return bundleFile(Path.resolve(process.cwd(), process.argv[2]))
.then(function (bundle) {
process.stdout.write(bundle);
});
}
if (require.main === module) {
main();
}