diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..7175240 Binary files /dev/null and b/.DS_Store differ diff --git a/Cakefile b/Cakefile index 89b1af6..0f8c0e8 100644 --- a/Cakefile +++ b/Cakefile @@ -60,7 +60,7 @@ task "dist", "Generate dist/eco.js", -> "strscan": read "node_modules/strscan/lib/strscan.js" "coffee-script": stub "CoffeeScript" - package = for name, source of modules + pkg = for name, source of modules """ '#{name}': function(module, require, exports) { #{source} @@ -89,12 +89,12 @@ task "dist", "Generate dist/eco.js", -> } }; })({ - #{package.join ',\n'} + #{pkg.join ',\n'} })('eco'); """ try - fs.mkdirSync "#{__dirname}/dist", 0755 + fs.mkdirSync "#{__dirname}/dist", 0o0755 catch err fs.writeFileSync "#{__dirname}/dist/eco.js", "#{header}\n#{source}" diff --git a/README.md b/README.md index ed96add..0617eee 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,22 @@ +# Improved web performance and size + +I am currently working on a web application using eco templates for both the server +and client. The plan is to pre-compile the templates using brunch and load keep them +in the main build file. I just had a looked at the minified output and I was horrified at +what eco had done. + +* It pushes many meaningless lines to the output array, +* preserves multiple recurrent white-spaces when just one white space would be appropriate, +* and leaves html comments in... for fun? + +So in light of this I modified the preprocessor and utilities slightly and corrected these +issues. + +Later I would like to modify eco further so that it didn't build a copy of its base functions +into every template it rendered and instead shared some code brought in through a require. +This would reduce the framework agnosticism presently enjoyed by eco, so I am going to have to +think about it a little more... + Eco: Embedded CoffeeScript templates ==================================== diff --git a/lib/.DS_Store b/lib/.DS_Store new file mode 100644 index 0000000..7af7ec7 Binary files /dev/null and b/lib/.DS_Store differ diff --git a/lib/command.js b/lib/command.js index 6fa10e8..05d43b3 100644 --- a/lib/command.js +++ b/lib/command.js @@ -1,6 +1,7 @@ +// Generated by CoffeeScript 1.3.3 (function() { - var compile, compileToFile, each, eachFile, eco, exec, fs, indent, mkdir, parseOptions, path, preprocessArgs, printUsage, printVersion, read, stripExtension, sys; - var __slice = Array.prototype.slice; + var compile, compileToFile, each, eachFile, eco, exec, fs, indent, mkdir, parseOptions, path, preprocessArgs, printUsage, printVersion, read, stripExtension, sys, + __slice = [].slice; fs = require("fs"); @@ -20,20 +21,20 @@ }; printVersion = function() { - var package; - package = JSON.parse(fs.readFileSync(__dirname + "/../package.json", "utf8")); - console.error("Eco version " + package.version); + var pkg; + pkg = JSON.parse(fs.readFileSync(__dirname + "/../pkg.json", "utf8")); + console.error("Eco version " + pkg.version); return process.exit(0); }; preprocessArgs = function(args) { - var arg, char, match, result, _i, _j, _len, _len2, _ref; + var arg, char, match, result, _i, _j, _len, _len1, _ref; result = []; for (_i = 0, _len = args.length; _i < _len; _i++) { arg = args[_i]; if (match = /^-([a-z]{2,})/.exec(arg)) { _ref = match[1].split(''); - for (_j = 0, _len2 = _ref.length; _j < _len2; _j++) { + for (_j = 0, _len1 = _ref.length; _j < _len1; _j++) { char = _ref[_j]; result.push("-" + char); } @@ -88,7 +89,9 @@ } } } - if (option) printUsage(); + if (option) { + printUsage(); + } return options; }; @@ -116,13 +119,19 @@ var traverse; traverse = function(root, dir, done) { return fs.readdir(dir, function(err, entries) { - if (err) return callback(err); + if (err) { + return callback(err); + } return each(entries, function(entry, proceed) { var file; - if (entry == null) return done(); + if (entry == null) { + return done(); + } file = path.join(dir, entry); return fs.stat(file, function(err, stat) { - if (err) return callback(err); + if (err) { + return callback(err); + } if (stat.isFile() && /\.eco$/.test(file)) { return callback(null, file, root, proceed); } else if (stat.isDirectory()) { @@ -135,10 +144,14 @@ }); }; return each(files, function(file, proceed) { - if (file == null) return; + if (file == null) { + return; + } return fs.stat(file, function(err, stat) { var root; - if (err) return callback(err); + if (err) { + return callback(err); + } if (stat.isDirectory()) { return traverse(file, file, proceed); } else { @@ -156,7 +169,9 @@ compile = function(infile, identifier, name, callback) { return fs.readFile(infile, "utf8", function(err, source) { var template; - if (err) return callback(err); + if (err) { + return callback(err); + } template = indent(eco.precompile(source), 2); return callback(null, "(function() {\n this." + identifier + " || (this." + identifier + " = {});\n this." + identifier + "[" + (JSON.stringify(name)) + "] = " + (template.slice(2)) + ";\n}).call(this);"); }); @@ -178,10 +193,14 @@ } outfile = path.join(outdir != null ? outdir : root, name + ".js"); return mkdir(path.dirname(outfile), function(err) { - if (err) return callback(err); + if (err) { + return callback(err); + } return compile(infile, identifier, name, function(err, result) { return fs.writeFile(outfile, result + "\n", "utf8", function(err) { - if (err) return callback(err); + if (err) { + return callback(err); + } return callback(null, outfile, name); }); }); @@ -190,29 +209,45 @@ exports.run = function(args) { var infile, name, options; - if (args == null) args = process.argv.slice(2); + if (args == null) { + args = process.argv.slice(2); + } options = parseOptions(args); if (options.stdio) { - if (options.files.length || options.output) printUsage(); + if (options.files.length || options.output) { + printUsage(); + } process.openStdin(); return read(process.stdin, function(source) { return sys.puts(eco.precompile(source)); }); } else if (options.print) { - if (options.files.length !== 1 || options.output) printUsage(); + if (options.files.length !== 1 || options.output) { + printUsage(); + } infile = options.files[0]; name = stripExtension(path.basename(infile)); return compile(infile, options.identifier, name, function(err, result) { - if (err) throw err; + if (err) { + throw err; + } return sys.puts(result); }); } else { - if (!options.files.length) printUsage(); + if (!options.files.length) { + printUsage(); + } return eachFile(options.files, function(err, infile, root, proceed) { - if (err) throw err; - if (infile == null) return; + if (err) { + throw err; + } + if (infile == null) { + return; + } return compileToFile(infile, options.identifier, root, options.output, function(err, outfile, name) { - if (err) throw err; + if (err) { + throw err; + } console.error("" + (JSON.stringify(name)) + ": " + infile + " -> " + outfile); return proceed(); }); diff --git a/lib/compiler.js b/lib/compiler.js index 0eff0e5..7f624eb 100644 --- a/lib/compiler.js +++ b/lib/compiler.js @@ -1,3 +1,4 @@ +// Generated by CoffeeScript 1.3.3 (function() { var CoffeeScript, indent, precompile, preprocess; diff --git a/lib/index.js b/lib/index.js index bca5ac8..4a6fa8e 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,3 +1,4 @@ +// Generated by CoffeeScript 1.3.3 (function() { var compile, eco, precompile, preprocess, _ref; @@ -6,9 +7,9 @@ preprocess = require("./preprocessor").preprocess; module.exports = eco = function(source) { - var _base, _ref2; + var _base, _ref1; if (eco.cache) { - return (_ref2 = (_base = eco.cache)[source]) != null ? _ref2 : _base[source] = compile(source); + return (_ref1 = (_base = eco.cache)[source]) != null ? _ref1 : _base[source] = compile(source); } else { return compile(source); } diff --git a/lib/preprocessor.js b/lib/preprocessor.js index 5a33c64..6e0c558 100644 --- a/lib/preprocessor.js +++ b/lib/preprocessor.js @@ -1,3 +1,4 @@ +// Generated by CoffeeScript 1.3.3 (function() { var Preprocessor, Scanner, util; @@ -38,7 +39,11 @@ Preprocessor.prototype.printString = function(string) { if (string.length) { - return this.record("__out.push " + (util.inspectString(string))); + string = util.trim(string); + string = util.inspectString(string); + if (string.length && string !== "\n") { + return this.record("__out.push " + string); + } } }; @@ -71,7 +76,9 @@ Preprocessor.prototype.dedent = function() { this.level--; - if (this.level < 0) this.fail("unexpected dedent"); + if (this.level < 0) { + this.fail("unexpected dedent"); + } if (this.captures[0] === this.level) { this.captures.shift(); return this.dedent(); diff --git a/lib/scanner.js b/lib/scanner.js index cd7258d..2e06bfd 100644 --- a/lib/scanner.js +++ b/lib/scanner.js @@ -1,3 +1,4 @@ +// Generated by CoffeeScript 1.3.3 (function() { var Scanner, StringScanner, trim; @@ -100,10 +101,16 @@ } else if (this.tail) { this.mode = "data"; code = trim(this.flush()); - if (this.arrow) code += " " + this.arrow; - if (this.isDedentable(code)) callback(["dedent"]); + if (this.arrow) { + code += " " + this.arrow; + } + if (this.isDedentable(code)) { + callback(["dedent"]); + } callback(["recordCode", code]); - if (this.directive) return callback(["indent", this.arrow]); + if (this.directive) { + return callback(["indent", this.arrow]); + } } }; diff --git a/lib/util.js b/lib/util.js index 80fed3e..9142b70 100644 --- a/lib/util.js +++ b/lib/util.js @@ -1,3 +1,4 @@ +// Generated by CoffeeScript 1.3.3 (function() { var repeat, specialCharacters; @@ -22,7 +23,7 @@ }; exports.trim = function(string) { - return string.replace(/^\s+/, "").replace(/\s+$/, ""); + return string.replace(/^\s+/, "").replace(/\s+$/, "").replace(/\s{2,}/gm, " ").replace(/\\n/gm, ""); }; specialCharacters = { @@ -42,7 +43,9 @@ return specialCharacters[character]; } else { code = character.charCodeAt(0).toString(16); - if (code.length === 1) code = "0" + code; + if (code.length === 1) { + code = "0" + code; + } return "\\u00" + code; } }); diff --git a/package.json b/package.json index e8730af..5a7c454 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "eco" , "description": "Embedded CoffeeScript templates" , "author": "Sam Stephenson" -, "version": "1.1.0-rc-3" +, "version": "1.1.1-beta" , "licenses": [ { "type": "MIT" , "url": "https://github.com/sstephenson/eco/raw/master/LICENSE" diff --git a/src/command.coffee b/src/command.coffee index 7c44283..93fd065 100644 --- a/src/command.coffee +++ b/src/command.coffee @@ -22,8 +22,8 @@ printUsage = -> process.exit 1 printVersion = -> - package = JSON.parse fs.readFileSync __dirname + "/../package.json", "utf8" - console.error "Eco version #{package.version}" + pkg = JSON.parse fs.readFileSync __dirname + "/../pkg.json", "utf8" + console.error "Eco version #{pkg.version}" process.exit 0 preprocessArgs = (args) -> diff --git a/src/preprocessor.coffee b/src/preprocessor.coffee index f698dd1..08540b0 100644 --- a/src/preprocessor.coffee +++ b/src/preprocessor.coffee @@ -3,7 +3,7 @@ util = require "./util" module.exports = class Preprocessor @preprocess: (source) -> - preprocessor = new Preprocessor source + preprocessor = new Preprocessor source#.replace(/\s{2,}/gm, "") preprocessor.preprocess() constructor: (source) -> @@ -25,7 +25,9 @@ module.exports = class Preprocessor printString: (string) -> if string.length - @record "__out.push #{util.inspectString string}" + string = util.trim string + string = util.inspectString string + @record "__out.push #{string}" if string.length and string != "\n" beginCode: (options) -> @options = options diff --git a/src/util.coffee b/src/util.coffee index 9f40cf6..1947b4a 100644 --- a/src/util.coffee +++ b/src/util.coffee @@ -10,6 +10,9 @@ exports.trim = (string) -> string .replace(/^\s+/, "") .replace(/\s+$/, "") + .replace(/\s+/gm, " ") + .replace(/\\n/gm,"") + .replace(/<\!--[^\-]+-->/g, "") specialCharacters = '\\': '\\\\'