Comparing version 0.2.12 to 0.2.13
@@ -7,8 +7,14 @@ /* | ||
var less = require('less'), | ||
fs = require('fs'), | ||
diveSync = require("diveSync"); | ||
var less = require('less'); | ||
var fs = require('fs'); | ||
var diveSync = require("diveSync"); | ||
var log = console.log; | ||
/* make the logger more hackable, e.g. use grunt's instead */ | ||
function setConsole(c){ | ||
log = c.log; | ||
} | ||
/* Generate a CSS file based on a LESS file. Optionally pass an array of paths to use for looking up @imports */ | ||
//FIXME styledirs is getting appended to over and over with duplicates | ||
function createCSS(lessFile, styledirs) { | ||
@@ -18,18 +24,15 @@ styledirs = styledirs || []; | ||
styledirs.push(lessFile.substr(0, lessFile.lastIndexOf('/'))); | ||
try { | ||
var data = fs.readFileSync(lessFile); | ||
var parser = new(less.Parser)({ | ||
// Specify search paths for @import directives. Combine with these common defaults | ||
paths: styledirs.concat(['.', './css']) | ||
}); | ||
parser.parse(data.toString(), function(e, tree) { | ||
var cssFile = lessFile.substr(0, lessFile.lastIndexOf('.')) + '.css'; | ||
fs.writeFileSync(cssFile, tree.toCSS({ | ||
compress: true | ||
})); | ||
console.log('created CSS file: ', cssFile); | ||
}); | ||
} catch (err) { | ||
console.log("ERROR: ", err); | ||
} | ||
var data = fs.readFileSync(lessFile); | ||
var parser = new(less.Parser)({ | ||
// Specify search paths for @import directives. Combine with these common defaults | ||
paths: styledirs.concat(['.', './css']) | ||
}); | ||
parser.parse(data.toString(), function(e, tree) { | ||
if (e) throw new Error ('Error parsing LESS file: ' + lessFile + '\n Cause: ' + e.message + '\n'); | ||
var cssFile = lessFile.substr(0, lessFile.lastIndexOf('.')) + '.css'; | ||
fs.writeFileSync(cssFile, tree.toCSS({ | ||
compress: true | ||
})); | ||
log('created CSS file: ', cssFile); | ||
}); | ||
} | ||
@@ -44,8 +47,8 @@ | ||
data = stripLessJS(data); | ||
fs.writeFileSync(htmlFile, data); | ||
if (data !== origData) { | ||
console.log('adjusted LESS links in file: ', htmlFile); | ||
fs.writeFileSync(htmlFile, data); | ||
log('adjusted LESS links in file: ', htmlFile); | ||
} | ||
} catch (err) { | ||
console.log("ERROR: ", err); | ||
throw new Error("Could not adust links in file: " + htmlFile + "\n Cause: " + err.message + '\n'); | ||
} | ||
@@ -55,3 +58,2 @@ } | ||
/* replace sytlesheet/less with stylesheet */ | ||
function stripLessRel(contents) { | ||
@@ -62,3 +64,2 @@ return contents.replace(/stylesheet\/less/g, "stylesheet"); | ||
/* replace .less with .css */ | ||
function replaceLessExtension(contents) { | ||
@@ -69,3 +70,2 @@ return contents.replace(/\.less\b/g, ".css"); | ||
/* strip less.js from the web page (no longer needed after compiling less to css) */ | ||
function stripLessJS(contents) { | ||
@@ -86,3 +86,3 @@ return contents.replace(/<script.*?\bless\b[^"']*?\.js.*?<\/script>/g, ""); | ||
styledirs = ensureArray(styledirs); | ||
console.log('optimizing LESS in: ', directory); | ||
log('optimizing LESS in: ', directory); | ||
diveSync(directory, | ||
@@ -99,3 +99,4 @@ //ignore node_modules | ||
createCSS(file, styledirs); | ||
} else if (stripExtensions.indexOf(extension) !== -1) { | ||
} | ||
else if (stripExtensions.indexOf(extension) !== -1) { | ||
adjustLinks(file); | ||
@@ -116,3 +117,4 @@ } | ||
stripLessJS: stripLessJS, | ||
optimizeProject: optimizeProject | ||
optimizeProject: optimizeProject, | ||
setConsole: setConsole | ||
}; |
@@ -10,3 +10,3 @@ { | ||
], | ||
"version": "0.2.12", | ||
"version": "0.2.13", | ||
"repository": { | ||
@@ -13,0 +13,0 @@ "type": "git", |
@@ -1,9 +0,9 @@ | ||
var buster = require('buster') | ||
, lessless = require('../lib/lessless') | ||
, fs = require('fs'); | ||
var buster = require('buster'); | ||
var lessless = require('../lib/lessless'); | ||
var fs = require('fs'); | ||
buster.spec.expose(); | ||
describe('generating CSS from a LESS file', function(){ | ||
before(function () { | ||
describe('generating CSS from a LESS file', function() { | ||
before(function() { | ||
//generate a css file from a less file | ||
@@ -14,3 +14,3 @@ var testfile = "test/test.less"; | ||
after(function () { | ||
after(function() { | ||
//delete the generated css file | ||
@@ -20,60 +20,61 @@ fs.unlinkSync("test/test.css"); | ||
it('should create file', function(){ | ||
var file = fs.readFileSync("test/test.css"); | ||
console.log("file: ", file); | ||
it('should create file', function() { | ||
var file = fs.readFileSync("test/test.css"); | ||
expect(file).toBeDefined(); | ||
}); | ||
}); | ||
}); | ||
describe('specifying paths to CSS folders for less to lookup', function(){ | ||
before(function () { | ||
var testfile = 'test/testpaths.less'; | ||
lessless.createCSS(testfile, ['test/mystyles']); | ||
}); | ||
// describe('//specifying paths to CSS folders for less to lookup', function() { | ||
// before(function() { | ||
// var testfile = 'test/testpaths.less'; | ||
// lessless.createCSS(testfile, ['test/mystyles']); | ||
// }); | ||
after(function () { | ||
fs.unlinkSync('test/testpaths.css'); | ||
}); | ||
// after(function() { | ||
// fs.unlinkSync('test/testpaths.css'); | ||
// }); | ||
it('should create CSS file, correctly finding the imported less file', function () { | ||
var file = fs.readFileSync('test/testpaths.css'); | ||
expect(file).toBeDefined(); | ||
}); | ||
}); | ||
// it('should create CSS file, correctly finding the imported less file', function() { | ||
// var file = fs.readFileSync('test/testpaths.css'); | ||
// expect(file).toBeDefined(); | ||
// expect(true).toEqual(true); | ||
// }); | ||
// }); | ||
describe('adjusting LESS links', function(){ | ||
describe('adjusting LESS links', function() { | ||
var origHTML, testFile = "test/test.html"; | ||
describe('replacing stylesheet/less with stylesheet', function(){ | ||
it('should start with 1', function(){ | ||
var html = '<link rel="stylesheet/less" type="text/css" href="stylesheet1.less" media="all">'; | ||
expect(html).toMatch(/stylesheet\/less/g); | ||
//make the change | ||
var newHTML = lessless.stripLessRel(html); | ||
expect(newHTML).not().toMatch(/stylesheet\/less/g); | ||
expect(newHTML).toMatch(/\"stylesheet\"/g); | ||
}); | ||
}); | ||
describe('replacing stylesheet/less with stylesheet', function() { | ||
it('should start with 1', function() { | ||
var html = '<link rel="stylesheet/less" type="text/css" href="stylesheet1.less" media="all">'; | ||
expect(html).toMatch(/stylesheet\/less/g); | ||
//make the change | ||
var newHTML = lessless.stripLessRel(html); | ||
// expect(newHTML).not.toMatch(/stylesheet\/less/g); | ||
expect(newHTML).not.toMatch(/stylesheet\/less/g); | ||
expect(newHTML).toMatch(/\"stylesheet\"/g); | ||
}); | ||
}); | ||
describe('replacling .less with .css', function(){ | ||
it('should replace .less with .css', function(){ | ||
describe('replacling .less with .css', function() { | ||
it('should replace .less with .css', function() { | ||
var html = '<link rel="stylesheet/less" type="text/css" href="stylesheet1.less" media="all">'; | ||
expect(html).toMatch(/\.less/g); | ||
expect(html).not().toMatch(/\.css/g); | ||
expect(html).not.toMatch(/\.css/g); | ||
//make the change | ||
var newHTML = lessless.replaceLessExtension(html); | ||
expect(newHTML).not().toMatch(/\.less/g); | ||
expect(newHTML).not.toMatch(/\.less/g); | ||
expect(newHTML).toMatch(/\.css/g); | ||
}); | ||
it('should not match words containing .less, like .lesson', function () { | ||
it('should not match words containing .less, like .lesson', function() { | ||
var html = "org.lds.lua.lesson.service.impl.NavigationServiceImpl"; | ||
var newHTML = lessless.replaceLessExtension(html); | ||
expect(newHTML).toMatch(/\.lesson/g); | ||
expect(newHTML).not().toMatch(/\.less\b/g); | ||
expect(newHTML).toMatch(/\.lesson/g); | ||
expect(newHTML).not.toMatch(/\.less\b/g); | ||
}); | ||
}); | ||
describe('stripping out less.js script', function(){ | ||
it('should remove the script tag', function(){ | ||
describe('stripping out less.js script', function() { | ||
it('should remove the script tag', function() { | ||
var html = "<script src='js/lib/less.js'></script>"; | ||
@@ -83,4 +84,4 @@ expect(html).toMatch(/js\/lib\/less.js/); | ||
var newHTML = lessless.stripLessJS(html); | ||
expect(newHTML).not().toMatch(/js\/lib\/less.js/); | ||
expect(newHTML).not().toMatch(/script/); | ||
expect(newHTML).not.toMatch(/js\/lib\/less.js/); | ||
expect(newHTML).not.toMatch(/script/); | ||
}); | ||
@@ -92,4 +93,4 @@ it('should remove versioned and compressed "less" script tags', function() { | ||
var newHTML = lessless.stripLessJS(html); | ||
expect(newHTML).not().toMatch(/js\/lib\/less-1\.2\.1\.min.js/); | ||
expect(newHTML).not().toMatch(/script/); | ||
expect(newHTML).not.toMatch(/js\/lib\/less-1\.2\.1\.min.js/); | ||
expect(newHTML).not.toMatch(/script/); | ||
}); | ||
@@ -96,0 +97,0 @@ it('should not match scripts where "less" is not a standalone word', function() { |
54401
194
26