Skip to content
This repository has been archived by the owner on Mar 9, 2022. It is now read-only.

Commit

Permalink
Adds support for Browserify
Browse files Browse the repository at this point in the history
  • Loading branch information
vbwx committed Jun 22, 2015
1 parent 1910206 commit b426d88
Show file tree
Hide file tree
Showing 16 changed files with 246 additions and 38 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
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 /^[^#\/'"*]*(require|module|exports)\b/m.test(source)
&& !(/\b(function|var|global) +(require|module|exports)\b|\b(module|require) *=[^=]/.test(source))
}
82 changes: 65 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,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){

Expand All @@ -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) {

This comment has been minimized.

Copy link
@kennethormandy

kennethormandy Jun 23, 2015

filePath should be srcPath here, that fixes the issue I described in sintaxi#97 (comment)

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

})

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()
})
})
})

})

0 comments on commit b426d88

Please sign in to comment.