Skip to content

Commit 80f1928

Browse files
committed
Switch to cjs bundler
1 parent 5f6272c commit 80f1928

File tree

4 files changed

+109
-102
lines changed

4 files changed

+109
-102
lines changed

.gitmodules

+3
Original file line numberDiff line numberDiff line change
@@ -95,3 +95,6 @@
9595
ref = refs/heads/master
9696
prefix = hyhoryto-jekx0x
9797
path = lib/culvert
98+
[submodule "lib/wheaty-cjs-bundler"]
99+
path = lib/wheaty-cjs-bundler
100+
url = [email protected]:creationix/wheaty-cjs-bundler.git

build/web/tedit-old.js

+104
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
#!js
2+
3+
4+
var pathJoin = require('path').join;
5+
var bodec = require('bodec');
6+
var mine = require('mine');
7+
var modes = require('js-git/lib/modes');
8+
9+
function wrapper(name) {
10+
var modules = {};
11+
var defs = {/*DEFS*/};
12+
window.require = require;
13+
require(/*MAIN*/);
14+
function require(filename) {
15+
var module = modules[filename];
16+
if (module) return module.exports;
17+
module = modules[filename] = {exports:{}};
18+
var dirname = filename.substring(0, filename.lastIndexOf("/"));
19+
var def = defs[filename];
20+
if (!def) throw new Error("No such module: " + filename);
21+
def(module, module.exports, dirname, filename);
22+
return module.exports;
23+
}
24+
}
25+
26+
module.exports = function* (pathToEntry) {
27+
28+
var started = {};
29+
var js = "";
30+
var main = "src/main-web.js";
31+
32+
yield* load(main);
33+
34+
js = "(" +
35+
wrapper.toString()
36+
.replace("/*DEFS*/", js)
37+
.replace("/*MAIN*/", JSON.stringify(main)) +
38+
"());\n";
39+
40+
return [200, {"Content-Type":"application/javascript"}, js];
41+
42+
function* load(path) {
43+
if (started[path]) return;
44+
started[path] = true;
45+
var meta = yield* pathToEntry(path);
46+
if (!meta) throw new Error("No such file: " + path);
47+
var blob = yield meta.repo.loadAs("blob", meta.hash);
48+
var code = bodec.toUnicode(blob);
49+
var deps = mine(code);
50+
var base = pathJoin(path, "..");
51+
for (var i = deps.length - 1; i >= 0; --i) {
52+
var dep = deps[i];
53+
var depName = dep.name;
54+
if (depName[0] === ".") {
55+
depName = yield* findLocal(pathJoin(base, depName));
56+
}
57+
else {
58+
depName = yield* findModule(base, depName);
59+
}
60+
if (depName) {
61+
yield* load(depName);
62+
var offset = dep.offset;
63+
code = code.substring(0, offset) +
64+
depName +
65+
code.substring(offset + dep.name.length);
66+
}
67+
}
68+
js += JSON.stringify(path) +
69+
": function (module, exports, __dirname, __filename) {" +
70+
code + "},\n";
71+
}
72+
73+
function* findLocal(path) {
74+
var meta = yield* pathToEntry(path);
75+
if (meta) {
76+
// Exact match! Happy days.
77+
if (modes.isFile(meta.mode)) return path;
78+
if (meta.mode !== modes.tree) return;
79+
// Maybe it's a module with a package.json?
80+
var pkgPath = pathJoin(path, "package.json");
81+
meta = yield* pathToEntry(pkgPath);
82+
if (meta && modes.isFile(meta.mode)) {
83+
var json = yield meta.repo.loadAs("text", meta.hash);
84+
var pkgInfo = JSON.parse(json);
85+
if (pkgInfo.main) {
86+
return yield* findLocal(pathJoin(path, pkgInfo.main));
87+
}
88+
}
89+
var idxPath = pathJoin(path, "index.js");
90+
meta = yield* pathToEntry(idxPath);
91+
if (meta && modes.isFile(meta.mode)) return idxPath;
92+
}
93+
// Maybe they forgot the extension?
94+
path = path + ".js";
95+
meta = yield* pathToEntry(path);
96+
if (meta && modes.isFile(meta.mode)) return path;
97+
}
98+
99+
function* findModule(base, name) {
100+
return (yield* findLocal(pathJoin("src", name))) ||
101+
(yield* findLocal(pathJoin("lib", name)));
102+
}
103+
104+
};

