Comparing version 1.0.14 to 1.0.15
@@ -7,2 +7,5 @@ ####################################################################### | ||
ver 1.0.15 | ||
* 改进:土豆插件改进。 | ||
ver 1.0.14 | ||
@@ -9,0 +12,0 @@ * 改进:土豆插件改进。 |
{ | ||
"name": "hrt", | ||
"version": "1.0.14", | ||
"version": "1.0.15", | ||
"description": "HTTP Rewrite Tool", | ||
@@ -5,0 +5,0 @@ "author": "Longhao Luo <lhluo@tudou.com>", |
@@ -20,13 +20,16 @@ var Fs = require('fs'); | ||
// 支持相对路径,补充模块ID,模板转换等 | ||
function fixModule(root, path, pathname, data) { | ||
if(/\bdefine\s*\(/.test(data) && !/\bdefine\s*\(\s*['"]/.test(data)) { | ||
data = data.replace(/\b(define\s*\(\s*)/, '$1\'' + pathname.slice(11).replace(/\.js$/, '') + '\', ').replace(/^\//, ''); | ||
} | ||
data = data.replace(/\brequire\s*\(\s*[['"].+,\s*function\s*\(/g, function() { | ||
var s = arguments[0]; | ||
s = s.replace(/(['"])(.+?)\1/g, function() { | ||
var f = arguments[2]; | ||
// 将JS代码改成AMD模块,包含路径转换,补充模块ID,模板转换等 | ||
function fixModule(path, str) { | ||
var root = path.replace(/^(.*?)[\\\/](src|build|dist)[\\\/].*$/, '$1'); | ||
var relativePath = path.split(Path.sep).join('/').replace(/^.+\/src\/js\//, ''); | ||
var mid = relativePath.replace(/\.js$/, ''); | ||
function fixDep(s, format) { | ||
if (format) { | ||
s = s.replace(/\s/g, ''); | ||
} | ||
return s.replace(/(['"])(.+?)\1(,?)/g, function($0, $1, $2, $3) { | ||
var f = $2; | ||
if(f.charAt(0) == '.') { | ||
f = pathname.slice(11).replace(/[\w-]+\.js$/, '').replace(/^\//, '') + f; | ||
f = relativePath.replace(/[\w-]+\.js$/, '') + f; | ||
f = f.replace(/\w+\/\.\.\//g, '').replace(/\.\//g, ''); | ||
@@ -37,9 +40,32 @@ } | ||
} | ||
return arguments[1] + f + arguments[1]; | ||
}); | ||
return s; | ||
if (format) { | ||
return '\n "' + f + '"' + $3 + '\n'; | ||
} else { | ||
return $1 + f + $1 + $3; | ||
} | ||
}).replace(/,\n\n/g, ',\n'); | ||
} | ||
// 补充模块ID | ||
if(/(?:^|[^\w\.])define\s*\(/.test(str) && !/(?:^|[^\w\.])define\s*\(\s*['"]/.test(str)) { | ||
str = str.replace(/\b(define\s*\(\s*)/, '$1"' + mid + '", '); | ||
} | ||
// 补齐依赖 | ||
str = str.replace(/((?:^|[^\w\.])define\s*\(\s*['"].*?['"]\s*,\s*)([['"][\s\S]+?)(,\s*function\s*\()/g, function($0, $1, $2, $3) { | ||
return $1 + fixDep($2, true) + $3; | ||
}); | ||
str = str.replace(/((?:^|[^\w\.])require\s*\(\s*)([\['"][\s\S]+?)(,\s*function\s*\()/g, function($0, $1, $2, $3) { | ||
return $1 + fixDep($2, false) + $3; | ||
}); | ||
str = str.replace(/((?:^|[^\w\.])define\s*\(\s*['"].*?['"]\s*)(,\s*function\s*\()/g, '$1,[]$2'); | ||
data = data.replace(/\brequire\s*\.\s*text\s*\(\s*(['"])([\w-./]+)\1\s*\)/g, function() { | ||
var f = arguments[2]; | ||
// 非AMD模块 | ||
if(!/(?:^|[^\w\.])(define|require)\s*\(/.test(str)) { | ||
return str += '\n/* autogeneration */\ndefine("' + mid + '", [], function(){});\n'; | ||
} | ||
// JS模板转换 | ||
str = str.replace(/(\b)require\.text\(\s*(['"])(.+?)\2\s*\)/g, function($0, $1, $2, $3) { | ||
var f = $3; | ||
if(/^[a-z_/]/i.test(f)) { | ||
@@ -66,6 +92,6 @@ f = root + '/src/js/' + f; | ||
s = s.replace(/'/g, "\\'"); | ||
return "'" + s + "'"; | ||
return $1 + "'" + s + "'"; | ||
}); | ||
return data; | ||
return str; | ||
} | ||
@@ -76,3 +102,2 @@ | ||
var root = path.replace(/^(.*?)[\\\/](src|build|dist)[\\\/].*$/, '$1'); | ||
var pathname = this.req.url.replace(/^https?:\/\/[^\/]+/, ''); | ||
@@ -101,18 +126,12 @@ var newPath = path.split(Path.sep).join('/'); | ||
// if (/src\/js\/(lib|lite|loader)\.js$/.test(newPath)) { | ||
if (/src\/js\/(lib|lite|loader)\.js$/.test(newPath)) { | ||
// var str = Util.readFileSync(path, 'utf-8'); | ||
// var str = Util.readFileSync(path, 'utf-8'); | ||
// var debugStr = Util.readFileSync(root + '/src/js/lib/debug.js', 'utf-8'); | ||
// return callback('application/javascript', str + debugStr); | ||
// var debugStr = Util.readFileSync(root + '/src/js/lib/debug.js', 'utf-8'); | ||
} else if (/src\/js\/.+\.js$/.test(newPath)) { | ||
// return callback('application/javascript', str + debugStr); | ||
// } | ||
if (/src\/js\/.+\.js$/.test(newPath)) { | ||
var str = Util.readFileSync(path, 'utf-8'); | ||
str = fixModule(root, path, pathname, str); | ||
str = fixModule(path, str); | ||
return callback('application/javascript', str); | ||
@@ -119,0 +138,0 @@ } |
1161447
24044