Permalink
Browse files

first commit

  • Loading branch information...
1 parent aa90f23 commit 7769b23896a5b8eacdb368fc3b204bf0767ebf8d @WebReflection committed Sep 4, 2012
Showing with 284 additions and 0 deletions.
  1. +6 −0 cycles/a.js
  2. +6 −0 cycles/b.js
  3. +4 −0 cycles/main.js
  4. BIN jar/yuicompressor-2.4.6.jar
  5. +2 −0 js/a.js
  6. +5 −0 js/b.js
  7. +2 −0 js/c.js
  8. +2 −0 js/d.js
  9. +9 −0 js/e.js
  10. +19 −0 package.json
  11. +159 −0 require_client
  12. +70 −0 src/require.js
View
@@ -0,0 +1,6 @@
+console.log('a starting');
+exports.done = false;
+var b = require('./b.js');
+console.log('in a, b.done = ' + b.done);
+exports.done = true;
+console.log('a done');
View
@@ -0,0 +1,6 @@
+console.log('b starting');
+exports.done = false;
+var a = require('./a.js');
+console.log('in b, a.done = ' + a.done);
+exports.done = true;
+console.log('b done');
View
@@ -0,0 +1,4 @@
+console.log('main starting');
+var a = require('./a.js');
+var b = require('./b.js');
+console.log('in main, a.done=' + a.done + ', b.done=' + b.done);
View
Binary file not shown.
View
@@ -0,0 +1,2 @@
+// requiring a module within a module
+require("c").test();
View
@@ -0,0 +1,5 @@
+// circular references trap
+require("a");
+require("c");
+// using exports object to export
+exports.test = require("d").test;
View
@@ -0,0 +1,2 @@
+// using module.exports to export
+module.exports.test = require("b").test;
View
@@ -0,0 +1,2 @@
+// using context as module.exports pointer
+this.test = require("e");
View
@@ -0,0 +1,9 @@
+// circular reference again
+require("a");
+require("b");
+require("c");
+require("d");
+// reassigning module.exports to export main method
+module.exports = function exports() {
+ alert(global); // should be the window object
+};
View
@@ -0,0 +1,19 @@
+{
+ "version": "0.0.1",
+ "name": "require_client",
+ "description": "a meaningful node require for JS client side",
+ "homepage": "https://github.com/WebReflection/require_client",
+ "main": "./require_client",
+ "keywords": ["node", "require", "client", "web", "build"],
+ "author": {
+ "name": "Andrea Giammarchi",
+ "web": "http://webreflection.blogspot.com/"
+ },
+ "repository": {
+ "type": "git",
+ "url": "git://github.com/WebReflection/require_client.git"
+ },
+ "bin": {
+ "require_client": "./require_client"
+ }
+}
View
@@ -0,0 +1,159 @@
+#!/usr/bin/env node
+
+/*! (C) Andrea Giammarchi */
+
+function Queue(args, f) {
+ // http://webreflection.blogspot.ie/2012/03/tweet-sized-queue-system.html
+ setTimeout(args.next = function next() {
+ return (f = args.shift()) ? !!f(args) || !0 : !1;
+ }, 0);
+ return args;
+}
+
+function cleanPath(fileName) {
+ return fileName.slice(config.path.length).replace(reJSend, "");
+}
+
+function compile(regular, compressed, onclose) {
+ spawn(
+ "java", [
+ "-jar",
+ yuicompressor,
+ regular,
+ "-o",
+ compressed
+ ], { stdio: 'inherit' }
+ ).on("close", onclose);
+}
+
+function readableSize(bytes) {
+ var i = 2, size;
+ while (1024 <= bytes && i--) {
+ bytes /= 1024;
+ }
+ switch (i) {
+ case 2: size = "bytes"; break;
+ case 1: size = "KB"; break;
+ case 0: size = "MB"; break;
+ }
+ return (i === 2 ? bytes : bytes.toFixed(2)) + " " + size;
+}
+
+function resolveArguments(args, keepProgramName) {
+ var a = [].slice.call(args, 0);
+ if (/(?:^|\/|\\)node(?:\.exe)?$/.test(a[0])) a.shift();
+ keepProgramName || a.shift();
+ return a;
+}
+
+var
+ args = resolveArguments(process.argv),
+ validArgs = !!args.length,
+ reJSend = /\.js$/,
+ path = require("path"),
+ fs = require("fs"),
+ zlib = require("zlib"),
+ spawn = require('child_process').spawn,
+ config = {path:path.join(args.shift(), "./")},
+ require_src = fs.readFileSync(
+ path.join(__dirname, "src", "require.js"),
+ "utf-8"
+ ).replace(/\/\/:dev([^\1]*)(\/\/dev:)/g, ""),
+ grab = Function("config",
+ "return " + RegExp.$1.replace(/^[^\1]*(function grab\()/, "$1")
+ )(config),
+ tmp_file = path.join(__dirname, "build", "tmp"),
+ tmp_uid = 0,
+ tmp_parsed = 0,
+ require_parsed = path.join(__dirname, "build", "require_parsed.js"),
+ require_build = path.join(__dirname, "build", "require.js"),
+ yuicompressor = path.join(__dirname, "jar", "yuicompressor-2.4.6.jar"),
+ modules = {}
+;
+
+if (validArgs) {
+
+ fs.writeFileSync(require_parsed, require_src, "utf-8");
+
+ Queue([
+ function (q) {
+ compile(require_parsed, require_build, q.next);
+ },
+ function readDir(q, dir) {
+ // per each found file, check for require() dependencise and create a list
+ // of files with keys/values pairs
+ // when a file is not found, discard it
+ // the path should start from the config.path
+ // so add the path runtime ? or resolve through the root folder
+ // in few words, cycles should simply work as it is
+ fs.readdirSync(dir || (dir = config.path)).forEach(function (fileName) {
+ fileName = path.join(dir, fileName);
+ if (reJSend.test(fileName)) {
+ var tmp_name = tmp_file + tmp_uid++ + ".js";
+ tmp_parsed++;
+ console.log("compiling module " + cleanPath(fileName));
+ compile(fileName, tmp_name, function () {
+ modules[cleanPath(
+ fileName
+ )] = fs.readFileSync(tmp_name, "utf-8");
+ fs.unlinkSync(tmp_name);
+ --tmp_parsed || q.next();
+ });
+ } else if(fs.statSync(fileName).isDirectory()) {
+ readDir(q, fileName);
+ }
+ });
+ tmp_parsed || q.next();
+ },
+ function (q) {
+ fs.unlinkSync(require_parsed);
+ fs.writeFileSync(
+ require_build,
+ fs.readFileSync(
+ require_build, "utf-8"
+ )
+ .replace(/(strict:.*?,path:('|").*?\2)}\);$/, "\n$1\n});")
+ .replace(/\{production:1\}/, JSON.stringify(
+ modules, null, " "
+ )), "utf-8"
+ );
+ console.log(readableSize(
+ fs.statSync(require_build).size
+ ));
+ fs
+ .createReadStream(require_build)
+ .pipe(
+ zlib.createGzip({
+ level: zlib.Z_BEST_COMPRESSION
+ })
+ )
+ .pipe(
+ fs.createWriteStream(require_build + ".gz")
+ )
+ .on("close", function () {
+ console.log(readableSize(
+ fs.statSync(require_build + ".gz").size
+ ) + " gzipped");
+ fs.unlinkSync(require_build + ".gz");
+ if (args.length) {
+ fs.writeFileSync(
+ args.shift(),
+ fs.readFileSync(require_build, "utf-8"),
+ "utf-8"
+ );
+ fs.unlinkSync(require_build);
+ }
+ })
+ ;
+ }
+ ]);
+
+} else {
+ console.log("");
+ console.log("Usage:");
+ console.log(" node require_client JS/source/folder [destination_file]");
+ console.log("");
+ console.log("if destination_file is not specified, built will be in");
+ console.log(require_build);
+ console.log("");
+}
View
@@ -0,0 +1,70 @@
+/*! (C) Andrea Giammarchi */
+
+var require = function (global, Function, config) {
+
+ //:dev //will be removed at build time
+ function grab(path, normalize) {
+ if (normalize) {
+ return (config.path + path) + ".js";
+ //return (config.path + path).replace(/\.js$/, "") + ".js";
+ } else {
+ var xhr = new XMLHttpRequest;
+ xhr.open("get", grab(path, TRUE), !TRUE);
+ xhr.send(null);
+ return xhr.responseText;
+ }
+ }
+ //dev:function grab(path, normalize) {return normalize ? path : modules[path]}
+
+ function retrieve(path) {
+
+ var
+ exports = cache[path] = {},
+ module = {
+ id: path,
+ parent: global,
+ filename: grab(path, TRUE),
+ web: TRUE // require client
+ }
+ ;
+
+ module[EXPORTS] = exports;
+ Function(
+ "global", "module", EXPORTS,
+ (config.strict ?
+ "'use strict';" : ""
+ ) + grab(path)
+ ).call(exports,
+ global, module, exports
+ );
+
+ hasOP.call(
+ exports = module[EXPORTS],
+ LOADED
+ ) || (exports[LOADED] = TRUE);
+
+ return exports;
+ }
+
+ function require(module) {
+ return hasOP.call(cache, module) ?
+ cache[module] :
+ cache[module] = retrieve(module)
+ ;
+ }
+
+ var
+ EXPORTS = "exports",
+ LOADED = "loaded",
+ TRUE = !0,
+ cache = {},
+ hasOP = cache.hasOwnProperty,
+ modules = {production:1};
+ ;
+
+ require.config = config;
+ require.main = global;
+
+ return require;
+
+}(this, Function, {strict:!0, path:"js/"});

0 comments on commit 7769b23

Please sign in to comment.