From 43745c31eca29a83b04a611877d86a1274581e81 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Josip=20Jan=C5=BEi=C4=87?= Date: Wed, 25 Feb 2015 01:47:05 +0100 Subject: [PATCH 1/2] added ES6 support with babel --- lib/helpers/raw.js | 2 +- lib/javascript/processors/es.js | 19 ++++++ package.json | 72 +++++++++++++++------ test/errors.js | 19 ++++++ test/fixtures/errors/es/invalid.es | 3 + test/fixtures/javascripts/coffee/invalid.es | 4 ++ test/fixtures/javascripts/es/invalid.es | 1 + test/fixtures/javascripts/es/main.es | 5 ++ test/javascripts.js | 31 +++++++++ 9 files changed, 137 insertions(+), 19 deletions(-) create mode 100644 lib/javascript/processors/es.js create mode 100644 test/fixtures/errors/es/invalid.es create mode 100644 test/fixtures/javascripts/coffee/invalid.es create mode 100644 test/fixtures/javascripts/es/invalid.es create mode 100644 test/fixtures/javascripts/es/main.es diff --git a/lib/helpers/raw.js b/lib/helpers/raw.js index ab0531760..1a56ed518 100644 --- a/lib/helpers/raw.js +++ b/lib/helpers/raw.js @@ -15,7 +15,7 @@ var TerraformError = exports.TerraformError = require("../error").TerraformError var processors = exports.processors = { "html": ["jade", "ejs", "md"], "css" : ["styl", "less", "scss", "sass"], - "js" : ["coffee"] + "js" : ["coffee", "es"] } diff --git a/lib/javascript/processors/es.js b/lib/javascript/processors/es.js new file mode 100644 index 000000000..7a39223a2 --- /dev/null +++ b/lib/javascript/processors/es.js @@ -0,0 +1,19 @@ +var babel = require("babel-core") +var TerraformError = require("../../error").TerraformError + +exports.compile = function(filePath, fileContents, callback) { + var script = null + var error = null + try { + script = babel.transform(fileContents.toString()).code + } catch (e) { + e.source = "Babel" + e.dest = "JavaScript" + e.filename = filePath + e.lineno = parseInt(e.loc.line) + error = new TerraformError(e) + if (global.token) delete global.token // remove babel leak + }finally{ + callback(error, script) + } +} diff --git a/package.json b/package.json index b329460f1..55d0f1341 100644 --- a/package.json +++ b/package.json @@ -12,31 +12,67 @@ }, "author": "Brock Whitten ", "contributors": [ - { "name": "Brock Whitten", "email": "brock@chloi.io" }, - { "name": "Brian Donovan", "email": "donovan@squareup.com" }, - { "name": "Kenneth Ormandy", "email": "kenneth@chloi.io" }, - { "name": "Zhang Yichao", "email": "echaozh@gmail.com" }, - { "name": "Carlos Rodriguez" }, - { "name": "Zeke Sikelianos", "email": "zeke@sikelianos.com" }, - { "name": "Guilherme Rodrigues", "email": "gadr90@gmail.com" }, - { "name": "Radu Brehar", "email": "radu@jslog.com" }, - { "name": "Glen Maddern", "email": "glenmaddern@gmail.com" }, - { "name": "Jed Foster", "email": "jed@jedfoster.com" }, - { "name": "Sehrope Sarkuni", "email": "sehrope@jackdb.com" }, - { "name": "Keiichiro Matsumoto", "email": "matsumos@gmail.com" } + { + "name": "Brock Whitten", + "email": "brock@chloi.io" + }, + { + "name": "Brian Donovan", + "email": "donovan@squareup.com" + }, + { + "name": "Kenneth Ormandy", + "email": "kenneth@chloi.io" + }, + { + "name": "Zhang Yichao", + "email": "echaozh@gmail.com" + }, + { + "name": "Carlos Rodriguez" + }, + { + "name": "Zeke Sikelianos", + "email": "zeke@sikelianos.com" + }, + { + "name": "Guilherme Rodrigues", + "email": "gadr90@gmail.com" + }, + { + "name": "Radu Brehar", + "email": "radu@jslog.com" + }, + { + "name": "Glen Maddern", + "email": "glenmaddern@gmail.com" + }, + { + "name": "Jed Foster", + "email": "jed@jedfoster.com" + }, + { + "name": "Sehrope Sarkuni", + "email": "sehrope@jackdb.com" + }, + { + "name": "Keiichiro Matsumoto", + "email": "matsumos@gmail.com" + } ], "license": "MIT", "dependencies": { - "lru-cache": "2.5.0", - "jade": "0.35.0", + "autoprefixer": "5.1.0", + "babel-core": "^4.4.6", "coffee-script": "1.9.0", "ejs": "1.0.0", - "node-sass": "2.0.1", - "marked": "0.3.3", + "jade": "0.35.0", "less": "1.7.5", - "stylus": "0.47.3", + "lru-cache": "2.5.0", + "marked": "0.3.3", "minify": "git://github.com/kennethormandy/minify#v0.3.0", - "autoprefixer": "5.1.0" + "node-sass": "2.0.1", + "stylus": "0.47.3" }, "devDependencies": { "mocha": "1.8.2", diff --git a/test/errors.js b/test/errors.js index 02ff4b414..d104f1e22 100644 --- a/test/errors.js +++ b/test/errors.js @@ -200,6 +200,25 @@ describe("errors", function(){ }) }) + + describe(".es", function(){ + it("should error with invalid file", function(done){ + poly.render("es/invalid.es", function(error, body){ + should.not.exist(body) + should.exist(error) + console.log('err', error, 'body', body); + error.should.have.property('source', "Babel") + error.should.have.property('dest', "JavaScript") + error.should.have.property('lineno', 3) + error.should.have.property('filename') + error.should.have.property('message') + error.should.have.property('stack') + done() + }) + }) + }) + + describe(".scss", function(){ it("should get error if var missing in scss", function(done){ poly.render("scss/novar.scss", function(error, body){ diff --git a/test/fixtures/errors/es/invalid.es b/test/fixtures/errors/es/invalid.es new file mode 100644 index 000000000..c31e61404 --- /dev/null +++ b/test/fixtures/errors/es/invalid.es @@ -0,0 +1,3 @@ + + +foo( \ No newline at end of file diff --git a/test/fixtures/javascripts/coffee/invalid.es b/test/fixtures/javascripts/coffee/invalid.es new file mode 100644 index 000000000..5d3012ed8 --- /dev/null +++ b/test/fixtures/javascripts/coffee/invalid.es @@ -0,0 +1,4 @@ + +foo = function(){ + this shouldnt work +} diff --git a/test/fixtures/javascripts/es/invalid.es b/test/fixtures/javascripts/es/invalid.es new file mode 100644 index 000000000..dd5490778 --- /dev/null +++ b/test/fixtures/javascripts/es/invalid.es @@ -0,0 +1 @@ +foo = function{ !shouldntwork; } diff --git a/test/fixtures/javascripts/es/main.es b/test/fixtures/javascripts/es/main.es new file mode 100644 index 000000000..a2d12ef60 --- /dev/null +++ b/test/fixtures/javascripts/es/main.es @@ -0,0 +1,5 @@ + + + + +let add = (x, y) => { x + y } diff --git a/test/javascripts.js b/test/javascripts.js index 432b16d35..0ff0ca543 100644 --- a/test/javascripts.js +++ b/test/javascripts.js @@ -42,4 +42,35 @@ describe("javascripts", function(){ }) + describe(".es", function() { + + var root = __dirname + "/fixtures/javascripts/es" + var poly = polymer.root(root) + + it("should transpile ES6 to ES5", function(done){ + poly.render("main.es", function(errors, body){ + should.not.exist(errors) + should.exist(body) + done() + }) + }) + it("should minify beyond transpiling", function(done){ + poly.render("main.es", function(errors, body){ + should.not.exist(errors) + body.should.not.include("\n\n") + done() + }) + }) + it("should return errors if invalid", function(done){ + poly.render("invalid.es", function(errors, body){ + should.exist(errors) + should.not.exist(body) + errors.should.have.property("name") + errors.should.have.property("message") + errors.should.have.property("stack") + done() + }) + }) + }) + }) From e62c18d4651628819d3b91bdf1b10753ada8408d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Josip=20Jan=C5=BEi=C4=87?= Date: Thu, 28 May 2015 00:43:48 +0200 Subject: [PATCH 2/2] remove duplicate node-sass dependency --- package.json | 1 - 1 file changed, 1 deletion(-) diff --git a/package.json b/package.json index e3152004f..36bcebac8 100644 --- a/package.json +++ b/package.json @@ -39,7 +39,6 @@ "less": "2.5.0", "stylus": "0.47.3", "minify": "git://github.com/kennethormandy/minify#v0.3.0", - "node-sass": "2.0.1", "stylus": "0.47.3" }, "devDependencies": {