Comparing version 0.1.7 to 0.2.0
{ | ||
"name": "more-css", | ||
"version": "0.1.7", | ||
"version": "0.2.0", | ||
"description": "a css pre-compiler & agressive compressor", | ||
@@ -5,0 +5,0 @@ "maintainers": [ |
@@ -44,4 +44,12 @@ ##A css pre-compiler by javascript | ||
more.root(root:String):String | ||
设置相对根路径的根路径,此在build()方法中用到,且不使用相对根路径的情况下无需设置。 | ||
more.build(file:String, noImport:Boolean = false):String | ||
构建css文件,将一个可能包含多个文件的文件打包为一个单独的字符串。noImport为true时不处理@import的文件,默认包括。 | ||
# License | ||
[MIT License] |
@@ -9,2 +9,3 @@ var CssLexer = require('./lexer/CssLexer'), | ||
cleanCSS = require('clean-css'), | ||
fs = require('fs'), | ||
res, | ||
@@ -267,1 +268,82 @@ node, | ||
}; | ||
var root; | ||
exports.root = function(r) { | ||
if(r) { | ||
root = r; | ||
} | ||
return root; | ||
}; | ||
function removeImport(s) { | ||
//0��ʼ��1�ַ��� | ||
var state = 0; | ||
for(var i = 0; i < s.length; i++) { | ||
var c = s.charAt(i); | ||
if(c == '/') { | ||
c = s.charAt(i + 1); | ||
if(c == '/') { | ||
i = s.indexOf('\n', i + 2); | ||
if(i == -1) { | ||
i = s.length; | ||
} | ||
} | ||
else if(c == '*') { | ||
i = s.indexOf('*/', i + 2); | ||
if(i == -1) { | ||
i = s.length; | ||
} | ||
} | ||
} | ||
else if(c == '"' || c == "'") { | ||
for(var j = i + 1; j < s.length; j++) { | ||
var c2 = s.charAt(j); | ||
if(c == c2) { | ||
i = j; | ||
break; | ||
} | ||
else if(c2 == '\\') { | ||
j++; | ||
} | ||
} | ||
} | ||
else if(c == '@' && s.slice(i, i + 7) == '@import') { | ||
var j = s.indexOf(';', i + 7) + 1; | ||
s = s.slice(0, i) + s.slice(j); | ||
} | ||
} | ||
return s; | ||
} | ||
function build(file, res, noImport) { | ||
var s = fs.readFileSync(file, { | ||
encoding: 'utf-8' | ||
}); | ||
var cur = file.replace(/\w+\.css$/, ''); | ||
s = module.exports.parse(s, buildHash[file]); | ||
if(!noImport) { | ||
s = removeImport(s); | ||
var impts = module.exports.imports(); | ||
var vars = module.exports.vars(); | ||
impts.forEach(function(impt) { | ||
if(impt.charAt(0) == '/') { | ||
if(!root) { | ||
throw new Error('����@import����Ը�·���ļ���Ҫ��������root:\n' + file + ' -> ' + impt); | ||
} | ||
impt = root.replace(/[/\\]$/, '') + impt; | ||
} | ||
else { | ||
impt = cur + impt.replace(/\w+\/\.\.\\/g, '').replace(/\.\//g, ''); | ||
} | ||
buildHash[impt] = vars; | ||
build(impt, res, noImport); | ||
}); | ||
} | ||
res.push(s); | ||
} | ||
var buildHash; | ||
exports.build = function(file, noImport) { | ||
buildHash = {}; | ||
var res = []; | ||
if(fs.readFileSync) { | ||
build(file, res, noImport); | ||
} | ||
return res.join(''); | ||
} |
@@ -10,2 +10,3 @@ define(function(require, exports) { | ||
cleanCSS = require('clean-css'), | ||
fs = require('fs'), | ||
res, | ||
@@ -268,2 +269,83 @@ node, | ||
}; | ||
var root; | ||
exports.root = function(r) { | ||
if(r) { | ||
root = r; | ||
} | ||
return root; | ||
}; | ||
function removeImport(s) { | ||
//0��ʼ��1�ַ��� | ||
var state = 0; | ||
for(var i = 0; i < s.length; i++) { | ||
var c = s.charAt(i); | ||
if(c == '/') { | ||
c = s.charAt(i + 1); | ||
if(c == '/') { | ||
i = s.indexOf('\n', i + 2); | ||
if(i == -1) { | ||
i = s.length; | ||
} | ||
} | ||
else if(c == '*') { | ||
i = s.indexOf('*/', i + 2); | ||
if(i == -1) { | ||
i = s.length; | ||
} | ||
} | ||
} | ||
else if(c == '"' || c == "'") { | ||
for(var j = i + 1; j < s.length; j++) { | ||
var c2 = s.charAt(j); | ||
if(c == c2) { | ||
i = j; | ||
break; | ||
} | ||
else if(c2 == '\\') { | ||
j++; | ||
} | ||
} | ||
} | ||
else if(c == '@' && s.slice(i, i + 7) == '@import') { | ||
var j = s.indexOf(';', i + 7) + 1; | ||
s = s.slice(0, i) + s.slice(j); | ||
} | ||
} | ||
return s; | ||
} | ||
function build(file, res, noImport) { | ||
var s = fs.readFileSync(file, { | ||
encoding: 'utf-8' | ||
}); | ||
var cur = file.replace(/\w+\.css$/, ''); | ||
s = module.exports.parse(s, buildHash[file]); | ||
if(!noImport) { | ||
s = removeImport(s); | ||
var impts = module.exports.imports(); | ||
var vars = module.exports.vars(); | ||
impts.forEach(function(impt) { | ||
if(impt.charAt(0) == '/') { | ||
if(!root) { | ||
throw new Error('����@import����Ը�·���ļ���Ҫ��������root:\n' + file + ' -> ' + impt); | ||
} | ||
impt = root.replace(/[/\\]$/, '') + impt; | ||
} | ||
else { | ||
impt = cur + impt.replace(/\w+\/\.\.\\/g, '').replace(/\.\//g, ''); | ||
} | ||
buildHash[impt] = vars; | ||
build(impt, res, noImport); | ||
}); | ||
} | ||
res.push(s); | ||
} | ||
var buildHash; | ||
exports.build = function(file, noImport) { | ||
buildHash = {}; | ||
var res = []; | ||
if(fs.readFileSync) { | ||
build(file, res, noImport); | ||
} | ||
return res.join(''); | ||
} | ||
}); |
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
404637
76
9228
54
12