grunt-combohtml
Advanced tools
Comparing version 0.1.32 to 0.1.34
{ | ||
"name": "grunt-combohtml", | ||
"description": "combine build html with ssi.", | ||
"version": "0.1.32", | ||
"version": "0.1.34", | ||
"homepage": "https://github.com/jayli/grunt-combohtml", | ||
@@ -6,0 +6,0 @@ "author": { |
@@ -7,27 +7,25 @@ /* | ||
*/ | ||
var util = require('util'); | ||
var fs = require('fs'); | ||
var http = require('http'); | ||
var mock = require('./mock.js'); | ||
var Juicer = require("juicer"); | ||
var ssi = require('./ssi').ssi, | ||
ssiChunk = require('./ssi').ssiChunk, | ||
chunkParser = require('./chunk').parse, | ||
combineAssets = require('./url_combine').parse, | ||
path = require('path'); | ||
var path = require('path'); | ||
var isUtf8 = require('./is-utf8'); | ||
var async = require('async'); | ||
var iconv = require('iconv-lite'); | ||
var tidy = require('./tidy'); | ||
var civet = require('./civet'); | ||
var extract = require('./extract'); | ||
var relativeParse = require('./relative').parse; | ||
var concat = require('./concat').concat; | ||
var HTMLFragments = require('./html-fragments'); | ||
var async = require('async'); | ||
var Juicer = require("juicer"); | ||
var isUtf8 = require('./lib/is-utf8'); | ||
var tidy = require('./lib/tidy'); | ||
var civet = require('./lib/civet'); | ||
var extract = require('./lib/extract'); | ||
var relativeParse = require('./lib/relative').parse; | ||
var concat = require('./lib/concat').concat; | ||
var mock = require('./lib/mock'); | ||
var ssi = require('./lib/ssi').ssi, | ||
ssiChunk = require('./lib/ssi').ssiChunk, | ||
chunkParser = require('./lib/chunk').parse, | ||
combineAssets = require('./lib/url_combine').parse; | ||
// 一定是utf8格式 | ||
function mockFilter(chunk){ | ||
if(mock.checkDef(chunk)){ | ||
function mockFilter(chunk) { | ||
if (mock.checkDef(chunk)) { | ||
var pageParam = mock.getMockData(chunk); | ||
@@ -42,5 +40,5 @@ chunk = Juicer(chunk, pageParam); | ||
module.exports = function(grunt) { | ||
module.exports = function (grunt) { | ||
grunt.registerMultiTask('combohtml', 'combohtml.', function() { | ||
grunt.registerMultiTask('combohtml', 'combohtml.', function () { | ||
// Merge task-specific and/or target-specific options with these defaults. | ||
@@ -56,35 +54,35 @@ var options = this.options(); | ||
var asyncFns = []; | ||
this.files.forEach(function(v,k){ | ||
var asyncFns = []; | ||
this.files.forEach(function (v, k) { | ||
var asyncFn = function (callback) { | ||
var asyncFn = function (callback) { | ||
console.log(v.dest); | ||
var p = v.src[0]; | ||
var bf = read(p); | ||
var dirname = path.dirname(v.dest); | ||
var fDestName = path.basename(v.dest,path.extname(v.dest)); | ||
var filep = path.join(dirname, fDestName); | ||
console.log(v.dest); | ||
var p = v.src[0]; | ||
var bf = read(p); | ||
var dirname = path.dirname(v.dest); | ||
var fDestName = path.basename(v.dest, path.extname(v.dest)); | ||
var filep = path.join(dirname, fDestName); | ||
var ext = '-combo'; | ||
if(options.comboExt != undefined && options.comboExt != null){ | ||
ext = options.comboExt; | ||
} | ||
var ext = '-combo'; | ||
if (options.comboExt != undefined && options.comboExt != null) { | ||
ext = options.comboExt; | ||
} | ||
// combo后的js地址 | ||
var dest_js = filep + ext+ '.js'; | ||
// combo后的css地址 | ||
var dest_css = filep + ext + '.css'; | ||
// combo后的js地址 | ||
var dest_js = filep + ext + '.js'; | ||
// combo后的css地址 | ||
var dest_css = filep + ext + '.css'; | ||
// 一定是utf8格式的 | ||
var chunk = ssiChunk(p,bf.toString('utf8')); | ||
// 一定是utf8格式的 | ||
var chunk = ssiChunk(p, bf.toString('utf8')); | ||
// TODO: 这里的逻辑需要重构了 | ||
// TODO: 这里的逻辑需要重构了 | ||
// 需要处理js路径 | ||
if(typeof options.assetseParser == 'undefined' || (typeof options.assetseParser == 'undefined' && options.assetseParser !== false)){ | ||
if(typeof options.relative !== "undefined"){ | ||
if (typeof options.assetseParser == 'undefined' || (typeof options.assetseParser == 'undefined' && options.assetseParser !== false)) { | ||
if (typeof options.relative !== "undefined") { | ||
// 相对路径编译成绝对路径 | ||
chunk = relativeParse(chunk,options.relative,filep).content; | ||
if(options.combineAssets){ | ||
chunk = combineAssets(chunk,comboMapFile).content; | ||
chunk = relativeParse(chunk, options.relative, filep).content; | ||
if (options.combineAssets) { | ||
chunk = combineAssets(chunk, comboMapFile).content; | ||
} | ||
@@ -96,3 +94,3 @@ } else { | ||
var result = extract.parse(chunk,{ | ||
var result = extract.parse(chunk, { | ||
comboJS: isComboJS, | ||
@@ -105,14 +103,14 @@ comboCSS: isComboCSS | ||
// 未用到? | ||
if(isComboJS){ | ||
var js_content = concat(result.js,dest_js,v.orig.cwd,p,options.replacement); | ||
if (isComboJS) { | ||
var js_content = concat(result.js, dest_js, v.orig.cwd, p, options.replacement); | ||
} | ||
if(isComboCSS){ | ||
var css_content = concat(result.css,dest_css,v.orig.cwd,p,options.replacement); | ||
if (isComboCSS) { | ||
var css_content = concat(result.css, dest_css, v.orig.cwd, p, options.replacement); | ||
} | ||
if(isComboJS){ | ||
if (isComboJS) { | ||
chunk = chunk.replace('@@script', fDestName + ext + '.js'); | ||
} | ||
if(isComboCSS){ | ||
if (isComboCSS) { | ||
chunk = chunk.replace('@@style', fDestName + ext + '.css'); | ||
@@ -123,70 +121,75 @@ } | ||
if(!(options.convert2vm === false)){ | ||
outputVmFile(chunk,filep); | ||
sholdtidy = false; | ||
} | ||
if (!(options.convert2vm === false)) { | ||
outputVmFile(chunk, filep); | ||
sholdtidy = false; | ||
} | ||
if(!(options.convert2php === false)){ | ||
outputPhpFile(chunk,filep); | ||
sholdtidy = false; | ||
} | ||
if (!(options.convert2php === false)) { | ||
outputPhpFile(chunk, filep); | ||
sholdtidy = false; | ||
} | ||
if(sholdtidy && options.tidy){ | ||
chunk = tidy(chunk,{ | ||
'indent_size': 4, | ||
'indent_char': ' ', | ||
'brace_style': 'expand', | ||
'unformatted': ['a', 'sub', 'sup', 'b', 'i', 'u','script'] | ||
}); | ||
} | ||
if (sholdtidy && options.tidy) { | ||
chunk = tidy(chunk, { | ||
'indent_size': 4, | ||
'indent_char': ' ', | ||
'brace_style': 'expand', | ||
'unformatted': ['a', 'sub', 'sup', 'b', 'i', 'u', 'script'] | ||
}); | ||
} | ||
// meta 配置处理,加入到 </head> 前 | ||
if(options.meta){ | ||
var metaElements = genMetas(options.meta, v.dest); | ||
chunk = chunk.replace(/<\/head>/i, metaElements + '</head>'); | ||
} | ||
// meta 配置处理,加入到 </head> 前 | ||
if (options.meta) { | ||
var metaElements = genMetas(options.meta, v.dest); | ||
chunk = chunk.replace(/<\/head>/i, metaElements + '</head>'); | ||
} | ||
chunkParser(chunk,function(chunk){ | ||
if(options.mockFilter){ | ||
chunk = mockFilter(chunk); | ||
} | ||
chunk = teardownChunk(chunk,options.encoding); | ||
if(!(chunk instanceof Buffer)){ | ||
chunk = new Buffer(chunk); | ||
} | ||
if(options.encoding == 'gbk'){ | ||
chunk = iconv.encode(iconv.decode(chunk, 'utf8'),'gbk'); | ||
} | ||
fs.writeFileSync(v.dest,chunk); | ||
callback(); | ||
}); | ||
chunkParser(chunk, function (chunk) { | ||
if (options.mockFilter) { | ||
chunk = mockFilter(chunk); | ||
} | ||
chunk = teardownChunk(chunk, options.encoding); | ||
if (!(chunk instanceof Buffer)) { | ||
chunk = new Buffer(chunk); | ||
} | ||
if (options.encoding == 'gbk') { | ||
chunk = iconv.encode(iconv.decode(chunk, 'utf8'), 'gbk'); | ||
} | ||
fs.writeFileSync(v.dest, chunk); | ||
callback(); | ||
}); | ||
}; | ||
}; | ||
asyncFns.push(asyncFn); | ||
asyncFns.push(asyncFn); | ||
}); | ||
async.parallel(asyncFns, function (err, result) { | ||
var HTMLFragments; | ||
if (options.htmlProxy) { | ||
HTMLFragments = require('./lib/html-fragments'); | ||
} | ||
if (err) { | ||
async.parallel(asyncFns, function (err, result) { | ||
console.warn('combohtml 生成有错误'); | ||
console.error(err); | ||
if (err) { | ||
done(); | ||
console.warn('combohtml 生成有错误'); | ||
console.error(err); | ||
} else { | ||
done(); | ||
// HTML 区块代理 | ||
if (options.htmlProxy) { | ||
HTMLFragments.process(options.htmlProxy, options.htmlProxyDestDir, done); | ||
} else { | ||
done(); | ||
} | ||
} else { | ||
} | ||
// HTML 区块代理 | ||
if (options.htmlProxy) { | ||
HTMLFragments.process(options.htmlProxy, options.htmlProxyDestDir, done); | ||
} else { | ||
done(); | ||
} | ||
}); | ||
} | ||
}); | ||
}); | ||
@@ -202,3 +205,3 @@ | ||
*/ | ||
function genMetas (metaConfig, filePath) { | ||
function genMetas(metaConfig, filePath) { | ||
@@ -208,3 +211,3 @@ /** | ||
*/ | ||
Juicer.register('regexp', function (value, regexp, replacement){ | ||
Juicer.register('regexp', function (value, regexp, replacement) { | ||
regexp = new RegExp(regexp, 'igm'); | ||
@@ -214,18 +217,18 @@ return value.replace(regexp, replacement); | ||
var ret = [], | ||
ts = +new Date, | ||
// 目前提供的可用变量 | ||
metaReplacements = { | ||
'path': filePath.replace('build/', ''), // 文件路径 | ||
'ts': ts // 时间戳 | ||
}, | ||
metaTpl = Juicer('<meta name="${metaKey}" content="${metaValue}">'), | ||
platform = process.platform, | ||
ctrlChar; | ||
var ret = [], | ||
ts = Date.now(), | ||
// 目前提供的可用变量 | ||
metaReplacements = { | ||
'path': filePath.replace('build/', ''), // 文件路径 | ||
'ts': ts // 时间戳 | ||
}, | ||
metaTpl = Juicer('<meta name="${metaKey}" content="${metaValue}">'), | ||
platform = process.platform, | ||
ctrlChar; | ||
// 根据操作提供选择合适的换行控制符 | ||
if(platform == 'win32'){ | ||
if (platform == 'win32') { | ||
// Windows | ||
ctrlChar = '\n'; | ||
} else if(platform == 'darwin'){ | ||
} else if (platform == 'darwin') { | ||
// Mac OS | ||
@@ -239,15 +242,15 @@ ctrlChar = '\r'; | ||
// 遍历各个 meta Key | ||
for(var metaKey in metaConfig) { | ||
if(metaConfig.hasOwnProperty(metaKey)){ | ||
var metaValue = metaConfig[metaKey]; | ||
metaValue = Juicer(metaValue + '', metaReplacements); | ||
var metaStr = metaTpl.render({ | ||
metaKey: metaKey, | ||
metaValue: metaValue | ||
}); | ||
ret.push('\t' + metaStr); | ||
} | ||
} | ||
for (var metaKey in metaConfig) { | ||
if (metaConfig.hasOwnProperty(metaKey)) { | ||
var metaValue = metaConfig[metaKey]; | ||
metaValue = Juicer(metaValue + '', metaReplacements); | ||
var metaStr = metaTpl.render({ | ||
metaKey: metaKey, | ||
metaValue: metaValue | ||
}); | ||
ret.push('\t' + metaStr); | ||
} | ||
} | ||
return ret.join(ctrlChar) + ctrlChar; | ||
return ret.join(ctrlChar) + ctrlChar; | ||
@@ -257,8 +260,8 @@ } | ||
// 传入的chunk一定是utf8的 | ||
function teardownChunk(chunk,encoding){ | ||
if(!(chunk instanceof Buffer)){ | ||
function teardownChunk(chunk, encoding) { | ||
if (!(chunk instanceof Buffer)) { | ||
chunk = new Buffer(chunk); | ||
} | ||
if(encoding == 'gbk'){ | ||
chunk = iconv.encode(iconv.decode(chunk, 'utf8'),'gbk'); | ||
if (encoding == 'gbk') { | ||
chunk = iconv.encode(iconv.decode(chunk, 'utf8'), 'gbk'); | ||
} | ||
@@ -268,54 +271,54 @@ return chunk; | ||
function outputVmFile(content,fp){ | ||
function outputVmFile(content, fp) { | ||
var ctxt = civet.juicer2vm(content); | ||
fs.writeFileSync(fp + '.vm.html', ctxt); | ||
fs.writeFileSync(fp + '.vm.html', ctxt); | ||
} | ||
function outputPhpFile(content,fp){ | ||
function outputPhpFile(content, fp) { | ||
var ctxt = civet.juicer2php(content); | ||
fs.writeFileSync(fp + '.php.html', ctxt); | ||
fs.writeFileSync(fp + '.php.html', ctxt); | ||
} | ||
function writeFile(page, prjInfo, pageContent) { | ||
var pagePathDir = path.dirname(page); | ||
if (prjInfo.charset[0].match(/gbk/i)) { | ||
pageContent = iconv.encode(pageContent, 'gbk'); | ||
} | ||
fs.writeFileSync(page, pageContent); | ||
return; | ||
var pagePathDir = path.dirname(page); | ||
if (prjInfo.charset[0].match(/gbk/i)) { | ||
pageContent = iconv.encode(pageContent, 'gbk'); | ||
} | ||
fs.writeFileSync(page, pageContent); | ||
return; | ||
} | ||
function consoleColor(str,num){ | ||
function consoleColor(str, num) { | ||
if (!num) { | ||
num = '32'; | ||
} | ||
return "\033[" + num +"m" + str + "\033[0m"; | ||
return "\033[" + num + "m" + str + "\033[0m"; | ||
} | ||
function green(str){ | ||
return consoleColor(str,32); | ||
function green(str) { | ||
return consoleColor(str, 32); | ||
} | ||
function yellow(str){ | ||
return consoleColor(str,33); | ||
function yellow(str) { | ||
return consoleColor(str, 33); | ||
} | ||
function red(str){ | ||
return consoleColor(str,31); | ||
function red(str) { | ||
return consoleColor(str, 31); | ||
} | ||
function blue(str){ | ||
return consoleColor(str,34); | ||
function blue(str) { | ||
return consoleColor(str, 34); | ||
} | ||
function log(statCode, url, err) { | ||
var logStr = blue(statCode) + ' - ' + url ; | ||
if (err) | ||
logStr += ' - ' + red(err); | ||
console.log(logStr); | ||
var logStr = blue(statCode) + ' - ' + url; | ||
if (err) | ||
logStr += ' - ' + red(err); | ||
console.log(logStr); | ||
} | ||
function isDir(dir){ | ||
if(fs.existsSync(dir)){ | ||
function isDir(dir) { | ||
if (fs.existsSync(dir)) { | ||
var stat = fs.lstatSync(dir); | ||
@@ -328,4 +331,4 @@ return stat.isDirectory(); | ||
function isFile(dir){ | ||
if(fs.existsSync(dir)){ | ||
function isFile(dir) { | ||
if (fs.existsSync(dir)) { | ||
var stat = fs.lstatSync(dir); | ||
@@ -339,10 +342,10 @@ return stat.isFile(); | ||
// 得到的一定是utf8编码的buffer | ||
function read(file){ | ||
function read(file) { | ||
var fd = fs.readFileSync(file), | ||
bf; | ||
bf; | ||
if(isUtf8(fd)){ | ||
if (isUtf8(fd)) { | ||
bf = fs.readFileSync(file); | ||
} else { | ||
bf = iconv.encode(iconv.decode(fd, 'gbk'),'utf8'); | ||
bf = iconv.encode(iconv.decode(fd, 'gbk'), 'utf8'); | ||
} | ||
@@ -352,5 +355,5 @@ return bf; | ||
function die(){ | ||
console.log.apply(this,arguments) | ||
function die() { | ||
console.log.apply(this, arguments); | ||
process.exit(); | ||
} |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
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
3993
158336
4