Permalink
Browse files

Initial commit :)

  • Loading branch information...
0 parents commit e103f04cf8c3a58bf9525e69204d1dcfafaa1548 @bnoguchi committed Feb 9, 2011
@@ -0,0 +1 @@
+**.swp
@@ -0,0 +1,132 @@
+function fluentRequire (path) {
+ var mod = fluentRequire.modules[path];
+ if (mod && mod.compiled) {
+ return mod.compiled;
+ }
+ throw new Error("Missing module " + path);
+}
+
+fluentRequire.modules = {};
+
+/**
+ * Invoked by JavaScript sent from the server back to the browser.
+ * @param {String} module is the fluent-normalized module name
+ * @param {String} src is the JavaScript content to eval
+ * @param {Array} deps is an Array of dependencies (strings pointing to modules) that
+ * must load before module can be loaded.
+ */
+fluentRequire.load = function (module, src, deps) {
+ var i = deps.length
+ , depModule
+ , target = ModulePromise.from(module);
+ target.src = src;
+ while (i--) {
+ depModule = ModulePromise.from(deps[i], target);
+ if (!depModule.compiled) {
+ target.dependsOn(depModule);
+ fluentRequire.modules[depModule.name] = depModule;
+ depModule.load();
+ }
+ }
+ if (target.deps.length === 0) {
+ target.loaded();
+ }
+};
+
+var ModulePaths = {
+ isNormalized: function (name) {
+ return (/^\//).test(name);
+ },
+ isRel: function (name) {
+ return (/^\./).test(name);
+ },
+ isNpm: function (name) {
+ return (/^\/NPM\//).test(name) || !this.isRel(name);
+ },
+ normalizeRelToParent: function (name, parent) {
+ if (!parent) return name;
+ var pathparts = parent.basedir.split('/');
+ return this.normalizeRelToDir(name, pathparts);
+ },
+ normalizeRelToDir: function (name, dirparts) {
+ var parts = name.split('/')
+ for (var i = 0, l = parts.length, part; i < l; i++) {
+ part = parts[i];
+ if (part === '.') continue;
+ else if (part === '..') dirparts.pop();
+ else dirparts.push(part);
+ }
+ return dirparts.join('/') + '.js';
+ }
+};
+
+/**
+ * @param {String} name
+ * @param {ModulePromise} parent
+ */
+function ModulePromise (name, parent) {
+ this.parents = [];
+ this.name = ModulePaths.isNormalized(name)
+ ? name
+ : ModulePaths.isRel(name)
+ ? ModulePaths.normalizeRelToParent(name, parent)
+ : "/NPM/" + name + '.js';
+ this.basedir = ModulePaths.isNpm(name)
+ ? this.name.split('/').length > 2
+ ? this.name.split('/').slice(0, -1).join('/')
+ : this.name.replace(/\.js$/, '')
+ : parent
+ ? parent.basedir
+ : name.split('/').slice(0, -1).join('/');
+ this.deps = [];
+}
+
+ModulePromise.from = function (module, parent) {
+ if (!parent) {
+ return fluentRequire.modules[module] || (fluentRequire.modules[module] = new ModulePromise(module));
+ } else {
+ var name = ModulePaths.isNormalized(module)
+ ? module
+ : ModulePaths.isRel(module)
+ ? ModulePaths.normalizeRelToParent(module, parent)
+ : "/NPM/" + module + '.js';
+ return fluentRequire.modules[name] || (fluentRequire.modules[name] = new ModulePromise(module, parent));
+ }
+};
+
+ModulePromise.prototype = {
+ dependsOn: function (module) {
+ this.deps.push(module.name);
+ module.parents.push(this);
+ },
+ load: function () {
+ var priorScript = document.getElementsByTagName('script')[0]
+ , script = document.createElement('SCRIPT');
+ script.async = true;
+ script.src = this.name;
+ script.onerror = function () {};
+ priorScript.parentNode.insertBefore(script, priorScript);
+ },
+ loaded: function (dep) {
+ if (this.compiled) return;
+ var deps = this.deps;
+ if (dep) deps.splice(deps.indexOf(dep), 1);
+ if (!deps.length) {
+ var module = { exports: {} }
+ , exports = module.exports
+ , self = this;
+ (function () {
+ try {
+ return eval(self.src);
+ } catch (e) {
+ throw new Error("Something wrong with...\n" + self.src);
+ }
+ }).call(module.exports);
+ this.compiled = module.exports;
+ var i = this.parents.length;
+ while (i--) {
+ this.parents[i].loaded(this.name);
+ }
+ }
+ }
+};
@@ -0,0 +1 @@
+<!DOCTYPE html><html><head><title>node-fluent relative module example</title></head><body><div id="answer"></div><script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script><script type="text/javascript" src="/browser_require.js"></script><script type="text/javascript" src="/js/app.js"></script></body></html>
@@ -0,0 +1,9 @@
+!!! 5
+html
+ head
+ title node-fluent relative module example
+ body
+ #answer
+ script(type: 'text/javascript', src: 'https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js')
+ script(type: 'text/javascript', src: '/browser_require.js')
+ script(type: 'text/javascript', src: '/js/app.js')
@@ -0,0 +1,14 @@
+var _ = require('underscore')
+ , Set = require('data-structures-js/set')
+ , xss = require('validator/xss');
+
+console.log(Set);
+console.log(xss);
+
+$( function () {
+ var str = _.reduce( [1, 2, 3, 4], function (str, num) {
+ str += num;
+ return str;
+ }, '');
+ $('#answer').text(str);
+});
@@ -0,0 +1,11 @@
+var path = require('path');
+require.paths.unshift(path.join(__dirname, '..', '..'));
+var connect = require('connect');
+var app = connect.createServer();
+var fluent = require('fluent');
+app.use(fluent({
+ base: __dirname
+}));
+app.use(connect.staticProvider(__dirname));
+app.listen(1234);
+console.log("Server running at http://127.0.0.1:1234");
@@ -0,0 +1 @@
+<!DOCTYPE html><html><head><title>node-fluent relative module example</title></head><body><div id="answer"></div><script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script><script type="text/javascript" src="/browser_require.js"></script><script type="text/javascript" src="/js/app.js"></script></body></html>
@@ -0,0 +1,9 @@
+!!! 5
+html
+ head
+ title node-fluent relative module example
+ body
+ #answer
+ script(type: 'text/javascript', src: 'https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js')
+ script(type: 'text/javascript', src: '/browser_require.js')
+ script(type: 'text/javascript', src: '/js/app.js')
@@ -0,0 +1,3 @@
+module.exports = function () {
+ return "in the trees above";
+};
@@ -0,0 +1,5 @@
+var rel = require('./rel');
+
+$( function () {
+ $('#answer').text(rel());
+});
@@ -0,0 +1,5 @@
+var above = require('../above');
+
+module.exports = function () {
+ return "Bird's nest " + above();
+};
@@ -0,0 +1,5 @@
+var nest = require('./nested/nest');
+
+module.exports = function () {
+ return "Hello World! with " + nest();
+};
@@ -0,0 +1,11 @@
+var path = require('path');
+require.paths.unshift(path.join(__dirname, '..', '..'));
+var connect = require('connect');
+var app = connect.createServer();
+var fluent = require('fluent');
+app.use(fluent({
+ base: __dirname
+}));
+app.use(connect.staticProvider(__dirname));
+app.listen(1234);
+console.log("Server running at http://127.0.0.1:1234");
Oops, something went wrong.

0 comments on commit e103f04

Please sign in to comment.