build/web/tedit.js

+1-102
Original file line numberDiff line numberDiff line change
@@ -1,104 +1,3 @@
11
#!js
22

3-
4-
var pathJoin = require('path').join;
5-
var bodec = require('bodec');
6-
var mine = require('mine');
7-
var modes = require('js-git/lib/modes');
8-
9-
function wrapper(name) {
10-
var modules = {};
11-
var defs = {/*DEFS*/};
12-
window.require = require;
13-
require(/*MAIN*/);
14-
function require(filename) {
15-
var module = modules[filename];
16-
if (module) return module.exports;
17-
module = modules[filename] = {exports:{}};
18-
var dirname = filename.substring(0, filename.lastIndexOf("/"));
19-
var def = defs[filename];
20-
if (!def) throw new Error("No such module: " + filename);
21-
def(module, module.exports, dirname, filename);
22-
return module.exports;
23-
}
24-
}
25-
26-
module.exports = function* (pathToEntry) {
27-
28-
var started = {};
29-
var js = "";
30-
var main = "src/main-web.js";
31-
32-
yield* load(main);
33-
34-
js = "(" +
35-
wrapper.toString()
36-
.replace("/*DEFS*/", js)
37-
.replace("/*MAIN*/", JSON.stringify(main)) +
38-
"());\n";
39-
40-
return [200, {"Content-Type":"application/javascript"}, js];
41-
42-
function* load(path) {
43-
if (started[path]) return;
44-
started[path] = true;
45-
var meta = yield* pathToEntry(path);
46-
if (!meta) throw new Error("No such file: " + path);
47-
var blob = yield meta.repo.loadAs("blob", meta.hash);
48-
var code = bodec.toUnicode(blob);
49-
var deps = mine(code);
50-
var base = pathJoin(path, "..");
51-
for (var i = deps.length - 1; i >= 0; --i) {
52-
var dep = deps[i];
53-
var depName = dep.name;
54-
if (depName[0] === ".") {
55-
depName = yield* findLocal(pathJoin(base, depName));
56-
}
57-
else {
58-
depName = yield* findModule(base, depName);
59-
}
60-
if (depName) {
61-
yield* load(depName);
62-
var offset = dep.offset;
63-
code = code.substring(0, offset) +
64-
depName +
65-
code.substring(offset + dep.name.length);
66-
}
67-
}
68-
js += JSON.stringify(path) +
69-
": function (module, exports, __dirname, __filename) {" +
70-
code + "},\n";
71-
}
72-
73-
function* findLocal(path) {
74-
var meta = yield* pathToEntry(path);
75-
if (meta) {
76-
// Exact match! Happy days.
77-
if (modes.isFile(meta.mode)) return path;
78-
if (meta.mode !== modes.tree) return;
79-
// Maybe it's a module with a package.json?
80-
var pkgPath = pathJoin(path, "package.json");
81-
meta = yield* pathToEntry(pkgPath);
82-
if (meta && modes.isFile(meta.mode)) {
83-
var json = yield meta.repo.loadAs("text", meta.hash);
84-
var pkgInfo = JSON.parse(json);
85-
if (pkgInfo.main) {
86-
return yield* findLocal(pathJoin(path, pkgInfo.main));
87-
}
88-
}
89-
var idxPath = pathJoin(path, "index.js");
90-
meta = yield* pathToEntry(idxPath);
91-
if (meta && modes.isFile(meta.mode)) return idxPath;
92-
}
93-
// Maybe they forgot the extension?
94-
path = path + ".js";
95-
meta = yield* pathToEntry(path);
96-
if (meta && modes.isFile(meta.mode)) return path;
97-
}
98-
99-
function* findModule(base, name) {
100-
return (yield* findLocal(pathJoin("src", name))) ||
101-
(yield* findLocal(pathJoin("lib", name)));
102-
}
103-
104-
};
3+
module.exports = require('../../lib/wheaty-cjs-bundler')("src/main-web.js", ["src","lib"]);

lib/wheaty-cjs-bundler

Submodule wheaty-cjs-bundler added at 93278b5

0 commit comments

Comments
 (0)