diff --git a/Cakefile b/Cakefile index a9a6cb0a..67f95d87 100644 --- a/Cakefile +++ b/Cakefile @@ -70,6 +70,8 @@ task 'doc:underscore', 'rebuild the Underscore.coffee documentation page', -> task 'test', 'run the CoffeeScript language test suite', -> helpers.extend global, require 'assert' + require.paths.unshift './test' + test_count: 0 start_time: new Date() [original_ok, original_throws]: [ok, throws] diff --git a/lib/coffee-script.js b/lib/coffee-script.js index 6af3a8fa..b33e41c4 100644 --- a/lib/coffee-script.js +++ b/lib/coffee-script.js @@ -1,5 +1,5 @@ (function(){ - var Lexer, helpers, lexer, parser, path, process_scripts; + var Lexer, compile, helpers, lexer, parser, path, process_scripts; // CoffeeScript can be used both on the server, as a command-line compiler based // on Node.js/V8, or to run CoffeeScripts directly in the browser. This module // contains the main entry functions for tokenzing, parsing, and compiling source @@ -13,6 +13,11 @@ parser = require('./parser').parser; helpers = require('./helpers').helpers; helpers.extend(global, require('./nodes')); + require.registerExtension('.coffee', function(content) { + if (require.registerExtension) { + return compile(content); + } + }); } else { this.exports = (this.CoffeeScript = {}); Lexer = this.Lexer; @@ -24,7 +29,7 @@ lexer = new Lexer(); // Compile a string of CoffeeScript code to JavaScript, using the Coffee/Jison // compiler. - exports.compile = function compile(code, options) { + exports.compile = (compile = function compile(code, options) { options = options || {}; try { return (parser.parse(lexer.tokenize(code))).compile(options); @@ -34,7 +39,7 @@ } throw err; } - }; + }); // Tokenize a string of CoffeeScript code, and return the array of tokens. exports.tokens = function tokens(code) { return lexer.tokenize(code); diff --git a/src/coffee-script.coffee b/src/coffee-script.coffee index a8e6d133..9732e640 100644 --- a/src/coffee-script.coffee +++ b/src/coffee-script.coffee @@ -13,6 +13,7 @@ if process? parser: require('./parser').parser helpers: require('./helpers').helpers helpers.extend global, require './nodes' + require.registerExtension '.coffee', (content) -> compile content if require.registerExtension else this.exports: this.CoffeeScript: {} Lexer: this.Lexer @@ -26,7 +27,7 @@ lexer: new Lexer() # Compile a string of CoffeeScript code to JavaScript, using the Coffee/Jison # compiler. -exports.compile: (code, options) -> +exports.compile: compile: (code, options) -> options ||= {} try (parser.parse lexer.tokenize code).compile options diff --git a/test/test_importing.coffee b/test/test_importing.coffee new file mode 100644 index 00000000..6ae62e0b --- /dev/null +++ b/test/test_importing.coffee @@ -0,0 +1,2 @@ +# Check if it can import a coffeescript-only module and check its output +ok (require 'test_module').foo is "bar" \ No newline at end of file diff --git a/test/test_module.coffee b/test/test_module.coffee new file mode 100644 index 00000000..7c3b08df --- /dev/null +++ b/test/test_module.coffee @@ -0,0 +1 @@ +exports.foo: "bar" \ No newline at end of file