From f5fd562f09e3daecdb3c722e63026f8cfb7c4d80 Mon Sep 17 00:00:00 2001 From: Bernhard Waldbrunner Date: Tue, 23 Jun 2015 23:59:30 +0200 Subject: [PATCH] Commits from PR #98 --- lib/helpers/raw.js | 6 ++-- lib/javascript/index.js | 14 ++++++---- lib/javascript/processors/{es.js => js.js} | 0 lib/template/processors/hbs.js | 28 +++++++++++++++++++ package.json | 1 + .../browserify/{comment.es => comment.js} | 0 .../browserify/{declared.es => declared.js} | 0 .../nested/way/in/here/nested.coffee | 3 ++ .../browserify/nested/way/in/here/nested.js | 3 ++ .../{require_coffee.es => require_coffee.js} | 0 .../{require_js.es => require_js.js} | 0 test/javascripts.js | 28 ++++++++++++++----- 12 files changed, 67 insertions(+), 16 deletions(-) rename lib/javascript/processors/{es.js => js.js} (100%) create mode 100644 lib/template/processors/hbs.js rename test/fixtures/javascripts/browserify/{comment.es => comment.js} (100%) rename test/fixtures/javascripts/browserify/{declared.es => declared.js} (100%) create mode 100644 test/fixtures/javascripts/browserify/nested/way/in/here/nested.coffee create mode 100644 test/fixtures/javascripts/browserify/nested/way/in/here/nested.js rename test/fixtures/javascripts/browserify/{require_coffee.es => require_coffee.js} (100%) rename test/fixtures/javascripts/browserify/{require_js.es => require_js.js} (100%) diff --git a/lib/helpers/raw.js b/lib/helpers/raw.js index c51fcf7ff..5affea213 100644 --- a/lib/helpers/raw.js +++ b/lib/helpers/raw.js @@ -13,9 +13,9 @@ var TerraformError = exports.TerraformError = require("../error").TerraformError */ var processors = exports.processors = { - "html": ["jade", "ejs", "md"], + "html": ["jade", "ejs", "md", "hbs"], "css" : ["styl", "less", "scss", "sass"], - "js" : ["coffee", "es"] + "js" : ["js", "coffee"] } @@ -362,7 +362,7 @@ var outputPath = exports.outputPath = function(source, allowAlternateExtensions) for(var targetExtension in processors){ // .html, .css, .js if (processors.hasOwnProperty(targetExtension)) { processors[targetExtension].forEach(function(sourceExtension){ // .jade, .ejs, .md - if (allowAlternateExtensions) { + if (allowAlternateExtensions && targetExtension !== sourceExtension) { // Don’t bother if it’s .js to .js // Search for a alternate extension before the known source extension e.g. foobar.bar.jade var alternateFileExtension = new RegExp("^.*\\.(\\w{3,4})\\." + sourceExtension + "$") var match = alternateFileExtension.exec(source) diff --git a/lib/javascript/index.js b/lib/javascript/index.js index 28fe2ec07..59d934e2a 100644 --- a/lib/javascript/index.js +++ b/lib/javascript/index.js @@ -1,7 +1,7 @@ -var path = require("path") -var fs = require("fs") -var helpers = require('../helpers') -var minify = require('minify') +var path = require("path") +var fs = require("fs") +var helpers = require('../helpers') +var minify = require('harp-minify') var browserify = require('browserify') var through = require('through') @@ -20,7 +20,8 @@ helpers.processors["js"].forEach(function(sourceType){ extensions.push('.' + sourceType) processors[sourceType] = require("./processors/" + sourceType) }) -processors['js'] = processors['es'] // so it's possible to require .js files + +// processors['js'] = processors['es'] // so it's possible to require .js files module.exports = function(root, filePath, callback){ @@ -70,7 +71,7 @@ module.exports = function(root, filePath, callback){ } process.once('uncaughtException', exceptionHandler) - browserify(filePath, {extensions: extensions}).transform(function(file) { + browserify(srcPath, {extensions: extensions}).transform(function(file) { var result = '' return through(write, end) @@ -80,6 +81,7 @@ module.exports = function(root, filePath, callback){ function end() { if(success) { var that = this + console.log(path.extname(file)) render(path.extname(file).replace(/^\./, '').toLowerCase(), result, function(err, data) { that.queue(data) that.queue(null) diff --git a/lib/javascript/processors/es.js b/lib/javascript/processors/js.js similarity index 100% rename from lib/javascript/processors/es.js rename to lib/javascript/processors/js.js diff --git a/lib/template/processors/hbs.js b/lib/template/processors/hbs.js new file mode 100644 index 000000000..988f0d9d9 --- /dev/null +++ b/lib/template/processors/hbs.js @@ -0,0 +1,28 @@ +var Handlebars = require('handlebars') +var TerraformError = require("../../error").TerraformError + +module.exports = function(fileContents, options){ + + return { + compile: function(){ + return Handlebars.compile(fileContents.toString(), options) + }, + + parseError: function(error){ + + var arr = error.message.split("\n") + var path_arr = arr[0].split(":") + + error.lineno = parseInt(error.lineno || path_arr[path_arr.length -1] || -1) + error.message = arr[arr.length - 1] + error.name = error.name + error.source = "Handlebars" + error.dest = "HTML" + error.filename = error.path || options.filename + error.stack = fileContents.toString() + + return new TerraformError(error) + } + } + +} diff --git a/package.json b/package.json index a75514647..00427efbd 100644 --- a/package.json +++ b/package.json @@ -67,6 +67,7 @@ "license": "MIT", "dependencies": { "autoprefixer": "5.1.0", + "handlebars": "1.3.0", "browserify": "^10.2.4", "coffee-script": "1.9.2", "ejs": "1.0.0", diff --git a/test/fixtures/javascripts/browserify/comment.es b/test/fixtures/javascripts/browserify/comment.js similarity index 100% rename from test/fixtures/javascripts/browserify/comment.es rename to test/fixtures/javascripts/browserify/comment.js diff --git a/test/fixtures/javascripts/browserify/declared.es b/test/fixtures/javascripts/browserify/declared.js similarity index 100% rename from test/fixtures/javascripts/browserify/declared.es rename to test/fixtures/javascripts/browserify/declared.js diff --git a/test/fixtures/javascripts/browserify/nested/way/in/here/nested.coffee b/test/fixtures/javascripts/browserify/nested/way/in/here/nested.coffee new file mode 100644 index 000000000..f0aef7e8d --- /dev/null +++ b/test/fixtures/javascripts/browserify/nested/way/in/here/nested.coffee @@ -0,0 +1,3 @@ +pow = require('./../../../../Math.js').pow + +console.log pow(4) diff --git a/test/fixtures/javascripts/browserify/nested/way/in/here/nested.js b/test/fixtures/javascripts/browserify/nested/way/in/here/nested.js new file mode 100644 index 000000000..c5026a310 --- /dev/null +++ b/test/fixtures/javascripts/browserify/nested/way/in/here/nested.js @@ -0,0 +1,3 @@ +var pow = require('./../../../../Math.js').pow; + +console.log(pow(4)); diff --git a/test/fixtures/javascripts/browserify/require_coffee.es b/test/fixtures/javascripts/browserify/require_coffee.js similarity index 100% rename from test/fixtures/javascripts/browserify/require_coffee.es rename to test/fixtures/javascripts/browserify/require_coffee.js diff --git a/test/fixtures/javascripts/browserify/require_js.es b/test/fixtures/javascripts/browserify/require_js.js similarity index 100% rename from test/fixtures/javascripts/browserify/require_js.es rename to test/fixtures/javascripts/browserify/require_js.js diff --git a/test/javascripts.js b/test/javascripts.js index 707861573..dd71d0ec4 100644 --- a/test/javascripts.js +++ b/test/javascripts.js @@ -41,13 +41,13 @@ describe("javascripts", function(){ }) }) - + describe("browserify", function() { var root = __dirname + "/fixtures/javascripts/browserify" var poly = polymer.root(root) - + process.chdir(root) - + it("should require coffeescript file in coffeescript", function(done) { poly.render("require_coffee.coffee", function(errors, body) { should.not.exist(errors) @@ -63,14 +63,14 @@ describe("javascripts", function(){ }) }) it("should require coffeescript file in javascript", function(done) { - poly.render("require_coffee.es", function(errors, body) { + poly.render("require_coffee.js", function(errors, body) { should.not.exist(errors) body.should.include("MODULE_NOT_FOUND") done() }) }) it("should require javascript file in javascript", function(done) { - poly.render("require_js.es", function(errors, body) { + poly.render("require_js.js", function(errors, body) { should.not.exist(errors) body.should.include("MODULE_NOT_FOUND") done() @@ -84,7 +84,7 @@ describe("javascripts", function(){ }) }) it("should skip commented require in javascript", function(done) { - poly.render("comment.es", function(errors, body) { + poly.render("comment.js", function(errors, body) { should.not.exist(errors) body.should.not.include("MODULE_NOT_FOUND") done() @@ -98,12 +98,26 @@ describe("javascripts", function(){ }) }) it("should skip already declared require in javascript", function(done) { - poly.render("declared.es", function(errors, body) { + poly.render("declared.js", function(errors, body) { should.not.exist(errors) body.should.not.include("MODULE_NOT_FOUND") done() }) }) + it("should require javascript file in heavily nested coffeescript", function(done) { + poly.render("nested/way/in/here/nested.coffee", function(errors, body) { + should.not.exist(errors) + body.should.include("MODULE_NOT_FOUND") + done() + }) + }) + it("should require javascript file in heavily nested javascript file", function(done) { + poly.render("nested/way/in/here/nested.js", function(errors, body) { + should.not.exist(errors) + body.should.include("MODULE_NOT_FOUND") + done() + }) + }) }) })