Comparing version
@@ -451,3 +451,3 @@ /** | ||
for (i = 0, max = preservedTokens.length; i < max; i = i + 1) { | ||
css = css.replace("___YUICSSMIN_PRESERVED_TOKEN_" + i + "___", preservedTokens[i]); | ||
css = css.replace("___YUICSSMIN_PRESERVE_CANDIDATE_COMMENT_" + i + "___", preservedTokens[i]); | ||
} | ||
@@ -454,0 +454,0 @@ return css; |
118
lib/index.js
var fs = require('fs'), | ||
path = require('path'), | ||
http = require('http'), | ||
url = require('url'), | ||
compressor = require('./cssmin').compressor; | ||
iconv = require('iconv-lite'), | ||
compressor = require('./cssmin').compressor, | ||
native2ascii = require('native2ascii').native2ascii, | ||
utils = require('./utils'); | ||
@@ -10,3 +11,2 @@ var CssCombo = function(){ | ||
var debug = false, | ||
exclude = [/.combo.css/, /-min.css/, /.combine.css/], | ||
config = {}, | ||
@@ -16,4 +16,4 @@ imports = []; | ||
function isExcluded(filename){ | ||
for(var i in exclude){ | ||
if(exclude[i].test(filename)){ | ||
for(var i in config.exclude){ | ||
if(config.exclude[i].test(filename)){ | ||
return true; | ||
@@ -25,10 +25,2 @@ } | ||
function mkdirSync(dirpath, mode) { | ||
if(!path.existsSync(dirpath)) { | ||
// try to create parent dir first. | ||
mkdirSync(path.dirname(dirpath), mode); | ||
fs.mkdirSync(dirpath, mode); | ||
} | ||
} | ||
function isRemoteFile(filepath){ | ||
@@ -38,29 +30,3 @@ return /http(s?):\/\//.test(filepath); | ||
function getRemoteFile(filePath, callback){ | ||
var content = '', | ||
options = url.parse(filePath); | ||
if(typeof options != 'undefined'){ | ||
debug && console.log('start request'); | ||
var req = http.request(options, function(res){ | ||
debug && console.log('status: ' + res.statusCode); | ||
res.on('data', function(chunk){ | ||
debug && console.log('data got'); | ||
content += chunk; | ||
}); | ||
res.on('end', function(){ | ||
callback && callback(content); | ||
}); | ||
}); | ||
req.on('error', function(e){ | ||
debug && console.log('request error: ' + e); | ||
}); | ||
req.end(); | ||
}else{ | ||
debug && console.log('parse error: ' + filePath); | ||
callback && callback(content); | ||
} | ||
} | ||
function getFileContent(file, callback){ | ||
// TODO https ? | ||
var content = ''; | ||
@@ -70,3 +36,8 @@ if(!isRemoteFile(file)){ | ||
var filePath = path.resolve(config.base, file); | ||
content = fs.readFileSync(filePath, config.inputEncoding).toString(); | ||
if(path.existsSync(filePath)){ | ||
var buf = fs.readFileSync(filePath); | ||
content = iconv.decode(buf, config.inputEncoding ? config.inputEncoding : utils.detectCharset(buf)); | ||
}else{ | ||
console.log('WARNING: cannot find file ' + filePath); | ||
} | ||
}else{ | ||
@@ -77,7 +48,6 @@ debug && console.log('file excluded: ' + file); | ||
}else{ | ||
// TODO get remote file content. | ||
debug && console.log('This is a remote file: ' + file); | ||
getRemoteFile(file, function(data){ | ||
content = data; | ||
callback && callback(data); | ||
debug && console.log('Try to get remote file: ' + file); | ||
utils.getRemoteFile(file, function(data){ | ||
content = iconv.decode(data, utils.detectCharset(data)); | ||
callback && callback(content); | ||
}); | ||
@@ -107,2 +77,5 @@ } | ||
fileContent = compressor.cssmin(fileContent); | ||
// native2ascii | ||
fileContent = native2ascii(fileContent); | ||
config.outputEncoding = 'ascii'; | ||
cssFileExt = '.css'; | ||
@@ -114,8 +87,7 @@ } | ||
// fs.mkdirSync(path.dirname(comboFile)); | ||
mkdirSync(path.dirname(comboFile)); | ||
utils.mkdirSync(path.dirname(comboFile)); | ||
var fd = fs.openSync(comboFile, 'w'); | ||
//write file | ||
// write file | ||
fs.writeSync(fd, fileContent, 0, config.outputEncoding); | ||
@@ -128,3 +100,2 @@ fs.closeSync(fd); | ||
var reg = /@import\s*(url)?\(?['|"]([^'"]+)\.css['|"]\)?[^;]*;/ig, | ||
// allReg = /@import\s*(url)?\(?['|"]([^'"]+)\.(c|le)ss['|"]\)?[^;]*;/ig, | ||
result; | ||
@@ -153,6 +124,6 @@ result = reg.exec(content); | ||
var file = config.target; | ||
debug && console.log('start analyze file : ' + file); | ||
utils.log('start analyze file : ' + file, 'info'); | ||
config.base = path.dirname(file); | ||
fs.readFile(file, config.inputEncoding, function(err, data){ | ||
fs.readFile(file, '', function(err, data){ | ||
if(err){ | ||
@@ -162,3 +133,6 @@ debug && console.log(err); | ||
var fileContent = data.toString(); | ||
config.inputEncoding = config.inputEncoding ? config.inputEncoding : utils.detectCharset(data); | ||
var fileContent = iconv.decode(data, config.inputEncoding); | ||
utils.log('file charset is: ' + config.inputEncoding, 'info'); | ||
utils.log(fileContent); | ||
@@ -169,3 +143,2 @@ // preserve data url and comment. | ||
fileContent = compressor._extractComments(fileContent, preservedTokens); | ||
// console.log(fileContent); | ||
debug && console.log(preservedTokens); | ||
@@ -176,27 +149,8 @@ | ||
debug && console.log('analyze done.'); | ||
// after combo, @charset position may be changed. since the output file encoding is specified, we should remove @charset. | ||
data = data.replace(/@charset\s+['|"](\w*)["|'];/g, ''); | ||
// restore all comments back. | ||
data = compressor._restoreComments(data, preservedTokens); | ||
generateOutput(data); | ||
}); | ||
// var fileContent = data.toString(), | ||
// imports = [], | ||
//// lessImports = [], | ||
// result; | ||
// while((result = /@import\s*(url)?\(?['|"]([^'"]+)\.(c|le)ss['|"]\)?[^;]*;/ig.exec(fileContent)) != null){ | ||
// console.log(result); | ||
// if(result[3] == 'c'){ | ||
// // TODO resolve path. | ||
// var filePath = result[2] + '.css', | ||
// importContent = getFileContent(filePath, cfg); | ||
// imports.push(filePath); | ||
// fileContent = fileContent.replace(result[0], '\n' + importContent + '\n'); | ||
// }else if(result[3] == 'le'){ | ||
//// lessImports.push(result[2] + '.less'); | ||
// // leave less files behind. I will deal with it later. | ||
// }else{ | ||
// debug && console.log('import file syntax error.'); | ||
// } | ||
// } | ||
//// console.log(fileContent); | ||
// generateFile(fileContent, cfg); | ||
}); | ||
@@ -208,3 +162,3 @@ | ||
build: function(cfg){ | ||
debug = cfg.debug; | ||
utils.debug = debug = cfg.debug; | ||
if(!cfg.target) { | ||
@@ -215,6 +169,2 @@ debug && console.log('please enter an complier path\r\n'); | ||
if(!cfg.inputEncoding || cfg.inputEncoding == 'gbk' || cfg.inputEncoding == 'GBK' || cfg.inputEncoding == 'gb2312') { | ||
cfg.inputEncoding = ''; | ||
} | ||
if(!cfg.outputEncoding || cfg.outputEncoding == 'gbk' || cfg.outputEncoding == 'GBK' || cfg.outputEncoding == 'gb2312') { | ||
@@ -224,4 +174,8 @@ cfg.outputEncoding = ''; | ||
cfg.compress = cfg.compress ==0 ? 0 : 1; | ||
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)); | ||
@@ -228,0 +182,0 @@ |
{ | ||
"name":"css-combo", | ||
"version":"0.1.2", | ||
"version":"0.1.3", | ||
"description":"css combo tool", | ||
@@ -5,0 +5,0 @@ "author":"Dorange <daxingplay@gmail.com>", |
@@ -17,7 +17,7 @@ # css-combo | ||
* target��{String} ����ļ� | ||
* inputEncoding��{String} �����ļ����룬��ѡ��Ĭ��GBK | ||
* outputEncoding��{String} ����ļ����룬��ѡ��Ĭ��GBK | ||
* inputEncoding��{String} �����ļ����룬��ѡ��Ĭ�ϼ������ļ��е�@charset���á��������ļ�û������@charset����ô������ñ�ѡ�� | ||
* outputEncoding��{String} ����ļ����룬��ѡ��Ĭ��UTF-8�����ѡ����ѹ�������ᴦ����ascii | ||
* output��{String} ���Ŀ¼������ʹ�����·�� | ||
* exclude��{Array} �������������飬��ѡ��Ĭ�ϲ�����[/.combine.css/, /-min.css/, /.combo.css/] | ||
* compress: {Boolean} �Ƿ�ѹ����Ĭ��Ϊtrue | ||
* compress: {Boolean} �Ƿ�ѹ����Ĭ��Ϊtrue����������ͬYUICompressor | ||
* debug: {Boolean} �Ƿ��ӡ��־ | ||
@@ -28,5 +28,4 @@ | ||
* ����Ŀ¼�����ʽ | ||
* <del>��ȥ��ע���ٴ��</del> | ||
* ���Ӷ�ģ��@charset�ļ�� | ||
* ����ת�������ѹ����ascii | ||
* <del>���Ӷ�ģ��@charset�ļ��</del> | ||
* <del>����ת�������ѹ����ascii</del> | ||
@@ -33,0 +32,0 @@ ## License |
@@ -6,5 +6,6 @@ var CssCombo = require('../lib/index'); | ||
debug: true, | ||
inputEncoding:'gbk', | ||
outputEncoding:'gbk', | ||
output:'css/' | ||
inputEncoding: 'gbk', | ||
outputEncoding: 'gbk', | ||
output:'css/', | ||
compress: 0 | ||
}); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
31938
5.42%12
20%682
7.4%33
-2.94%2
100%