Permalink
Browse files

Remove `identifier` option; eco.compile returns a bare function now

  • Loading branch information...
1 parent 6a92073 commit 8ab3bec5700db98915a873907fe4fc238366c9e0 @sstephenson committed May 23, 2011
Showing with 10 additions and 34 deletions.
  1. +5 −11 lib/eco/compiler.js
  2. +5 −8 src/eco/compiler.coffee
  3. +0 −15 test/test_compile.coffee
View
@@ -4,21 +4,15 @@
preprocess = require("./preprocessor").preprocess;
indent = require("./util").indent;
module.exports = eco = function(source) {
- var module;
- (new Function("module", compile(source)))(module = {});
- return module.exports;
+ return new Function("return " + (compile(source)))();
};
eco.preprocess = preprocess;
- eco.compile = compile = function(source, options) {
- var identifier, script, _ref;
- identifier = (_ref = options != null ? options.identifier : void 0) != null ? _ref : "module.exports";
- if (!identifier.match(/\./)) {
- identifier = "var " + identifier;
- }
+ eco.compile = compile = function(source) {
+ var script;
script = CoffeeScript.compile(preprocess(source), {
noWrap: true
});
- return "" + identifier + " = function(__obj) {\n if (!__obj) __obj = {};\n var __out = [], __capture = function(callback) {\n var out = __out, result;\n __out = [];\n callback.call(this);\n result = __out.join('');\n __out = out;\n return __safe(result);\n }, __sanitize = function(value) {\n if (value && value.ecoSafe) {\n return value;\n } else if (typeof value !== 'undefined' && value != null) {\n return __escape(value);\n } else {\n return '';\n }\n }, __safe, __objSafe = __obj.safe, __escape = __obj.escape;\n __safe = __obj.safe = function(value) {\n if (value && value.ecoSafe) {\n return value;\n } else {\n if (!(typeof value !== 'undefined' && value != null)) value = '';\n var result = new String(value);\n result.ecoSafe = true;\n return result;\n }\n };\n if (!__escape) {\n __escape = __obj.escape = function(value) {\n return ('' + value)\n .replace(/&/g, '&amp;')\n .replace(/</g, '&lt;')\n .replace(/>/g, '&gt;')\n .replace(/\x22/g, '&quot;');\n };\n }\n (function() {\n" + (indent(script, 4)) + "\n }).call(__obj);\n __obj.safe = __objSafe, __obj.escape = __escape;\n return __out.join('');\n};";
+ return "function(__obj) {\n if (!__obj) __obj = {};\n var __out = [], __capture = function(callback) {\n var out = __out, result;\n __out = [];\n callback.call(this);\n result = __out.join('');\n __out = out;\n return __safe(result);\n }, __sanitize = function(value) {\n if (value && value.ecoSafe) {\n return value;\n } else if (typeof value !== 'undefined' && value != null) {\n return __escape(value);\n } else {\n return '';\n }\n }, __safe, __objSafe = __obj.safe, __escape = __obj.escape;\n __safe = __obj.safe = function(value) {\n if (value && value.ecoSafe) {\n return value;\n } else {\n if (!(typeof value !== 'undefined' && value != null)) value = '';\n var result = new String(value);\n result.ecoSafe = true;\n return result;\n }\n };\n if (!__escape) {\n __escape = __obj.escape = function(value) {\n return ('' + value)\n .replace(/&/g, '&amp;')\n .replace(/</g, '&lt;')\n .replace(/>/g, '&gt;')\n .replace(/\x22/g, '&quot;');\n };\n }\n (function() {\n" + (indent(script, 4)) + "\n }).call(__obj);\n __obj.safe = __objSafe, __obj.escape = __escape;\n return __out.join('');\n};";
};
eco.render = function(source, data) {
return (eco(source))(data);
@@ -27,7 +21,7 @@
require.extensions[".eco"] = function(module, filename) {
var source;
source = require("fs").readFileSync(filename, "utf-8");
- return module._compile(compile(source), filename);
+ return module._compile("module.exports = " + (compile(source)), filename);
};
} else if (require.registerExtension) {
require.registerExtension(".eco", compile);
View
@@ -3,18 +3,15 @@ CoffeeScript = require "coffee-script"
{indent} = require "./util"
module.exports = eco = (source) ->
- (new Function "module", compile source) module = {}
- module.exports
+ do new Function "return #{compile source}"
eco.preprocess = preprocess
-eco.compile = compile = (source, options) ->
- identifier = options?.identifier ? "module.exports"
- identifier = "var #{identifier}" unless identifier.match(/\./)
- script = CoffeeScript.compile preprocess(source), noWrap: true
+eco.compile = compile = (source) ->
+ script = CoffeeScript.compile preprocess(source), noWrap: true
"""
- #{identifier} = function(__obj) {
+ function(__obj) {
if (!__obj) __obj = {};
var __out = [], __capture = function(callback) {
var out = __out, result;
@@ -65,7 +62,7 @@ eco.render = (source, data) ->
if require.extensions
require.extensions[".eco"] = (module, filename) ->
source = require("fs").readFileSync filename, "utf-8"
- module._compile compile(source), filename
+ module._compile "module.exports = #{compile source}", filename
else if require.registerExtension
require.registerExtension ".eco", compile
View
@@ -21,18 +21,3 @@ module.exports =
catch err
test.ok err.toString().match /^Parse error on line 1/
test.done()
-
- "identifier defaults to module.exports": (test) ->
- output = eco.compile fixture("hello.eco")
- test.ok output.match /^module\.exports =/
- test.done()
-
- "identifier can be changed": (test) ->
- output = eco.compile fixture("hello.eco"), identifier: "hello"
- test.ok output.match /^var hello =/
- test.done()
-
- "identifiers with '.' omit 'var' keyword": (test) ->
- output = eco.compile fixture("hello.eco"), identifier: "window.hello"
- test.ok output.match /^window\.hello =/
- test.done()

0 comments on commit 8ab3bec

Please sign in to comment.