diff --git a/README.md b/README.md index 971d790874..37fda89096 100644 --- a/README.md +++ b/README.md @@ -8,14 +8,16 @@ You only need to include algos.coffee It will include the rest as necessary. - - ##Installation -* Reqire files - * Node.js - just include algos.coffee as usual. - * Browser - use requre.js with "cs" plugin to include algos.coffee directly into your index.html - * See require.js-example folder. - * Run plain http-server from within "equire.js-example" folder and point your browser to index.html to see how it works. (it should alert message in CAPS) + +* Node.js + * "npm install coffee-script" to be able to require CS + * "npm install requirejs" to enable loading of AMD type modules within indicated subfolder + * Configure requirejs module as observerd in examples/node.js/server.js + * run "node server" from within "examples/node.js" folder (it should output message in CAPS to console.log) +* Browser - use require.js with "cs" plugin to include algos.coffee directly into your index.html + * See examples/browser folder. + * Run plain http-server from within "examples/browser" folder and point your browser to index.html to see how it works. (it should output message in CAPS to console.log) * Note how to invoke scoring function: * algos.score = function (user, task, direction) {} diff --git a/require.js-example/app/main.js b/examples/browser/app/main.js similarity index 99% rename from require.js-example/app/main.js rename to examples/browser/app/main.js index 8769fe736a..8c7ff41d4e 100644 --- a/require.js-example/app/main.js +++ b/examples/browser/app/main.js @@ -5,7 +5,6 @@ requirejs.config({ cs: 'requirejs/cs', 'coffee-script': 'requirejs/coffee-script' } - }); //Require files we need. diff --git a/require.js-example/app/module.coffee b/examples/browser/app/module.coffee similarity index 87% rename from require.js-example/app/module.coffee rename to examples/browser/app/module.coffee index 07585f44a5..25ca85aa19 100644 --- a/require.js-example/app/module.coffee +++ b/examples/browser/app/module.coffee @@ -10,11 +10,11 @@ #I.e. require parent module #NOTE: I did not figure out how to require coffee-script here. As this has to be both Node.js and require.js compatible syntax. And they disagree on how to do that. Anyway we can stick with .js parent modules for a while. - parent = require('./parentModule'); + parent = require('cs!./parentModule'); module.exports = { foo: (message)-> #use parent module - alert parent.parentFoo message + console.log parent.parentFoo message } \ No newline at end of file diff --git a/examples/browser/app/parentModule.coffee b/examples/browser/app/parentModule.coffee new file mode 100644 index 0000000000..2190593c67 --- /dev/null +++ b/examples/browser/app/parentModule.coffee @@ -0,0 +1,13 @@ +({ define: ( + if typeof define == "function" + define + else + (F)-> + F(require, exports, module) +)}).define (require, exports, module)-> + #Write your code here as usual + module.exports = + { + parentFoo: (message)-> + message.toUpperCase() + } \ No newline at end of file diff --git a/require.js-example/index.html b/examples/browser/index.html similarity index 100% rename from require.js-example/index.html rename to examples/browser/index.html diff --git a/require.js-example/requirejs/coffee-script.js b/examples/browser/requirejs/coffee-script.js similarity index 100% rename from require.js-example/requirejs/coffee-script.js rename to examples/browser/requirejs/coffee-script.js diff --git a/require.js-example/requirejs/cs.js b/examples/browser/requirejs/cs.js similarity index 100% rename from require.js-example/requirejs/cs.js rename to examples/browser/requirejs/cs.js diff --git a/require.js-example/requirejs/require.js b/examples/browser/requirejs/require.js similarity index 100% rename from require.js-example/requirejs/require.js rename to examples/browser/requirejs/require.js diff --git a/examples/node.js/app/module.coffee b/examples/node.js/app/module.coffee new file mode 100644 index 0000000000..25ca85aa19 --- /dev/null +++ b/examples/node.js/app/module.coffee @@ -0,0 +1,20 @@ +#This is a boilerplate to make this file both node.js and AMD (require.js) module compatible. +({ define: ( + if typeof define == "function" + define + else + (F)-> + F(require, exports, module) +)}).define (require, exports, module)-> + #Write your code here as usual + + #I.e. require parent module + #NOTE: I did not figure out how to require coffee-script here. As this has to be both Node.js and require.js compatible syntax. And they disagree on how to do that. Anyway we can stick with .js parent modules for a while. + parent = require('cs!./parentModule'); + + module.exports = + { + foo: (message)-> + #use parent module + console.log parent.parentFoo message + } \ No newline at end of file diff --git a/examples/node.js/app/parentModule.coffee b/examples/node.js/app/parentModule.coffee new file mode 100644 index 0000000000..2190593c67 --- /dev/null +++ b/examples/node.js/app/parentModule.coffee @@ -0,0 +1,13 @@ +({ define: ( + if typeof define == "function" + define + else + (F)-> + F(require, exports, module) +)}).define (require, exports, module)-> + #Write your code here as usual + module.exports = + { + parentFoo: (message)-> + message.toUpperCase() + } \ No newline at end of file diff --git a/examples/node.js/requirejs/cs.js b/examples/node.js/requirejs/cs.js new file mode 100644 index 0000000000..a7065665de --- /dev/null +++ b/examples/node.js/requirejs/cs.js @@ -0,0 +1,168 @@ +/** + * Created with JetBrains PhpStorm. + * User: y + * Date: 5/10/13 + * Time: 6:48 PM + * To change this template use File | Settings | File Templates. + */ +/** + * @license cs 0.4.3 Copyright (c) 2010-2011, The Dojo Foundation All Rights Reserved. + * Available via the MIT or new BSD license. + * see: http://github.com/jrburke/require-cs for details + */ + +/*jslint */ +/*global define, window, XMLHttpRequest, importScripts, Packages, java, + ActiveXObject, process, require */ + +define(['coffee-script'], function (CoffeeScript) { + 'use strict'; + var fs, getXhr, + progIds = ['Msxml2.XMLHTTP', 'Microsoft.XMLHTTP', 'Msxml2.XMLHTTP.4.0'], + fetchText = function () { + throw new Error('Environment unsupported.'); + }, + buildMap = {}; + + if (typeof process !== "undefined" && + process.versions && + !!process.versions.node) { + //Using special require.nodeRequire, something added by r.js. + fs = require.nodeRequire('fs'); + fetchText = function (path, callback) { + callback(fs.readFileSync(path, 'utf8')); + }; + } else if ((typeof window !== "undefined" && window.navigator && window.document) || typeof importScripts !== "undefined") { + // Browser action + getXhr = function () { + //Would love to dump the ActiveX crap in here. Need IE 6 to die first. + var xhr, i, progId; + if (typeof XMLHttpRequest !== "undefined") { + return new XMLHttpRequest(); + } else { + for (i = 0; i < 3; i += 1) { + progId = progIds[i]; + try { + xhr = new ActiveXObject(progId); + } catch (e) {} + + if (xhr) { + progIds = [progId]; // so faster next time + break; + } + } + } + + if (!xhr) { + throw new Error("getXhr(): XMLHttpRequest not available"); + } + + return xhr; + }; + + fetchText = function (url, callback) { + var xhr = getXhr(); + xhr.open('GET', url, true); + xhr.onreadystatechange = function (evt) { + //Do not explicitly handle errors, those should be + //visible via console output in the browser. + if (xhr.readyState === 4) { + callback(xhr.responseText); + } + }; + xhr.send(null); + }; + // end browser.js adapters + } else if (typeof Packages !== 'undefined') { + //Why Java, why is this so awkward? + fetchText = function (path, callback) { + var stringBuffer, line, + encoding = "utf-8", + file = new java.io.File(path), + lineSeparator = java.lang.System.getProperty("line.separator"), + input = new java.io.BufferedReader(new java.io.InputStreamReader(new java.io.FileInputStream(file), encoding)), + content = ''; + try { + stringBuffer = new java.lang.StringBuffer(); + line = input.readLine(); + + // Byte Order Mark (BOM) - The Unicode Standard, version 3.0, page 324 + // http://www.unicode.org/faq/utf_bom.html + + // Note that when we use utf-8, the BOM should appear as "EF BB BF", but it doesn't due to this bug in the JDK: + // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4508058 + if (line && line.length() && line.charAt(0) === 0xfeff) { + // Eat the BOM, since we've already found the encoding on this file, + // and we plan to concatenating this buffer with others; the BOM should + // only appear at the top of a file. + line = line.substring(1); + } + + stringBuffer.append(line); + + while ((line = input.readLine()) !== null) { + stringBuffer.append(lineSeparator); + stringBuffer.append(line); + } + //Make sure we return a JavaScript string and not a Java string. + content = String(stringBuffer.toString()); //String + } finally { + input.close(); + } + callback(content); + }; + } + + return { + fetchText: fetchText, + + get: function () { + return CoffeeScript; + }, + + write: function (pluginName, name, write) { + if (buildMap.hasOwnProperty(name)) { + var text = buildMap[name]; + write.asModule(pluginName + "!" + name, text); + } + }, + + version: '0.4.3', + + load: function (name, parentRequire, load, config) { + var path = parentRequire.toUrl(name + '.coffee'); + fetchText(path, function (text) { + + //Do CoffeeScript transform. + try { + text = CoffeeScript.compile(text, config.CoffeeScript); + } catch (err) { + err.message = "In " + path + ", " + err.message; + throw err; + } + + //Hold on to the transformed text if a build. + if (config.isBuild) { + buildMap[name] = text; + } + + //IE with conditional comments on cannot handle the + //sourceURL trick, so skip it if enabled. + /*@if (@_jscript) @else @*/ + if (!config.isBuild) { + text += "\r\n//@ sourceURL=" + path; + } + /*@end@*/ + + load.fromText(name, text); + + //Give result to load. Need to wait until the module + //is fully parse, which will happen after this + //execution. + parentRequire([name], function (value) { + load(value); + }); + }); + } + }; +}); \ No newline at end of file diff --git a/examples/node.js/server.js b/examples/node.js/server.js new file mode 100644 index 0000000000..f0040044b5 --- /dev/null +++ b/examples/node.js/server.js @@ -0,0 +1,18 @@ +var requirejs = require('requirejs'); + +requirejs.config({ + //Pass the top-level main.js/index.js require + //function to requirejs so that node modules + //are loaded relative to the top-level JS file. + nodeRequire: require, + baseUrl:'', //relative to this file + paths: { //relative to baseUrl + cs: 'requirejs/cs', //plugin to use coffee-script to parse file first. + 'coffee-script': 'coffee-script' + } +}); + +requirejs(['cs!./app/module'], + function (module) { + module.foo('Hello!'); + }); \ No newline at end of file diff --git a/require.js-example/app/parentModule.js b/require.js-example/app/parentModule.js deleted file mode 100644 index 7f26689a72..0000000000 --- a/require.js-example/app/parentModule.js +++ /dev/null @@ -1,16 +0,0 @@ -//this module is required to run module. -//again boilerplate here for node.js\require.js compatibility. -({ - define: (typeof define === "function" ? define : function (F) { - return F(require, exports, module); - }) -}).define(function (require, exports, module) { - - //write regular node.js code here - return module.exports = { - parentFoo: function (message) { - return message.toUpperCase(); - } - }; - - }); \ No newline at end of file