Comparing version 1.0.13 to 1.0.14
{ | ||
"name": "hrt", | ||
"version": "1.0.13", | ||
"version": "1.0.14", | ||
"description": "HTTP Rewrite Tool", | ||
@@ -5,0 +5,0 @@ "author": "Longhao Luo <lhluo@tudou.com>", |
@@ -0,1 +1,2 @@ | ||
var Fs = require('fs'); | ||
var Path = require('path'); | ||
@@ -7,2 +8,4 @@ var Util = require('../util'); | ||
var RE_AUTOFIXNAME = /define\((?=[^'"])/; | ||
// URL移除版本号 | ||
@@ -18,5 +21,56 @@ function stripVersionInfo(url) { | ||
// 支持相对路径,补充模块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]; | ||
if(f.charAt(0) == '.') { | ||
f = pathname.slice(11).replace(/[\w-]+\.js$/, '').replace(/^\//, '') + f; | ||
f = f.replace(/\w+\/\.\.\//g, '').replace(/\.\//g, ''); | ||
} | ||
else if(f.charAt(0) == '/') { | ||
f = f.slice(1); | ||
} | ||
return arguments[1] + f + arguments[1]; | ||
}); | ||
return s; | ||
}); | ||
data = data.replace(/\brequire\s*\.\s*text\s*\(\s*(['"])([\w-./]+)\1\s*\)/g, function() { | ||
var f = arguments[2]; | ||
if(/^[a-z_/]/i.test(f)) { | ||
f = root + '/src/js/' + f; | ||
} | ||
else { | ||
f = path.replace(/[\w-]+\.js$/, '') + f; | ||
f = f.replace(/\w+\/\.\.\//g, '').replace(/\.\//g, ''); | ||
} | ||
var s = ''; | ||
try { | ||
s = Fs.readFileSync(f, { | ||
encoding: 'utf-8' | ||
}); | ||
} catch(e) { | ||
console.error(e); | ||
s = e.toString(); | ||
} | ||
s = s.replace(/^\uFEFF/, ''); | ||
s = s.replace(/(\r\n|\r|\n)\s*/g, ' '); | ||
s = s.replace(/\\/g, '\\\\'); | ||
s = s.replace(/'/g, "\\'"); | ||
return "'" + s + "'"; | ||
}); | ||
return data; | ||
} | ||
// 合并本地文件 | ||
function merge(path, callback) { | ||
var root = path.replace(/^(.*?)[\\\/](src|build|dist)[\\\/].*$/, '$1'); | ||
var pathname = this.req.url.replace(/^https?:\/\/[^\/]+/, ''); | ||
@@ -27,3 +81,3 @@ var newPath = path.split(Path.sep).join('/'); | ||
if (/\.less$/.test(newPath)) { | ||
var content = Util.readFileSync(path, 'utf-8'); | ||
var str = Util.readFileSync(path, 'utf-8'); | ||
@@ -37,3 +91,3 @@ var parser = new(Less.Parser)({ | ||
parser.parse(content, function(error, tree) { | ||
parser.parse(str, function(error, tree) { | ||
if (error) { | ||
@@ -47,9 +101,19 @@ return console.error(error); | ||
// Insert debug script | ||
if (/src\/js\/(lib|lite|loader)\.js$/.test(newPath)) { | ||
var content = Util.readFileSync(path, 'utf-8'); | ||
// if (/src\/js\/(lib|lite|loader)\.js$/.test(newPath)) { | ||
var debugContent = Util.readFileSync(root + '/src/js/lib/debug.js', 'utf-8'); | ||
// var str = Util.readFileSync(path, 'utf-8'); | ||
return callback('application/javascript', content + debugContent); | ||
// var debugStr = Util.readFileSync(root + '/src/js/lib/debug.js', 'utf-8'); | ||
// 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); | ||
return callback('application/javascript', str); | ||
} | ||
@@ -56,0 +120,0 @@ |
1160614
309
24024
44