From b426d8833e2b5eb6004b1f2ca80d9a147beab1e4 Mon Sep 17 00:00:00 2001 From: Bernhard Waldbrunner Date: Mon, 22 Jun 2015 02:06:56 +0200 Subject: [PATCH 1/6] Adds support for Browserify --- lib/helpers/memoized.js | 2 +- lib/helpers/raw.js | 13 ++- lib/javascript/index.js | 82 +++++++++++++++---- lib/javascript/processors/es.js | 3 + package.json | 78 +++++++++++++----- .../javascripts/browserify/Math.coffee | 4 + test/fixtures/javascripts/browserify/Math.js | 5 ++ .../javascripts/browserify/comment.coffee | 3 + .../javascripts/browserify/comment.es | 5 ++ .../javascripts/browserify/declared.coffee | 6 ++ .../javascripts/browserify/declared.es | 7 ++ .../browserify/require_coffee.coffee | 3 + .../javascripts/browserify/require_coffee.es | 3 + .../javascripts/browserify/require_js.coffee | 3 + .../javascripts/browserify/require_js.es | 3 + test/javascripts.js | 64 +++++++++++++++ 16 files changed, 246 insertions(+), 38 deletions(-) create mode 100644 lib/javascript/processors/es.js create mode 100644 test/fixtures/javascripts/browserify/Math.coffee create mode 100644 test/fixtures/javascripts/browserify/Math.js create mode 100644 test/fixtures/javascripts/browserify/comment.coffee create mode 100644 test/fixtures/javascripts/browserify/comment.es create mode 100644 test/fixtures/javascripts/browserify/declared.coffee create mode 100644 test/fixtures/javascripts/browserify/declared.es create mode 100644 test/fixtures/javascripts/browserify/require_coffee.coffee create mode 100644 test/fixtures/javascripts/browserify/require_coffee.es create mode 100644 test/fixtures/javascripts/browserify/require_js.coffee create mode 100644 test/fixtures/javascripts/browserify/require_js.es diff --git a/lib/helpers/memoized.js b/lib/helpers/memoized.js index 5b79ebafe..7d04ee32f 100644 --- a/lib/helpers/memoized.js +++ b/lib/helpers/memoized.js @@ -32,4 +32,4 @@ exports.walkData = helpers.walkData exports.isTemplate = helpers.isTemplate exports.isStylesheet = helpers.isStylesheet exports.isJavaScript = helpers.isJavaScript - +exports.needsBrowserify = helpers.needsBrowserify diff --git a/lib/helpers/raw.js b/lib/helpers/raw.js index 5a0bd006b..c51fcf7ff 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"] } @@ -496,3 +496,14 @@ exports.isJavaScript = function(filePath){ return processors["js"].indexOf(ext) !== -1 } + +/** + * needsBrowserify + * + * returns true if the code uses require, exports or module but doesn't declare them + */ + +exports.needsBrowserify = function(source) { + return /^[^#\/'"*]*(require|module|exports)\b/m.test(source) + && !(/\b(function|var|global) +(require|module|exports)\b|\b(module|require) *=[^=]/.test(source)) +} diff --git a/lib/javascript/index.js b/lib/javascript/index.js index d5ec6b6bf..28fe2ec07 100644 --- a/lib/javascript/index.js +++ b/lib/javascript/index.js @@ -1,7 +1,9 @@ -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('minify') +var browserify = require('browserify') +var through = require('through') /** * Build Processor list for javascripts. @@ -13,10 +15,12 @@ var minify = require('minify') * } * */ - var processors = {} + var extensions = [], processors = {} 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 module.exports = function(root, filePath, callback){ @@ -41,18 +45,62 @@ module.exports = function(root, filePath, callback){ * Lookup Directories */ - var render = processors[ext].compile(srcPath, data, function(err, js) { - if (err) return callback(err); - - /** - * Consistently minify - */ - var post = minify.js(js, { - compress: false, - mangle: true - }); - callback(null, post); - }) + var render = function(ext, data, cb) { + processors[ext].compile(srcPath, data, function(err, js) { + if (err) return cb(err) + + /** + * Consistently minify + */ + var post = minify.js(js, { + compress: false, + mangle: true + }) + cb(null, post) + }) + } + + if(helpers.needsBrowserify(data.toString())) { + var post = '', success = true + + var exceptionHandler = function(err) { + success = false + console.log(err.message) + render(ext, data, callback) + } + + process.once('uncaughtException', exceptionHandler) + browserify(filePath, {extensions: extensions}).transform(function(file) { + var result = '' + return through(write, end) + + function write(buf) { + result += buf + } + function end() { + if(success) { + var that = this + render(path.extname(file).replace(/^\./, '').toLowerCase(), result, function(err, data) { + that.queue(data) + that.queue(null) + }) + } + } + }).on('error', exceptionHandler).bundle() + .on('data', function(buf) { + if (success) { + post += buf + } + }).on('end', function() { + if (success) { + process.removeListener('uncaughtException', exceptionHandler) + callback(null, post) + } + }) + } + else { + render(ext, data, callback) + } }) diff --git a/lib/javascript/processors/es.js b/lib/javascript/processors/es.js new file mode 100644 index 000000000..4291f1541 --- /dev/null +++ b/lib/javascript/processors/es.js @@ -0,0 +1,3 @@ +exports.compile = function(filePath, fileContents, callback){ + callback(null, fileContents.toString()) +} diff --git a/package.json b/package.json index d74702f8f..a75514647 100644 --- a/package.json +++ b/package.json @@ -12,32 +12,72 @@ }, "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": "Najam Khn", "email": "najamkhn@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" + }, + { + "name": "Najam Khn", + "email": "najamkhn@gmail.com" + } ], "license": "MIT", "dependencies": { - "lru-cache": "2.6.1", - "jade": "git://github.com/harp/jade#v1.9.3-bc.2", + "autoprefixer": "5.1.0", + "browserify": "^10.2.4", "coffee-script": "1.9.2", "ejs": "1.0.0", - "node-sass": "3.0.0-beta.5", - "marked": "0.3.3", + "jade": "git://github.com/harp/jade#v1.9.3-bc.2", "less": "2.5.0", - "stylus": "0.47.3", + "lru-cache": "2.6.1", + "marked": "0.3.3", "minify": "git://github.com/kennethormandy/minify#v0.3.0", - "autoprefixer": "5.1.0" + "node-sass": "3.0.0-beta.5", + "stylus": "0.47.3", + "through": "^2.3.7" }, "devDependencies": { "mocha": "1.8.2", diff --git a/test/fixtures/javascripts/browserify/Math.coffee b/test/fixtures/javascripts/browserify/Math.coffee new file mode 100644 index 000000000..ee5932f46 --- /dev/null +++ b/test/fixtures/javascripts/browserify/Math.coffee @@ -0,0 +1,4 @@ +# Let's see if we can mix .coffee & .es + +exports.pow = (num) -> + num * num diff --git a/test/fixtures/javascripts/browserify/Math.js b/test/fixtures/javascripts/browserify/Math.js new file mode 100644 index 000000000..c462e3352 --- /dev/null +++ b/test/fixtures/javascripts/browserify/Math.js @@ -0,0 +1,5 @@ +// Let's see if we can mix .es & .js + +exports.pow = function(num) { + return num * num; +}; diff --git a/test/fixtures/javascripts/browserify/comment.coffee b/test/fixtures/javascripts/browserify/comment.coffee new file mode 100644 index 000000000..a1fb7b864 --- /dev/null +++ b/test/fixtures/javascripts/browserify/comment.coffee @@ -0,0 +1,3 @@ +# pow = require('./Math').pow; + +console.log(pow(4)); diff --git a/test/fixtures/javascripts/browserify/comment.es b/test/fixtures/javascripts/browserify/comment.es new file mode 100644 index 000000000..93418d714 --- /dev/null +++ b/test/fixtures/javascripts/browserify/comment.es @@ -0,0 +1,5 @@ +/* + * pow = require('./Math').pow; + */ + +console.log(pow(4)); diff --git a/test/fixtures/javascripts/browserify/declared.coffee b/test/fixtures/javascripts/browserify/declared.coffee new file mode 100644 index 000000000..c209946aa --- /dev/null +++ b/test/fixtures/javascripts/browserify/declared.coffee @@ -0,0 +1,6 @@ +require = (file) -> + # custom implementation + +pow = require('./Math').pow + +console.log pow(4) diff --git a/test/fixtures/javascripts/browserify/declared.es b/test/fixtures/javascripts/browserify/declared.es new file mode 100644 index 000000000..40484a0fb --- /dev/null +++ b/test/fixtures/javascripts/browserify/declared.es @@ -0,0 +1,7 @@ +var require = function(file) { + // custom implementation +}; + +var pow = require('./Math').pow; + +console.log(pow(4)); diff --git a/test/fixtures/javascripts/browserify/require_coffee.coffee b/test/fixtures/javascripts/browserify/require_coffee.coffee new file mode 100644 index 000000000..43b6e2e87 --- /dev/null +++ b/test/fixtures/javascripts/browserify/require_coffee.coffee @@ -0,0 +1,3 @@ +pow = require('./Math.coffee').pow + +console.log pow(4) diff --git a/test/fixtures/javascripts/browserify/require_coffee.es b/test/fixtures/javascripts/browserify/require_coffee.es new file mode 100644 index 000000000..86a4bc2a3 --- /dev/null +++ b/test/fixtures/javascripts/browserify/require_coffee.es @@ -0,0 +1,3 @@ +var pow = require('./Math.coffee').pow; + +console.log(pow(4)); diff --git a/test/fixtures/javascripts/browserify/require_js.coffee b/test/fixtures/javascripts/browserify/require_js.coffee new file mode 100644 index 000000000..614e2afc8 --- /dev/null +++ b/test/fixtures/javascripts/browserify/require_js.coffee @@ -0,0 +1,3 @@ +pow = require('./Math.js').pow + +console.log pow(4) diff --git a/test/fixtures/javascripts/browserify/require_js.es b/test/fixtures/javascripts/browserify/require_js.es new file mode 100644 index 000000000..2d2e69f5e --- /dev/null +++ b/test/fixtures/javascripts/browserify/require_js.es @@ -0,0 +1,3 @@ +var pow = require('./Math.js').pow; + +console.log(pow(4)); diff --git a/test/javascripts.js b/test/javascripts.js index 432b16d35..707861573 100644 --- a/test/javascripts.js +++ b/test/javascripts.js @@ -41,5 +41,69 @@ 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) + body.should.include("MODULE_NOT_FOUND") + done() + }) + }) + it("should require javascript file in coffeescript", function(done) { + poly.render("require_js.coffee", function(errors, body) { + should.not.exist(errors) + body.should.include("MODULE_NOT_FOUND") + done() + }) + }) + it("should require coffeescript file in javascript", function(done) { + poly.render("require_coffee.es", 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) { + should.not.exist(errors) + body.should.include("MODULE_NOT_FOUND") + done() + }) + }) + it("should skip commented require in coffeescript", function(done) { + poly.render("comment.coffee", function(errors, body) { + should.not.exist(errors) + body.should.not.include("MODULE_NOT_FOUND") + done() + }) + }) + it("should skip commented require in javascript", function(done) { + poly.render("comment.es", function(errors, body) { + should.not.exist(errors) + body.should.not.include("MODULE_NOT_FOUND") + done() + }) + }) + it("should skip already declared require in coffeescript", function(done) { + poly.render("declared.coffee", function(errors, body) { + should.not.exist(errors) + body.should.not.include("MODULE_NOT_FOUND") + done() + }) + }) + it("should skip already declared require in javascript", function(done) { + poly.render("declared.es", function(errors, body) { + should.not.exist(errors) + body.should.not.include("MODULE_NOT_FOUND") + done() + }) + }) + }) }) From b04067fc0e44bd3320af9896d72d83e5c91504fd Mon Sep 17 00:00:00 2001 From: Bernhard Waldbrunner Date: Sun, 28 Jun 2015 21:49:47 +0200 Subject: [PATCH 2/6] Already part of PR #98 --- lib/javascript/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/javascript/index.js b/lib/javascript/index.js index 28fe2ec07..1e4a7534d 100644 --- a/lib/javascript/index.js +++ b/lib/javascript/index.js @@ -70,7 +70,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) From 0027b86529104f558205e0989644913abcc4d354 Mon Sep 17 00:00:00 2001 From: Bernhard Waldbrunner Date: Sun, 28 Jun 2015 23:34:44 +0200 Subject: [PATCH 3/6] Exception handling for multiple missing required files --- lib/javascript/index.js | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/lib/javascript/index.js b/lib/javascript/index.js index 1e4a7534d..798ef740d 100644 --- a/lib/javascript/index.js +++ b/lib/javascript/index.js @@ -15,7 +15,8 @@ var through = require('through') * } * */ - var extensions = [], processors = {} +var extensions = [], processors = {}, exceptionHandler = null + helpers.processors["js"].forEach(function(sourceType){ extensions.push('.' + sourceType) processors[sourceType] = require("./processors/" + sourceType) @@ -63,13 +64,19 @@ module.exports = function(root, filePath, callback){ if(helpers.needsBrowserify(data.toString())) { var post = '', success = true - var exceptionHandler = function(err) { - success = false + if (exceptionHandler) { + process.removeListener('uncaughtException', exceptionHandler) + } + exceptionHandler = function(err) { console.log(err.message) - render(ext, data, callback) + if (success) { + success = false + render(ext, data, callback) + } } - process.once('uncaughtException', exceptionHandler) + process.on('uncaughtException', exceptionHandler) + browserify(srcPath, {extensions: extensions}).transform(function(file) { var result = '' return through(write, end) @@ -93,7 +100,6 @@ module.exports = function(root, filePath, callback){ } }).on('end', function() { if (success) { - process.removeListener('uncaughtException', exceptionHandler) callback(null, post) } }) From ad808048442af2e29b814ae4519ec1b4e5258f2d Mon Sep 17 00:00:00 2001 From: Bernhard Waldbrunner Date: Wed, 29 Jul 2015 01:24:55 +0200 Subject: [PATCH 4/6] Support for Hjson & CSON added --- lib/helpers/memoized.js | 3 +- lib/helpers/raw.js | 13 +++++- lib/json/index.js | 54 +++++++++++++++++++++++++ lib/json/processors/cson.js | 23 +++++++++++ lib/json/processors/hjson.js | 24 +++++++++++ lib/terraform.js | 3 ++ package.json | 2 + test/errors.js | 32 +++++++++++++++ test/fixtures/errors/json/invalid.cson | 1 + test/fixtures/errors/json/invalid.hjson | 3 ++ test/fixtures/json/cson/data.cson | 40 ++++++++++++++++++ test/fixtures/json/cson/invalid.cson | 1 + test/fixtures/json/hjson/data.hjson | 16 ++++++++ test/fixtures/json/hjson/invalid.hjson | 3 ++ test/helpers.js | 11 ++--- test/json.js | 53 ++++++++++++++++++++++++ 16 files changed, 275 insertions(+), 7 deletions(-) create mode 100644 lib/json/index.js create mode 100644 lib/json/processors/cson.js create mode 100644 lib/json/processors/hjson.js create mode 100644 test/fixtures/errors/json/invalid.cson create mode 100644 test/fixtures/errors/json/invalid.hjson create mode 100644 test/fixtures/json/cson/data.cson create mode 100644 test/fixtures/json/cson/invalid.cson create mode 100644 test/fixtures/json/hjson/data.hjson create mode 100644 test/fixtures/json/hjson/invalid.hjson create mode 100644 test/json.js diff --git a/lib/helpers/memoized.js b/lib/helpers/memoized.js index 7d04ee32f..73ab01eb1 100644 --- a/lib/helpers/memoized.js +++ b/lib/helpers/memoized.js @@ -32,4 +32,5 @@ exports.walkData = helpers.walkData exports.isTemplate = helpers.isTemplate exports.isStylesheet = helpers.isStylesheet exports.isJavaScript = helpers.isJavaScript -exports.needsBrowserify = helpers.needsBrowserify +exports.needsBrowserify = helpers.needsBrowserify +exports.isJSON = helpers.isJSON diff --git a/lib/helpers/raw.js b/lib/helpers/raw.js index c51fcf7ff..aba9df230 100644 --- a/lib/helpers/raw.js +++ b/lib/helpers/raw.js @@ -15,7 +15,8 @@ var TerraformError = exports.TerraformError = require("../error").TerraformError var processors = exports.processors = { "html": ["jade", "ejs", "md"], "css" : ["styl", "less", "scss", "sass"], - "js" : ["coffee", "es"] + "js" : ["coffee", "es"], + "json": ["hjson", "cson"] } @@ -507,3 +508,13 @@ exports.needsBrowserify = function(source) { return /^[^#\/'"*]*(require|module|exports)\b/m.test(source) && !(/\b(function|var|global) +(require|module|exports)\b|\b(module|require) *=[^=]/.test(source)) } + +/** + * isJSON(filePath) + * + * returns true if file is a pre-processor JSON file + */ +exports.isJSON = function(filePath) { + var ext = path.extname(filePath).replace(/^\./, '') + return processors['json'].indexOf(ext) !== -1 +} diff --git a/lib/json/index.js b/lib/json/index.js new file mode 100644 index 000000000..68954c1ec --- /dev/null +++ b/lib/json/index.js @@ -0,0 +1,54 @@ +var path = require("path") +var fs = require("fs") +var helpers = require('../helpers') + +/** + * Build Processor list for JSON data. + * + * same as doing... + * + * var processors = { + * "cson" : require("./processors/cson") + * } + * + */ +var extensions = [], processors = {}, exceptionHandler = null + +helpers.processors["json"].forEach(function(sourceType){ + extensions.push('.' + sourceType) + processors[sourceType] = require("./processors/" + sourceType) +}) + +module.exports = function(root, filePath, callback){ + + var srcPath = path.resolve(root, filePath) + var ext = path.extname(srcPath).replace(/^\./, '') + + fs.readFile(srcPath, function(err, data){ + + /** + * File not Found + */ + + if(err && err.code === 'ENOENT') return callback(null, null) + + /** + * Read File Error + */ + + if(err) return callback(err) + + /** + * Lookup Directories + */ + + processors[ext].compile(srcPath, data, function(err, obj) { + if (err) return callback(err) + + var post = JSON.stringify(obj) + callback(null, post) + }) + + }) + +} diff --git a/lib/json/processors/cson.js b/lib/json/processors/cson.js new file mode 100644 index 000000000..9211ced14 --- /dev/null +++ b/lib/json/processors/cson.js @@ -0,0 +1,23 @@ +var CSON = require("cson") +var TerraformError = require("../../error").TerraformError + +exports.compile = function(filePath, fileContents, callback){ + + var formatError = function(e){ + return new TerraformError({ + source: "CSON", + dest: "JSON", + lineno: parseInt(e.line || -1), + name: e.type + "Error", + filename: filePath, + message: e.message, + stack: fileContents.toString() + }) + } + + CSON.parse(fileContents.toString(), {}, function(err, obj) { + if (err) + return callback(formatError(err)) + callback(null, obj) + }) +} diff --git a/lib/json/processors/hjson.js b/lib/json/processors/hjson.js new file mode 100644 index 000000000..52d146975 --- /dev/null +++ b/lib/json/processors/hjson.js @@ -0,0 +1,24 @@ +var Hjson = require("hjson") +var TerraformError = require("../../error").TerraformError + +exports.compile = function(filePath, fileContents, callback){ + + var formatError = function(e){ + return new TerraformError({ + source: "Hjson", + dest: "JSON", + lineno: parseInt(e.line || -1), + name: e.type + "Error", + filename: filePath, + message: e.message, + stack: fileContents.toString() + }) + } + + try { + var obj = Hjson.parse(fileContents.toString()) + callback(null, obj) + } catch (ex) { + callback(formatError(ex)) + } +} diff --git a/lib/terraform.js b/lib/terraform.js index 3e386f06a..97afcb5fe 100644 --- a/lib/terraform.js +++ b/lib/terraform.js @@ -3,6 +3,7 @@ var path = require('path') var stylesheet = require('./stylesheet') var template = require('./template') var javascript = require('./javascript') +var json = require('./json') var helpers = require('./helpers') @@ -147,6 +148,8 @@ exports.root = function(root, globals){ stylesheet(root, filePath, callback) }else if(helpers.isJavaScript(filePath)){ javascript(root, filePath, callback) + }else if(helpers.isJSON(filePath)) { + json(root, filePath, callback) }else{ callback(null, null) } diff --git a/package.json b/package.json index a75514647..334a0e4e5 100644 --- a/package.json +++ b/package.json @@ -69,7 +69,9 @@ "autoprefixer": "5.1.0", "browserify": "^10.2.4", "coffee-script": "1.9.2", + "cson": "^3.0.1", "ejs": "1.0.0", + "hjson": "^1.7.3", "jade": "git://github.com/harp/jade#v1.9.3-bc.2", "less": "2.5.0", "lru-cache": "2.6.1", diff --git a/test/errors.js b/test/errors.js index 02ff4b414..19fe21c4f 100644 --- a/test/errors.js +++ b/test/errors.js @@ -259,5 +259,37 @@ describe("errors", function(){ }) }) }) + + describe(".hjson", function() { + it("should get error if property name is missing", function(done) { + poly.render("json/invalid.hjson", function(error, body) { + should.not.exist(body) + should.exist(error) + error.should.have.property('source') + error.should.have.property('dest') + error.should.have.property('lineno') + error.should.have.property('filename') + error.should.have.property('message') + error.should.have.property('stack') + done() + }) + }) + }) + + describe(".cson", function() { + it("should get error if syntax is invalid", function(done) { + poly.render("json/invalid.cson", function(error, body) { + should.not.exist(body) + should.exist(error) + error.should.have.property('source') + error.should.have.property('dest') + error.should.have.property('lineno') + error.should.have.property('filename') + error.should.have.property('message') + error.should.have.property('stack') + done() + }) + }) + }) }) diff --git a/test/fixtures/errors/json/invalid.cson b/test/fixtures/errors/json/invalid.cson new file mode 100644 index 000000000..9845617d9 --- /dev/null +++ b/test/fixtures/errors/json/invalid.cson @@ -0,0 +1 @@ +prop: ++ diff --git a/test/fixtures/errors/json/invalid.hjson b/test/fixtures/errors/json/invalid.hjson new file mode 100644 index 000000000..a08b2f8e3 --- /dev/null +++ b/test/fixtures/errors/json/invalid.hjson @@ -0,0 +1,3 @@ +{ + : +} diff --git a/test/fixtures/json/cson/data.cson b/test/fixtures/json/cson/data.cson new file mode 100644 index 000000000..9d91420c4 --- /dev/null +++ b/test/fixtures/json/cson/data.cson @@ -0,0 +1,40 @@ +# Comments!!! + +# An Array with no commas! +greatDocumentaries: [ + 'earthlings.com' + 'forksoverknives.com' + 'cowspiracy.com' +] + +# An Object without braces! +importantFacts: + # Multi-Line Strings! Without Quote Escaping! + emissions: ''' + Livestock and their byproducts account for at least 32,000 million tons of carbon dioxide (CO2) per year, or 51% of all worldwide greenhouse gas emissions. + Goodland, R Anhang, J. “Livestock and Climate Change: What if the key actors in climate change were pigs, chickens and cows?” + WorldWatch, November/December 2009. Worldwatch Institute, Washington, DC, USA. Pp. 10–19. + http://www.worldwatch.org/node/6294 + ''' + + landuse: ''' + Livestock covers 45% of the earth’s total land. + Thornton, Phillip, Mario Herrero, and Polly Ericksen. “Livestock and Climate Change.” Livestock Exchange, no. 3 (2011). + https://cgspace.cgiar.org/bitstream/handle/10568/10601/IssueBrief3.pdf + ''' + + burger: ''' + One hamburger requires 660 gallons of water to produce – the equivalent of 2 months’ worth of showers. + Catanese, Christina. “Virtual Water, Real Impacts.” Greenversations: Official Blog of the U.S. EPA. 2012. + http://blog.epa.gov/healthywaters/2012/03/virtual-water-real-impacts-world-water-day-2012/ + “50 Ways to Save Your River.” Friends of the River. + http://www.friendsoftheriver.org/site/PageServer?pagename=50ways + ''' + + milk: ''' + 1,000 gallons of water are required to produce 1 gallon of milk. + “Water trivia facts.” United States Environmental Protection Agency. + http://water.epa.gov/learn/kids/drinkingwater/water_trivia_facts.cfm#_edn11 + ''' + + more: 'http://cowspiracy.com/facts' diff --git a/test/fixtures/json/cson/invalid.cson b/test/fixtures/json/cson/invalid.cson new file mode 100644 index 000000000..9845617d9 --- /dev/null +++ b/test/fixtures/json/cson/invalid.cson @@ -0,0 +1 @@ +prop: ++ diff --git a/test/fixtures/json/hjson/data.hjson b/test/fixtures/json/hjson/data.hjson new file mode 100644 index 000000000..2ec2d4fd6 --- /dev/null +++ b/test/fixtures/json/hjson/data.hjson @@ -0,0 +1,16 @@ +{ + # specify rate in requests/second (because comments are helpful!) + rate: 1000 + + // prefer c-style comments? + /* feeling old fashioned? */ + + # did you notice that rate doesn't need quotes? + hey: look ma, no quotes for strings either! + + # best of all + notice: [] + anything: ? + + # yes, commas are optional! +} diff --git a/test/fixtures/json/hjson/invalid.hjson b/test/fixtures/json/hjson/invalid.hjson new file mode 100644 index 000000000..a08b2f8e3 --- /dev/null +++ b/test/fixtures/json/hjson/invalid.hjson @@ -0,0 +1,3 @@ +{ + : +} diff --git a/test/helpers.js b/test/helpers.js index f7dbf3075..1be6c17c0 100644 --- a/test/helpers.js +++ b/test/helpers.js @@ -34,11 +34,12 @@ describe("helpers", function(){ it('should look for templates on json files.', function(done){ var list = polymer.helpers.buildPriorityList('profile.json') list.should.be.an.instanceOf(Array) - list.should.have.lengthOf(3) - list.should.include('profile.json.jade') - list.should.include('profile.json.ejs') - list.should.include('profile.json.md') - list.should.eql('profile.json.jade, profile.json.ejs, profile.json.md'. split(', ')) + list.should.have.lengthOf(4) + list.should.include('profile.hjson') + list.should.include('profile.cson') + list.should.include('profile.json.hjson') + list.should.include('profile.json.cson') + list.should.eql('profile.hjson, profile.cson, profile.json.hjson, profile.json.cson'. split(', ')) done() }) diff --git a/test/json.js b/test/json.js new file mode 100644 index 000000000..feb678423 --- /dev/null +++ b/test/json.js @@ -0,0 +1,53 @@ +var should = require('should') +var polymer = require('../') + +describe("JSON", function(){ + + describe(".cson", function(){ + var root = __dirname + "/fixtures/json/cson" + var poly = polymer.root(root) + + it("should convert CSON to JSON", function(done){ + poly.render("data.cson", function(errors, body){ + should.not.exist(errors) + should.exist(body) + done() + }) + }) + it("should return errors if syntax is invalid", function(done){ + poly.render("invalid.cson", 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() + }) + }) + + }) + + describe(".hjson", function() { + var root = __dirname + "/fixtures/json/hjson" + var poly = polymer.root(root) + + it("should convert Hjson to JSON", function(done){ + poly.render("data.hjson", function(errors, body){ + should.not.exist(errors) + should.exist(body) + done() + }) + }) + it("should return errors if property name is missing", function(done){ + poly.render("invalid.hjson", 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 e8517fcab92666d0fc769c06cb8ffc879cc309fc Mon Sep 17 00:00:00 2001 From: Bernhard Waldbrunner Date: Sun, 9 Aug 2015 19:19:49 +0200 Subject: [PATCH 5/6] Added support for HJSON/CSON data files --- README.md | 5 +- lib/helpers/raw.js | 84 ++++++--- lib/json/processors/hjson.js | 6 +- test/data.js | 160 +++++++++++++++++- test/fixtures/data-cson/invalid/_data.cson | 7 + .../data-cson/valid/.foo/placeholder.txt | 1 + test/fixtures/data-cson/valid/_layout.jade | 3 + .../data-cson/valid/articles/_data.cson | 10 ++ .../valid/articles/hello-jupiter.jade | 4 + .../data-cson/valid/articles/hello-pluto.jade | 3 + .../data-cson/valid/articles/hello-world.jade | 4 + test/fixtures/data-cson/valid/index.jade | 6 + test/fixtures/data-cson/valid/pub.json.jade | 1 + test/fixtures/data-hjson/invalid/_data.hjson | 10 ++ .../data-hjson/valid/.foo/placeholder.txt | 1 + test/fixtures/data-hjson/valid/_layout.jade | 3 + .../data-hjson/valid/articles/_data.hjson | 13 ++ .../valid/articles/hello-jupiter.jade | 4 + .../valid/articles/hello-pluto.jade | 3 + .../valid/articles/hello-world.jade | 4 + test/fixtures/data-hjson/valid/index.jade | 6 + test/fixtures/data-hjson/valid/pub.json.jade | 1 + test/helpers.js | 11 +- 23 files changed, 321 insertions(+), 29 deletions(-) create mode 100644 test/fixtures/data-cson/invalid/_data.cson create mode 100644 test/fixtures/data-cson/valid/.foo/placeholder.txt create mode 100644 test/fixtures/data-cson/valid/_layout.jade create mode 100644 test/fixtures/data-cson/valid/articles/_data.cson create mode 100644 test/fixtures/data-cson/valid/articles/hello-jupiter.jade create mode 100644 test/fixtures/data-cson/valid/articles/hello-pluto.jade create mode 100644 test/fixtures/data-cson/valid/articles/hello-world.jade create mode 100644 test/fixtures/data-cson/valid/index.jade create mode 100644 test/fixtures/data-cson/valid/pub.json.jade create mode 100644 test/fixtures/data-hjson/invalid/_data.hjson create mode 100644 test/fixtures/data-hjson/valid/.foo/placeholder.txt create mode 100644 test/fixtures/data-hjson/valid/_layout.jade create mode 100644 test/fixtures/data-hjson/valid/articles/_data.hjson create mode 100644 test/fixtures/data-hjson/valid/articles/hello-jupiter.jade create mode 100644 test/fixtures/data-hjson/valid/articles/hello-pluto.jade create mode 100644 test/fixtures/data-hjson/valid/articles/hello-world.jade create mode 100644 test/fixtures/data-hjson/valid/index.jade create mode 100644 test/fixtures/data-hjson/valid/pub.json.jade diff --git a/README.md b/README.md index d1057940f..e32dda67a 100644 --- a/README.md +++ b/README.md @@ -4,10 +4,10 @@ ## Features -- pre-processorse +- pre-processors - layouts - partials -- metadata (via _data.json) +- metadata (via \_data.json/\_data.cson/\_data.hjson) - LRU cache (production mode) ### Supported Pre-Processors @@ -15,6 +15,7 @@ **HTML** – EJS, Jade, Markdown **CSS** – LESS, Stylus, Sass (SCSS) **JavaScript** – CoffeeScript +**JSON** - CSON, Hjson ## Install diff --git a/lib/helpers/raw.js b/lib/helpers/raw.js index aba9df230..1bbaf676e 100644 --- a/lib/helpers/raw.js +++ b/lib/helpers/raw.js @@ -1,6 +1,8 @@ var path = require('path') var fs = require('fs') var TerraformError = exports.TerraformError = require("../error").TerraformError +var HJSON = require('hjson') +var CSON = require('cson') /** @@ -16,7 +18,7 @@ var processors = exports.processors = { "html": ["jade", "ejs", "md"], "css" : ["styl", "less", "scss", "sass"], "js" : ["coffee", "es"], - "json": ["hjson", "cson"] + "json": ["cson", "hjson"] } @@ -63,6 +65,13 @@ var buildPriorityList = exports.buildPriorityList = function(filePath){ if(processor){ + if (ext === 'json') { + // foo.json => foo.json.jade + processors['html'].forEach(function(p){ + list.push(filePath + '.' + p) + }) + } + // foo.html => foo.jade processor.forEach(function(p){ var regexp = new RegExp(ext + '$') @@ -204,7 +213,9 @@ var isEmpty = function(obj) { */ var dataTree = exports.dataTree = function (filename) { - var dirPath = path.resolve(filename) + var i, file, files, exception, e, format, dataPath, fileData, data + var dirPath = path.resolve(filename) + try{ var list = fs.readdirSync(dirPath) }catch(e){ @@ -219,25 +230,60 @@ var dataTree = exports.dataTree = function (filename) { var obj = {} obj._contents = [] - try{ - var dataPath = path.resolve(dirPath, "_data.json") - var fileData = fs.readFileSync(dataPath) - obj._data = JSON.parse(fileData) - }catch(e){ - if(e.code && e.code === "ENOENT"){ - // data file failed or does not exist - }else{ - e.source = "Data" - e.dest = "Globals" - e.lineno = -1 - e.filename = dataPath - e.stack = fileData.toString() - throw new TerraformError(e) + files = ["_data.json"] + + processors['json'].forEach(function(p) { + files.push("_data." + p) + }) + files.push("") + + exception = true + for (i = 0; i < files.length && exception; ++i) { + file = files[i] + exception = false + try { + if (file) { + format = path.extname(file).substring(1).toUpperCase() + dataPath = path.resolve(dirPath, file) + fileData = fs.readFileSync(dataPath).toString() + + if (!fileData || fileData.replace(/^\s\s*/, '').replace(/\s\s*$/, '') == '') { + fileData = "" + } else { + // attempt to parse the file + switch(format) { + case "CSON": + data = CSON.parse(fileData) + if (data.constructor != Object) throw data + obj._data = data + break + case "HJSON": + obj._data = HJSON.parse(fileData) + break + default: + obj._data = JSON.parse(fileData) + } + } + } else { + // No data file found + format = "" + fileData = "" + dataPath = null + } + } catch(e) { + if (e.code === "ENOENT") { + exception = true // file not readable or does not exists + } else { + e.source = format + " Data" + e.dest = "Globals" + e.lineno = -1 + e.filename = dataPath + e.stack = fileData + throw new TerraformError(e) + } } - //console.log(e.code) - } - + list.forEach(function(file){ var filePath = path.resolve(dirPath, file) var stat = fs.statSync(filePath) diff --git a/lib/json/processors/hjson.js b/lib/json/processors/hjson.js index 52d146975..6a4b5067a 100644 --- a/lib/json/processors/hjson.js +++ b/lib/json/processors/hjson.js @@ -1,11 +1,11 @@ -var Hjson = require("hjson") +var HJSON = require("hjson") var TerraformError = require("../../error").TerraformError exports.compile = function(filePath, fileContents, callback){ var formatError = function(e){ return new TerraformError({ - source: "Hjson", + source: "HJSON", dest: "JSON", lineno: parseInt(e.line || -1), name: e.type + "Error", @@ -16,7 +16,7 @@ exports.compile = function(filePath, fileContents, callback){ } try { - var obj = Hjson.parse(fileContents.toString()) + var obj = HJSON.parse(fileContents.toString()) callback(null, obj) } catch (ex) { callback(formatError(ex)) diff --git a/test/data.js b/test/data.js index b1f78d65d..f7e840d04 100644 --- a/test/data.js +++ b/test/data.js @@ -55,7 +55,7 @@ describe("data", function(){ var poly = polymer.root(root) }catch(error){ should.exist(error) - error.should.have.property('source', "Data") + error.should.have.property('source', "JSON Data") error.should.have.property('dest', "Globals") error.should.have.property('lineno') error.should.have.property('filename') @@ -97,3 +97,161 @@ describe("data", function(){ }) }) + +describe("data-cson", function(){ + + describe("valid", function(){ + var root = __dirname + "/fixtures/data-cson/valid" + var poly = polymer.root(root) + + it("should be available in the layouts", function(done){ + poly.render("index.jade", function(error, body){ + should.not.exist(error) + should.exist(body) + body.should.include("

My Articles

") + body.should.include('
Earth people, New York to California
') + done() + }) + }) + + it("should be available in the template", function(done){ + poly.render("articles/hello-jupiter.jade", function(error, body){ + should.not.exist(error) + should.exist(body) + body.should.include("

I was born on Jupiter

") + body.should.include("

Brock Whitten

") + done() + }) + }) + + it("should handle escaped html", function(done){ + poly.render("articles/hello-pluto.jade", function(error, body){ + should.not.exist(error) + should.exist(body) + body.should.include("

Harp

") + done() + }) + }) + + + it("should be available to override data when calling partial", function(done){ + poly.render("index.jade", function(error, body){ + should.not.exist(error) + should.exist(body) + body.should.include("

I was born on Jupiter

") + body.should.include("

Kool Keith

") + done() + }) + }) + }) + + describe("invalid", function(){ + it("should return errors when invalid _data.cson file", function(done){ + var root = __dirname + "/fixtures/data-cson/invalid" + try{ + var poly = polymer.root(root) + }catch(error){ + should.exist(error) + error.should.have.property('source', "CSON Data") + error.should.have.property('dest', "Globals") + error.should.have.property('lineno') + error.should.have.property('filename') + error.should.have.property('message') + error.should.have.property('stack') + done() + } + }) + }) + + describe("public", function(){ + it("should return public object", function(done){ + var root = __dirname + "/fixtures/data-cson/valid" + var poly = polymer.root(root) + poly.render("pub.json.jade", { "layout": false }, function(err, result){ + var pub = JSON.parse(result) + should.not.exist(pub[".foo"]) + done() + }) + }) + }) + +}) + +describe("data-hjson", function(){ + + describe("valid", function(){ + var root = __dirname + "/fixtures/data-hjson/valid" + var poly = polymer.root(root) + + it("should be available in the layouts", function(done){ + poly.render("index.jade", function(error, body){ + should.not.exist(error) + should.exist(body) + body.should.include("

My Articles

") + body.should.include('
Earth people, New York to California
') + done() + }) + }) + + it("should be available in the template", function(done){ + poly.render("articles/hello-jupiter.jade", function(error, body){ + should.not.exist(error) + should.exist(body) + body.should.include("

I was born on Jupiter

") + body.should.include("

Brock Whitten

") + done() + }) + }) + + it("should handle escaped html", function(done){ + poly.render("articles/hello-pluto.jade", function(error, body){ + should.not.exist(error) + should.exist(body) + body.should.include("

Harp

") + done() + }) + }) + + + it("should be available to override data when calling partial", function(done){ + poly.render("index.jade", function(error, body){ + should.not.exist(error) + should.exist(body) + body.should.include("

I was born on Jupiter

") + body.should.include("

Kool Keith

") + done() + }) + }) + }) + + describe("invalid", function(){ + it("should return errors when invalid _data.hjson file", function(done){ + var root = __dirname + "/fixtures/data-hjson/invalid" + try{ + var poly = polymer.root(root) + }catch(error){ + should.exist(error) + error.should.have.property('source', "HJSON Data") + error.should.have.property('dest', "Globals") + error.should.have.property('lineno') + error.should.have.property('filename') + error.should.have.property('message') + error.should.have.property('stack') + done() + } + }) + }) + + describe("public", function(){ + it("should return public object", function(done){ + var root = __dirname + "/fixtures/data-hjson/valid" + var poly = polymer.root(root) + poly.render("pub.json.jade", { "layout": false }, function(err, result){ + var pub = JSON.parse(result) + should.not.exist(pub[".foo"]) + done() + }) + }) + }) + +}) diff --git a/test/fixtures/data-cson/invalid/_data.cson b/test/fixtures/data-cson/invalid/_data.cson new file mode 100644 index 000000000..aa0202d32 --- /dev/null +++ b/test/fixtures/data-cson/invalid/_data.cson @@ -0,0 +1,7 @@ +hello-world: + title: "Earth people, New York to California" + author: "Brock Whitten" + +"hello-jupiter" + title: "I was born on Jupiter" + author: "Brock Whitten" diff --git a/test/fixtures/data-cson/valid/.foo/placeholder.txt b/test/fixtures/data-cson/valid/.foo/placeholder.txt new file mode 100644 index 000000000..5c532474c --- /dev/null +++ b/test/fixtures/data-cson/valid/.foo/placeholder.txt @@ -0,0 +1 @@ +This file is here to test ignoring directories that begin with a dot (".") diff --git a/test/fixtures/data-cson/valid/_layout.jade b/test/fixtures/data-cson/valid/_layout.jade new file mode 100644 index 000000000..e46014a31 --- /dev/null +++ b/test/fixtures/data-cson/valid/_layout.jade @@ -0,0 +1,3 @@ +h1 My Articles +h5.feature= public.articles._data['hello-world'].title +!= yield \ No newline at end of file diff --git a/test/fixtures/data-cson/valid/articles/_data.cson b/test/fixtures/data-cson/valid/articles/_data.cson new file mode 100644 index 000000000..f27294a6c --- /dev/null +++ b/test/fixtures/data-cson/valid/articles/_data.cson @@ -0,0 +1,10 @@ +"hello-world": + title: "Earth people, New York to California" + author: "Brock Whitten" + +"hello-jupiter": + title: "I was born on Jupiter" + author: "Brock Whitten" + +"hello-pluto": + title: 'Harp' diff --git a/test/fixtures/data-cson/valid/articles/hello-jupiter.jade b/test/fixtures/data-cson/valid/articles/hello-jupiter.jade new file mode 100644 index 000000000..8cf7296e9 --- /dev/null +++ b/test/fixtures/data-cson/valid/articles/hello-jupiter.jade @@ -0,0 +1,4 @@ +h3= title +h4= author + +p Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. \ No newline at end of file diff --git a/test/fixtures/data-cson/valid/articles/hello-pluto.jade b/test/fixtures/data-cson/valid/articles/hello-pluto.jade new file mode 100644 index 000000000..9b7a09f5b --- /dev/null +++ b/test/fixtures/data-cson/valid/articles/hello-pluto.jade @@ -0,0 +1,3 @@ +//- Testing whether you can write escaped markup inside `_data.json` files + +h1!= title diff --git a/test/fixtures/data-cson/valid/articles/hello-world.jade b/test/fixtures/data-cson/valid/articles/hello-world.jade new file mode 100644 index 000000000..8cf7296e9 --- /dev/null +++ b/test/fixtures/data-cson/valid/articles/hello-world.jade @@ -0,0 +1,4 @@ +h3= title +h4= author + +p Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. \ No newline at end of file diff --git a/test/fixtures/data-cson/valid/index.jade b/test/fixtures/data-cson/valid/index.jade new file mode 100644 index 000000000..a0ce712f0 --- /dev/null +++ b/test/fixtures/data-cson/valid/index.jade @@ -0,0 +1,6 @@ +h2 Home + +h3 Articles + +for article, slug in public.articles._data + != partial('articles/' + slug + '.jade', { author: "Kool Keith" }) \ No newline at end of file diff --git a/test/fixtures/data-cson/valid/pub.json.jade b/test/fixtures/data-cson/valid/pub.json.jade new file mode 100644 index 000000000..d6b8764c8 --- /dev/null +++ b/test/fixtures/data-cson/valid/pub.json.jade @@ -0,0 +1 @@ +!= JSON.stringify(public) \ No newline at end of file diff --git a/test/fixtures/data-hjson/invalid/_data.hjson b/test/fixtures/data-hjson/invalid/_data.hjson new file mode 100644 index 000000000..9e56ad7e2 --- /dev/null +++ b/test/fixtures/data-hjson/invalid/_data.hjson @@ -0,0 +1,10 @@ +{ + hello-world { + title: Earth people, New York to California + author: Brock Whitten + } + hello-jupiter: { + title: I was born on Jupiter + author: Brock Whitten + } +} diff --git a/test/fixtures/data-hjson/valid/.foo/placeholder.txt b/test/fixtures/data-hjson/valid/.foo/placeholder.txt new file mode 100644 index 000000000..5c532474c --- /dev/null +++ b/test/fixtures/data-hjson/valid/.foo/placeholder.txt @@ -0,0 +1 @@ +This file is here to test ignoring directories that begin with a dot (".") diff --git a/test/fixtures/data-hjson/valid/_layout.jade b/test/fixtures/data-hjson/valid/_layout.jade new file mode 100644 index 000000000..e46014a31 --- /dev/null +++ b/test/fixtures/data-hjson/valid/_layout.jade @@ -0,0 +1,3 @@ +h1 My Articles +h5.feature= public.articles._data['hello-world'].title +!= yield \ No newline at end of file diff --git a/test/fixtures/data-hjson/valid/articles/_data.hjson b/test/fixtures/data-hjson/valid/articles/_data.hjson new file mode 100644 index 000000000..cd121e5b6 --- /dev/null +++ b/test/fixtures/data-hjson/valid/articles/_data.hjson @@ -0,0 +1,13 @@ +{ + hello-world: { + title: Earth people, New York to California + author: Brock Whitten + } + hello-jupiter: { + title: I was born on Jupiter + author: Brock Whitten + } + hello-pluto: { + title: Harp + } +} diff --git a/test/fixtures/data-hjson/valid/articles/hello-jupiter.jade b/test/fixtures/data-hjson/valid/articles/hello-jupiter.jade new file mode 100644 index 000000000..8cf7296e9 --- /dev/null +++ b/test/fixtures/data-hjson/valid/articles/hello-jupiter.jade @@ -0,0 +1,4 @@ +h3= title +h4= author + +p Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. \ No newline at end of file diff --git a/test/fixtures/data-hjson/valid/articles/hello-pluto.jade b/test/fixtures/data-hjson/valid/articles/hello-pluto.jade new file mode 100644 index 000000000..9b7a09f5b --- /dev/null +++ b/test/fixtures/data-hjson/valid/articles/hello-pluto.jade @@ -0,0 +1,3 @@ +//- Testing whether you can write escaped markup inside `_data.json` files + +h1!= title diff --git a/test/fixtures/data-hjson/valid/articles/hello-world.jade b/test/fixtures/data-hjson/valid/articles/hello-world.jade new file mode 100644 index 000000000..8cf7296e9 --- /dev/null +++ b/test/fixtures/data-hjson/valid/articles/hello-world.jade @@ -0,0 +1,4 @@ +h3= title +h4= author + +p Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. \ No newline at end of file diff --git a/test/fixtures/data-hjson/valid/index.jade b/test/fixtures/data-hjson/valid/index.jade new file mode 100644 index 000000000..a0ce712f0 --- /dev/null +++ b/test/fixtures/data-hjson/valid/index.jade @@ -0,0 +1,6 @@ +h2 Home + +h3 Articles + +for article, slug in public.articles._data + != partial('articles/' + slug + '.jade', { author: "Kool Keith" }) \ No newline at end of file diff --git a/test/fixtures/data-hjson/valid/pub.json.jade b/test/fixtures/data-hjson/valid/pub.json.jade new file mode 100644 index 000000000..d6b8764c8 --- /dev/null +++ b/test/fixtures/data-hjson/valid/pub.json.jade @@ -0,0 +1 @@ +!= JSON.stringify(public) \ No newline at end of file diff --git a/test/helpers.js b/test/helpers.js index 1be6c17c0..d8ebd2086 100644 --- a/test/helpers.js +++ b/test/helpers.js @@ -34,12 +34,15 @@ describe("helpers", function(){ it('should look for templates on json files.', function(done){ var list = polymer.helpers.buildPriorityList('profile.json') list.should.be.an.instanceOf(Array) - list.should.have.lengthOf(4) - list.should.include('profile.hjson') + list.should.have.lengthOf(7) + list.should.include('profile.json.jade') + list.should.include('profile.json.ejs') + list.should.include('profile.json.md') list.should.include('profile.cson') - list.should.include('profile.json.hjson') + list.should.include('profile.hjson') list.should.include('profile.json.cson') - list.should.eql('profile.hjson, profile.cson, profile.json.hjson, profile.json.cson'. split(', ')) + list.should.include('profile.json.hjson') + list.should.eql('profile.json.jade, profile.json.ejs, profile.json.md, profile.cson, profile.hjson, profile.json.cson, profile.json.hjson'. split(', ')) done() }) From d3a2832afac1dbcc46d9adf6de34b9b8d276439c Mon Sep 17 00:00:00 2001 From: Bernhard Waldbrunner Date: Fri, 11 Sep 2015 20:09:27 +0200 Subject: [PATCH 6/6] More precise regular expressions for Browserify detection --- lib/helpers/raw.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/helpers/raw.js b/lib/helpers/raw.js index 1bbaf676e..681384c27 100644 --- a/lib/helpers/raw.js +++ b/lib/helpers/raw.js @@ -551,8 +551,8 @@ exports.isJavaScript = function(filePath){ */ exports.needsBrowserify = function(source) { - return /^[^#\/'"*]*(require|module|exports)\b/m.test(source) - && !(/\b(function|var|global) +(require|module|exports)\b|\b(module|require) *=[^=]/.test(source)) + return /^[^#\/*'"]*\b(require|module|exports)\b/m.test(source) + && !(/^[^#\/*'"\w]*(function|var|global) +(require|module|exports)\b|^([^#\/*'"\w]*|[^#\/*'"]*window\.)(module|require) *=[^=]/m.test(source)) } /**