Skip to content

Commit

Permalink
Merge pull request #122 from sintaxi/release-v1.0.0
Browse files Browse the repository at this point in the history
Release v1.0.0
  • Loading branch information
sintaxi committed Jan 14, 2016
2 parents c863f6a + dd4279d commit af623dc
Show file tree
Hide file tree
Showing 28 changed files with 306 additions and 52 deletions.
2 changes: 1 addition & 1 deletion lib/helpers/memoized.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@ exports.walkData = helpers.walkData
exports.isTemplate = helpers.isTemplate
exports.isStylesheet = helpers.isStylesheet
exports.isJavaScript = helpers.isJavaScript

exports.needsBrowserify = helpers.needsBrowserify
27 changes: 19 additions & 8 deletions lib/helpers/raw.js
Original file line number Diff line number Diff line change
Expand Up @@ -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" : ["js", "coffee"]
}


Expand Down Expand Up @@ -219,18 +219,18 @@ var dataTree = exports.dataTree = function (filename) {
obj._contents = []

try{
var dataPath = path.resolve(dirPath, "_data.json")
var fileData = fs.readFileSync(dataPath)
obj._data = JSON.parse(fileData)
var dataPath = path.resolve(dirPath, "_data")
obj._data = require(dataPath)
delete require.cache[require.resolve(dataPath)]
}catch(e){
if(e.code && e.code === "ENOENT"){
if(e.code && e.code === "MODULE_NOT_FOUND"){
// data file failed or does not exist
}else{
e.source = "Data"
e.dest = "Globals"
e.lineno = -1
e.filename = dataPath
e.stack = fileData.toString()
e.filename = require.resolve(dataPath)
e.stack = fs.readFileSync(e.filename)
throw new TerraformError(e)
}
//console.log(e.code)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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))
}
79 changes: 63 additions & 16 deletions lib/javascript/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
var path = require("path")
var fs = require("fs")
var helpers = require('../helpers')
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')

/**
* Build Processor list for javascripts.
Expand All @@ -13,15 +15,20 @@ var minify = require('harp-minify')
* }
*
*/
var processors = {}
var extensions = [], processors = {}
helpers.processors["js"].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(/^\./, '')
var minifyOpts = {
compress: false,
mangle: false
}

fs.readFile(srcPath, function(err, data){

Expand All @@ -41,18 +48,58 @@ 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
*/
cb(null, minify.js(js, minifyOpts))
})
}

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(srcPath, {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, minify.js(post, minifyOpts))
}
})
}
else {
render(ext, data, callback)
}

})

Expand Down
3 changes: 3 additions & 0 deletions lib/javascript/processors/js.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
exports.compile = function(filePath, fileContents, callback){
callback(null, fileContents.toString())
}
2 changes: 1 addition & 1 deletion lib/template/processors/jade.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var jade = require('harp-jade')
var jade = require('jade')
var TerraformError = require("../../error").TerraformError

module.exports = function(fileContents, options){
Expand Down
57 changes: 35 additions & 22 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,37 +12,50 @@
},
"author": "Brock Whitten <brock@chloi.io>",
"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" }
"Brock Whitten <brock@chloi.io>",
"Brian Donovan <donovan@squareup.com>",
"Kenneth Ormandy <kenneth@chloi.io>",
"Zhang Yichao <echaozh@gmail.com>",
"Carlos Rodriguez",
"Zeke Sikelianos <zeke@sikelianos.com>",
"Guilherme Rodrigues <gadr90@gmail.com>",
"Radu Brehar <radu@jslog.com>",
"Glen Maddern <glenmaddern@gmail.com>",
"Jed Foster <jed@jedfoster.com>",
"Sehrope Sarkuni <sehrope@jackdb.com>",
"Keiichiro Matsumoto <matsumos@gmail.com>",
"Najam Khn <najamkhn@gmail.com>",
"Eric Drechsel",
"Najam Khan",
"Zhang Yichao <echaozh@gmail.com>",
"Dave Jensen",
"Ryan Lewis",
"Julian Duque <julianduquej@gmail.com>",
"Lu Nelson <lunelson@gmail.com>",
"Stephen Way",
"Pierre Spring <pierre@nelm.io>",
"John Boxall <john@mobify.com>",
"TJ Nicolaides <tjnicolaides@gmail.com>"
],
"license": "MIT",
"dependencies": {
"lru-cache": "2.7.0",
"harp-jade": "1.9.3-bc.4",
"autoprefixer": "6.2.3",
"browserify": "12.0.1",
"coffee-script": "1.10.0",
"node-sass": "3.4.2",
"ejs": "2.3.4",
"marked": "0.3.5",
"lodash": "3.10.1",
"harp-minify": "0.4.0",
"jade": "1.11.0",
"less": "2.5.3",
"lodash": "3.10.1",
"lru-cache": "4.0.0",
"marked": "0.3.5",
"node-sass": "3.4.2",
"postcss": "5.0.14",
"stylus": "0.53.0",
"harp-minify": "0.3.3",
"autoprefixer": "6.1.0",
"postcss": "5.0.12"
"through": "2.3.8"
},
"devDependencies": {
"mocha": "1.8.2",
"mocha": "2.3.4",
"should": "1.2.2"
}
}
12 changes: 12 additions & 0 deletions test/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,16 @@ describe("data", function(){
})
})

