Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for Browserify #97

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
13 changes: 12 additions & 1 deletion 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" : ["coffee", "es"]
}


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 /^[^#\/*'"]*\b(require|module|exports)\b/m.test(source)
&& !(/^[^#\/*'"\w]*(function|var|global) +(require|module|exports)\b|^([^#\/*'"\w]*|[^#\/*'"]*window\.)(module|require) *=[^=]/m.test(source))
}
88 changes: 71 additions & 17 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 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.
Expand All @@ -13,10 +15,13 @@ var minify = require('minify')
* }
*
*/
var processors = {}
var extensions = [], processors = {}, exceptionHandler = null

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){

Expand All @@ -41,18 +46,67 @@ 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

if (exceptionHandler) {
process.removeListener('uncaughtException', exceptionHandler)
}
exceptionHandler = function(err) {
console.log(err.message)
if (success) {
success = false
render(ext, data, callback)
}
}

process.on('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) {
callback(null, post)
}
})
}
else {
render(ext, data, callback)
}

})

Expand Down
3 changes: 3 additions & 0 deletions lib/javascript/processors/es.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())
}
78 changes: 59 additions & 19 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,32 +12,72 @@
},
"author": "Brock Whitten <[email protected]>",
"contributors": [
{ "name": "Brock Whitten", "email": "[email protected]" },
{ "name": "Brian Donovan", "email": "[email protected]" },
{ "name": "Kenneth Ormandy", "email": "[email protected]" },
{ "name": "Zhang Yichao", "email": "[email protected]" },
{ "name": "Carlos Rodriguez" },
{ "name": "Zeke Sikelianos", "email": "[email protected]" },
{ "name": "Guilherme Rodrigues", "email": "[email protected]" },
{ "name": "Radu Brehar", "email": "[email protected]" },
{ "name": "Glen Maddern", "email": "[email protected]" },
{ "name": "Jed Foster", "email": "[email protected]" },
{ "name": "Sehrope Sarkuni", "email": "[email protected]" },
{ "name": "Keiichiro Matsumoto", "email": "[email protected]" },
{ "name": "Najam Khn", "email": "[email protected]" }
{
"name": "Brock Whitten",
"email": "[email protected]"
},
{
"name": "Brian Donovan",
"email": "[email protected]"
},
{
"name": "Kenneth Ormandy",
"email": "[email protected]"
},
{
"name": "Zhang Yichao",
"email": "[email protected]"
},
{
"name": "Carlos Rodriguez"
},
{
"name": "Zeke Sikelianos",
"email": "[email protected]"
},
{
"name": "Guilherme Rodrigues",
"email": "[email protected]"
},
{
"name": "Radu Brehar",
"email": "[email protected]"
},
{
"name": "Glen Maddern",
"email": "[email protected]"
},
{
"name": "Jed Foster",
"email": "[email protected]"
},
{
"name": "Sehrope Sarkuni",
"email": "[email protected]"
},
{
"name": "Keiichiro Matsumoto",
"email": "[email protected]"
},
{
"name": "Najam Khn",
"email": "[email protected]"
}
],
"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",
Expand Down
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.es
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.es
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));
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.es
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.es
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
var pow = require('./Math.js').pow;

console.log(pow(4));
64 changes: 64 additions & 0 deletions test/javascripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
})
})
})

})