Comparing version 0.1.5 to 0.1.6
152
lib/index.js
@@ -8,9 +8,33 @@ var fs = require('fs'), | ||
var CssCombo = function(){ | ||
function CssCombo(cfg, success){ | ||
var self = this; | ||
var debug = false, | ||
config = {}, | ||
imports = []; | ||
self.imports = []; | ||
function isExcluded(filename){ | ||
utils.debug = cfg.debug; | ||
if(!cfg.target) { | ||
utils.log('please enter css path\r\n', 'error'); | ||
return false; | ||
} | ||
if(!cfg.outputEncoding || cfg.outputEncoding == 'gbk' || cfg.outputEncoding == 'GBK' || cfg.outputEncoding == 'gb2312') { | ||
cfg.outputEncoding = ''; | ||
} | ||
if(typeof cfg.exclude == 'undefined'){ | ||
cfg.exclude = [/.combo.css/, /-min.css/, /.combine.css/]; | ||
} | ||
cfg.compress = cfg.compress == 0 ? 0 : 1; | ||
cfg.output = path.resolve(path.normalize(cfg.output)); | ||
self.config = cfg; | ||
self.build(success); | ||
return true; | ||
} | ||
CssCombo.prototype = { | ||
isExcluded: function(filename){ | ||
var config = this.config; | ||
for(var i in config.exclude){ | ||
@@ -22,12 +46,12 @@ if(config.exclude[i].test(filename)){ | ||
return false; | ||
} | ||
function isRemoteFile(filepath){ | ||
}, | ||
isRemoteFile: function(filepath){ | ||
return /http(s?):\/\//.test(filepath); | ||
} | ||
function getFileContent(file, callback){ | ||
var content = ''; | ||
if(!isRemoteFile(file)){ | ||
if(!isExcluded(file)){ | ||
}, | ||
getFileContent: function(file, callback){ | ||
var self = this, | ||
config = self.config, | ||
content = ''; | ||
if(!self.isRemoteFile(file)){ | ||
if(!self.isExcluded(file)){ | ||
var filePath = path.resolve(config.base, file); | ||
@@ -38,10 +62,10 @@ if(path.existsSync(filePath)){ | ||
}else{ | ||
console.log('WARNING: cannot find file ' + filePath); | ||
utils.log('cannot find file ' + filePath, 'warning'); | ||
} | ||
}else{ | ||
debug && console.log('file excluded: ' + file); | ||
utils.log('file excluded: ' + file, 'debug'); | ||
} | ||
callback && callback(content); | ||
}else{ | ||
debug && console.log('Try to get remote file: ' + file); | ||
utils.log('Try to get remote file: ' + file, 'debug'); | ||
utils.getRemoteFile(file, function(data){ | ||
@@ -52,7 +76,7 @@ content = iconv.decode(data, utils.detectCharset(data)); | ||
} | ||
} | ||
// TODO deal with charset | ||
function generateOutput(fileContent){ | ||
var commentTpl = [ | ||
}, | ||
generateOutput: function(fileContent){ | ||
var self = this, | ||
config = self.config, | ||
commentTpl = [ | ||
'/*\r\n', | ||
@@ -63,4 +87,4 @@ 'combined files : \r\n', | ||
for (var i in imports) { | ||
commentTpl.push(imports[i] + '\r\n'); | ||
for (var i in self.imports) { | ||
commentTpl.push(self.imports[i] + '\r\n'); | ||
} | ||
@@ -74,3 +98,3 @@ | ||
if(config.compress){ | ||
debug && console.log('start compress file.'); | ||
utils.log('start compress file.', 'debug'); | ||
fileContent = compressor.cssmin(fileContent); | ||
@@ -84,3 +108,3 @@ // native2ascii | ||
var comboFile = path.join(config.output, path.basename(config.target).replace(/(.source)?.css/, cssFileExt)); | ||
debug && console.log('start generate combo file: ' + comboFile); | ||
utils.log('start generate combo file: ' + comboFile, 'debug'); | ||
@@ -94,5 +118,7 @@ utils.mkdirSync(path.dirname(comboFile)); | ||
fs.closeSync(fd); | ||
} | ||
function analyzeImports(content, callback){ | ||
utils.log('Successfully generated combo file: ' + comboFile, 'info'); | ||
}, | ||
analyzeImports: function(content, callback){ | ||
var self = this; | ||
if(content){ | ||
@@ -104,9 +130,9 @@ var reg = /@import\s*(url)?\(?['|"]([^'"]+)\.css['|"]\)?[^;]*;/ig, | ||
var filePath = result[2] + '.css'; | ||
imports.push(filePath); | ||
getFileContent(filePath, function(data){ | ||
self.imports.push(filePath); | ||
self.getFileContent(filePath, function(data){ | ||
if(content){ | ||
content = content.replace(result[0], '\n' + data + '\n'); | ||
content = analyzeImports(content, callback); | ||
content = self.analyzeImports(content, callback); | ||
}else{ | ||
debug && console.log('no content'); | ||
utils.log('no content', 'debug'); | ||
} | ||
@@ -118,9 +144,11 @@ }); | ||
}else{ | ||
debug && console.log('content empty.'); | ||
utils.log('content empty.', 'debug'); | ||
} | ||
} | ||
}, | ||
build: function(callback){ | ||
var self = this, | ||
config = self.config, | ||
file = config.target; | ||
function buildFile(){ | ||
var file = config.target; | ||
utils.log('start analyze file : ' + file, 'info'); | ||
utils.log('start analyze file : ' + file, 'debug'); | ||
@@ -130,3 +158,3 @@ config.base = path.dirname(file); | ||
if(err){ | ||
debug && console.log(err); | ||
utils.log(err, 'error'); | ||
} | ||
@@ -136,4 +164,3 @@ | ||
var fileContent = iconv.decode(data, config.inputEncoding); | ||
utils.log('file charset is: ' + config.inputEncoding, 'info'); | ||
utils.log(fileContent); | ||
utils.log('file charset is: ' + config.inputEncoding, 'debug'); | ||
@@ -144,7 +171,6 @@ // preserve data url and comment. | ||
fileContent = compressor._extractComments(fileContent, preservedTokens); | ||
debug && console.log(preservedTokens); | ||
// start analyze file content | ||
analyzeImports(fileContent, function(data){ | ||
debug && console.log('analyze done.'); | ||
self.analyzeImports(fileContent, function(data){ | ||
utils.log('analyze done.', 'debug'); | ||
// after combo, @charset position may be changed. since the output file encoding is specified, we should remove @charset. | ||
@@ -154,35 +180,17 @@ data = data.replace(/@charset\s+['|"](\w*)["|'];/g, ''); | ||
data = compressor._restoreComments(data, preservedTokens); | ||
generateOutput(data); | ||
self.generateOutput(data); | ||
callback && callback(); | ||
self.clean(); | ||
}); | ||
}); | ||
}, | ||
clean: function(){ | ||
delete this; | ||
} | ||
}; | ||
return { | ||
build: function(cfg){ | ||
utils.debug = debug = cfg.debug; | ||
if(!cfg.target) { | ||
debug && console.log('please enter an complier path\r\n'); | ||
return false; | ||
} | ||
if(!cfg.outputEncoding || cfg.outputEncoding == 'gbk' || cfg.outputEncoding == 'GBK' || cfg.outputEncoding == 'gb2312') { | ||
cfg.outputEncoding = ''; | ||
} | ||
if(typeof cfg.exclude == 'undefined'){ | ||
cfg.exclude = [/.combo.css/, /-min.css/, /.combine.css/]; | ||
} | ||
cfg.compress = cfg.compress == 0 ? 0 : 1; | ||
cfg.output = path.resolve(path.normalize(cfg.output)); | ||
config = cfg; | ||
buildFile(); | ||
return true; | ||
} | ||
module.exports = { | ||
build: function(cfg, success){ | ||
new CssCombo(cfg, success); | ||
} | ||
}(); | ||
module.exports = CssCombo; | ||
}; |
@@ -10,3 +10,5 @@ var jschardet = require('jschardet'), | ||
log: function(msg, type){ | ||
if(msg){ | ||
var self = this; | ||
type = type ? type : 'info'; | ||
if(msg && (self.debug || (!self.debug && type != 'debug'))){ | ||
console.log((type ? '[' + type.toUpperCase() + '] ' : '') + msg); | ||
@@ -13,0 +15,0 @@ } |
{ | ||
"name":"css-combo", | ||
"version":"0.1.5", | ||
"version":"0.1.6", | ||
"description":"css combo tool", | ||
@@ -5,0 +5,0 @@ "author":"Dorange <daxingplay@gmail.com>", |
@@ -5,3 +5,3 @@ var CssCombo = require('../lib/index'); | ||
target:'css/test.source.css', | ||
debug: true, | ||
debug: false, | ||
inputEncoding: 'gbk', | ||
@@ -8,0 +8,0 @@ outputEncoding: 'gbk', |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
32377
696