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

Adds minification to vanilla CSS and HTML #125

Open
wants to merge 4 commits into
base: release-v1.0.0
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
4 changes: 2 additions & 2 deletions lib/helpers/raw.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ var TerraformError = exports.TerraformError = require("../error").TerraformError
*/

var processors = exports.processors = {
"html": ["jade", "ejs", "md"],
"css" : ["styl", "less", "scss", "sass"],
"html": ["html", "jade", "ejs", "md"],
"css" : ["css", "scss", "sass", "less", "styl"],
"js" : ["js", "coffee"]
}

Expand Down
3 changes: 3 additions & 0 deletions lib/stylesheet/processors/css.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
exports.compile = function(filePath, dirs, fileContents, callback){
callback(null, fileContents.toString())
}
21 changes: 13 additions & 8 deletions lib/template/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,14 +146,19 @@ var scope = module.exports = function(projectPath, parentLocals){
*/
var tmpl = processors[ext](fileContents, { filename: filePath, basedir: projectPath })

try{
var render = tmpl.compile()
var output = render(locals)
}catch(e){
throw e.source && e.dest
? e
: tmpl.parseError(e)
}
try {
var render = tmpl.compile()
var output
if (ext !== 'html') {
output = render(locals)
} else {
output = minify.html(tmpl.compile())
}
} catch(e) {
throw e.source && e.dest
? e
: tmpl.parseError(e)
}


/**
Expand Down
26 changes: 26 additions & 0 deletions lib/template/processors/html.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
var TerraformError = require("../../error").TerraformError

module.exports = function(fileContents, options){

return {
compile: function(){
return fileContents.toString()
},

parseError: function(error){
var arr = error.message.split("\n")
var path_arr = arr[0].split(":")

error.lineno = parseInt(error.lineno || path_arr[path_arr.length -1] || -1)
error.message = arr[arr.length - 1]
error.name = error.name
error.source = "HTML"
error.dest = "HTML"
error.filename = error.path || options.filename
error.stack = fileContents.toString()

return new TerraformError(error)
}
}

}
4 changes: 4 additions & 0 deletions test/fixtures/stylesheets/css/main.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
body {
background: #ffc0cb;
font-feature-settings: "kern", "liga", "pnum", "onum";
}
5 changes: 5 additions & 0 deletions test/fixtures/templates/html/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@



<h1 class="h1">Chloi Inc.</h1>
<p>Vancouver, Canada</p>
20 changes: 10 additions & 10 deletions test/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@ describe("helpers", function(){
it('should return all possible file names for html ordered by priority.', function(done){
var list = polymer.helpers.buildPriorityList('index.html')
list.should.be.an.instanceOf(Array)
list.should.have.lengthOf(6)
var plist = "index.jade, index.ejs, index.md, index.html.jade, index.html.ejs, index.html.md".split(', ')
list.should.have.lengthOf(8)
var plist = "index.html, index.jade, index.ejs, index.md, index.html.html, index.html.jade, index.html.ejs, index.html.md".split(', ')
list.should.eql(plist)
done()
})

it('should build priority list for css file.', function(done){
var list = polymer.helpers.buildPriorityList('main.css')
list.should.be.an.instanceOf(Array)
list.should.have.lengthOf(8)
list.should.eql("main.styl, main.less, main.scss, main.sass, main.css.styl, main.css.less, main.css.scss, main.css.sass".split(', '))
list.should.have.lengthOf(10)
list.should.eql("main.css, main.scss, main.sass, main.less, main.styl, main.css.css, main.css.scss, main.css.sass, main.css.less, main.css.styl".split(', '))
done()
})

Expand All @@ -36,27 +36,27 @@ describe("helpers", function(){
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)
list.should.have.lengthOf(3)
list.should.eql('feed.xml.jade, feed.xml.ejs, feed.xml.md'. split(', '))
list.should.have.lengthOf(4)
list.should.eql('feed.xml.html, feed.xml.jade, feed.xml.ejs, feed.xml.md'. split(', '))
done()
})

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.have.lengthOf(4)
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.eql('profile.json.html, profile.json.jade, profile.json.ejs, profile.json.md'. split(', '))
done()
})

it('should look for templates when no ext present.', function(done){
var list = polymer.helpers.buildPriorityList('appcache')
list.should.be.an.instanceOf(Array)
list.should.have.lengthOf(3)
list.should.eql('appcache.jade, appcache.ejs, appcache.md'.split(', '))
list.should.have.lengthOf(4)
list.should.eql('appcache.html, appcache.jade, appcache.ejs, appcache.md'.split(', '))
done()
})

Expand Down
46 changes: 46 additions & 0 deletions test/stylesheets.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,52 @@ var polymer = require('../')

describe("stylesheets", function(){

describe(".css", function(){

var root = __dirname + '/fixtures/stylesheets/css'
var poly = polymer.root(root)

it("should have basic css file", function(done){
poly.render("main.css", function(error, body){
should.not.exist(error)
body.should.include("background:#ffc0cb")
done()
})
})
it("should autoprefix css", function(done){
poly.render("main.css", function(error, body){
should.not.exist(error)
body.should.include("-webkit-font-feature-settings")
done()
})
})

it("should minify beyond preprocessor", function(done){
poly.render("main.css", function(error, body){
should.not.exist(error)
body.should.not.include(";}")
done()
})
})

// it("should render a source map", function(done){
// poly.render("main.css", function(error, body, sourcemap){
// should.not.exist(error)
// should.exist(sourcemap)
// sourcemap.toString().should.include('main.css')
// done()
// })
// })
it("should not include the source map in the css body", function(done){
poly.render("main.css", function(error, body, sourcemap){
should.not.exist(error)
body.should.not.include("/*#")
done()
})
})

})

describe(".less", function(){

var root = __dirname + '/fixtures/stylesheets/less'
Expand Down
22 changes: 22 additions & 0 deletions test/templates.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,28 @@ describe("templates", function(){
var root = __dirname + "/fixtures/templates"
var poly = polymer.root(root)

describe(".html", function(){
it("should render html file", function(done){
poly.render("html/index.html", function(error, body){
should.not.exist(error)
should.exist(body)
body.should.include("<h1")
body.should.include("Chloi Inc.</h1>")
done()
})
})

it("should minify beyond preprocessor", function(done){
poly.render("html/index.html", function(error, body){
should.not.exist(error)
body.should.not.include("\n\n")
body.should.include("<h1 class=\"h1\">")
body.should.not.include("<h1 class=\"h1\">")
done()
})
})
})

describe(".ejs", function(){
it("should render ejs file", function(done){
poly.render("bio.ejs", function(error, body){
Expand Down