Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Include scripts to automate bundling modules for the web

  • Loading branch information...
commit ef089d66282e23a9010b1006bbd57bc2b2453542 1 parent 1b96353
@zaach authored
View
29 Jakefile
@@ -1,29 +0,0 @@
-#!/usr/bin/env narwhal
-
-var FILE = require("file"),
- OS = require("os"),
- jake = require("jake");
-
-var path = FILE.path(FILE.cwd());
-
-jake.task("build:web", function() {
- var bundler = require("cjs-bundler"),
- script = bundler.bundle([
- { id : "jison", path : path.join("lib", "jison.js") },
- { id : "jison/lexer", path : path.join("lib", "jison", "lexer.js") },
- { id : "jison/bnf", path : path.join("lib", "jison", "bnf.js") },
- { id : "jison/jisonlex", path : path.join("lib", "jison", "jisonlex.js") },
- { id : "jison/json2jison", path : path.join("lib", "jison", "json2jison.js") },
- { id : "jison/util/set", path : path.join("lib", "jison", "util", "set.js") },
- { id : "jison/util/typal", path : path.join("lib", "jison", "util", "typal.js") },
- { id : "jison/util/bnf-parser", path : path.join("lib", "jison", "util", "bnf-parser.js") },
- { id : "jison/util/lex-parser", path : path.join("lib", "jison", "util", "lex-parser.js") }
- ]);
-
- FILE.write("web/content/assets/js/jison.js", "var require = (function() {\n" + script + ";\nreturn require;\n})();");
- OS.system(['make', 'web']);
-});
-
-jake.task("preview", function () {
- OS.system(['make', 'preview']);
-});
View
3  Makefile
@@ -2,8 +2,9 @@
all: web preview
site:
+ node script/web-bundle.js > web/content/assets/js/jison.js
cd web/ && nanoc compile
- cp -r examples web/output/jison/
+ cp -r examples web/output/jison/
preview:
cd web/ && nanoc view
View
2  lib/jison/util/io.js
@@ -11,6 +11,7 @@ exports.join = require('path').join;
exports.basename = require('path').basename;
exports.args = process.argv.slice(1);
exports.exit = process.exit;
+exports.resolve = require('url').resolve;
exports.read = function (fname) {
return fs.readFileSync(fname, "utf8");
@@ -49,6 +50,7 @@ exports.join = fs.join;
exports.basename = fs.basename;
exports.args = system.args.slice(0);
exports.exit = require('os').exit;
+exports.resolve = fs.resolve;
exports.read = function (fname) {
return fs.read(fname);
View
43 script/cjs-bundler.js
@@ -0,0 +1,43 @@
+var IO = require("../lib/jison/util/io");
+
+exports.bundle = function(modules) {
+ var moduleDefs = [];
+
+ for (var i = 0; i < modules.length; i++) {
+ var baseID = modules[i].id;
+ var path = modules[i].path;
+
+ var requires = [];
+ var text = IO.read(path).replace(/require\s*\(\s*["']([\/\.\w-]+)["']\s*\)/g, function(match, requireID) {
+ if (/^\./.test(requireID)) {
+ var newID = IO.resolve(baseID, requireID)
+ //console.log("//replacing " + requireID + " in " + baseID + " with " + newID);
+ requireID = newID;
+ }
+ requires.push(requireID);
+ return "require("+JSON.stringify(requireID)+")";
+ });
+
+ moduleDefs.push("require.def("+JSON.stringify(baseID)+",{factory:function(require,exports,module){\n" + text + "\n//*/\n},requires:"+JSON.stringify(requires)+"})");
+ }
+
+ return "var require = (" + req + ")()\n" + moduleDefs.join(";\n\n") + ";";
+}
+
+var req = function() {
+ var modules = {};
+ var factories = {};
+ var r = function(id) {
+ if (!modules[id]) {
+ console.log(id);
+ modules[id] = {};
+ factories[id](r, modules[id], { id : id });
+ }
+ return modules[id];
+ };
+ r.def = function(id, params) {
+ console.log('def', id);
+ factories[id] = params.factory;
+ };
+ return r;
+}
View
24 script/web-bundle.js
@@ -0,0 +1,24 @@
+
+var IO = require("../lib/jison/util/io");
+
+var path = IO.cwd();
+
+function generate () {
+ var bundler = require("./cjs-bundler"),
+ script = bundler.bundle([
+ { id : "jison", path : IO.join(path, "lib", "jison.js") },
+ { id : "jison/lexer", path : IO.join(path, "lib", "jison", "lexer.js") },
+ { id : "jison/bnf", path : IO.join(path, "lib", "jison", "bnf.js") },
+ { id : "jison/jisonlex", path : IO.join(path, "lib", "jison", "jisonlex.js") },
+ //{ id : "jison/json2jison", path : IO.join(path, "lib", "jison", "json2jison.js") },
+ { id : "jison/util/set", path : IO.join(path, "lib", "jison", "util", "set.js") },
+ { id : "jison/util/typal", path : IO.join(path, "lib", "jison", "util", "typal.js") },
+ { id : "jison/util/bnf-parser", path : IO.join(path, "lib", "jison", "util", "bnf-parser.js") },
+ { id : "jison/util/lex-parser", path : IO.join(path, "lib", "jison", "util", "lex-parser.js") }
+ ]);
+
+ var out = "var require = (function() {\n" + script + ";\nreturn require;\n})();";
+ IO.p(out);
+}
+
+generate();
Please sign in to comment.
Something went wrong with that request. Please try again.