describe("dynamic", function(){
it("should return public object", function(done){
var root = __dirname + "/fixtures/data/dynamic"
var poly = polymer.root(root)
poly.render("pub.json.jade", { "layout": false }, function(err, result){
var pub = JSON.parse(result)
should.exist(pub["articles"]["_data"]["hello-world"])
should.not.exist(pub[".foo"])
done()
})
})
})
})
1 change: 1 addition & 0 deletions test/fixtures/data/dynamic/.foo/placeholder.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This file is here to test ignoring directories that begin with a dot (".")
3 changes: 3 additions & 0 deletions test/fixtures/data/dynamic/_layout.jade
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
h1 My Articles
h5.feature= public.articles._data['hello-world'].title
!= yield
6 changes: 6 additions & 0 deletions test/fixtures/data/dynamic/articles/_data.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
"hello-world": {
"title" : "Earth people, New York to California",
"author": "Brock Whitten"
}
}
4 changes: 4 additions & 0 deletions test/fixtures/data/dynamic/articles/hello-world.jade
Original file line number Diff line number Diff line change
@@ -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.
6 changes: 6 additions & 0 deletions test/fixtures/data/dynamic/index.jade
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
h2 Home

h3 Articles

for article, slug in public.articles._data
!= partial('articles/' + slug + '.jade', { author: "Kool Keith" })
1 change: 1 addition & 0 deletions test/fixtures/data/dynamic/pub.json.jade
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
!= JSON.stringify(public)
4 changes: 4 additions & 0 deletions test/fixtures/javascripts/browserify/Math.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Let's see if we can mix .coffee & .es

exports.pow = (num) ->
num * num
5 changes: 5 additions & 0 deletions test/fixtures/javascripts/browserify/Math.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Let's see if we can mix .es & .js

exports.pow = function(num) {
return num * num;
};
3 changes: 3 additions & 0 deletions test/fixtures/javascripts/browserify/comment.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# pow = require('./Math').pow;

console.log(pow(4));
5 changes: 5 additions & 0 deletions test/fixtures/javascripts/browserify/comment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/*
* pow = require('./Math').pow;
*/

console.log(pow(4));
6 changes: 6 additions & 0 deletions test/fixtures/javascripts/browserify/declared.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
require = (file) ->
# custom implementation

pow = require('./Math').pow

console.log pow(4)
7 changes: 7 additions & 0 deletions test/fixtures/javascripts/browserify/declared.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
var require = function(file) {
// custom implementation
};

var pow = require('./Math').pow;

console.log(pow(4));
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pow = require('./../../../../Math.js').pow

console.log pow(4)
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
var pow = require('./../../../../Math.js').pow;

console.log(pow(4));
3 changes: 3 additions & 0 deletions test/fixtures/javascripts/browserify/require_coffee.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pow = require('./Math.coffee').pow

console.log pow(4)
3 changes: 3 additions & 0 deletions test/fixtures/javascripts/browserify/require_coffee.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
var pow = require('./Math.coffee').pow;

console.log(pow(4));
3 changes: 3 additions & 0 deletions test/fixtures/javascripts/browserify/require_js.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pow = require('./Math.js').pow

console.log pow(4)
3 changes: 3 additions & 0 deletions test/fixtures/javascripts/browserify/require_js.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
var pow = require('./Math.js').pow;

console.log(pow(4));
22 changes: 22 additions & 0 deletions test/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@ describe("helpers", function(){
done()
})

it('should return all possible file names for js ordered by priority.', function(done){
var list = polymer.helpers.buildPriorityList('/js/bundle.js')
list.should.be.an.instanceOf(Array)
list.should.have.lengthOf(4)
var plist = "js/bundle.js, js/bundle.coffee, js/bundle.js.js, js/bundle.js.coffee".split(', ')
list.should.eql(plist)
done()
})


it('should build priority list assuming template file when unknown.', function(done){
var list = polymer.helpers.buildPriorityList('feed.xml')
list.should.be.an.instanceOf(Array)
Expand Down Expand Up @@ -257,6 +267,18 @@ describe("helpers", function(){
done()
})

it('should return true if javascript file.', function(done){
polymer.helpers.isJavaScript(path.join('foo.js')).should.be.true
polymer.helpers.isJavaScript(path.join('foo', 'bar', 'baz.js')).should.be.true
done()
})

it('should return true if minified javascript file.', function(done){
polymer.helpers.isJavaScript(path.join('foo.min.js')).should.be.true
polymer.helpers.isJavaScript(path.join('foo', 'bar', 'bas.min.js')).should.be.true
done()
})

it('should return false if less file.', function(done){
polymer.helpers.isStylesheet(path.join('foo.less')).should.be.true
polymer.helpers.isStylesheet(path.join('foo', 'bar', 'baz.less')).should.be.true
Expand Down
Loading

0 comments on commit af623dc

Please sign in to comment.