Skip to content

Commit

Permalink
fix:修复图片正则,全局配置图片转base64可用😰
Browse files Browse the repository at this point in the history
  • Loading branch information
luckyadam committed Jul 13, 2016
1 parent 61ce99e commit 2135e64
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 17 deletions.
15 changes: 9 additions & 6 deletions lib/build/athena_mate/athena_replace.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,16 +240,14 @@ function transmitResource(filename, value, resourcePrefix, config, includeJson,
});
}
}
var mapJson = mapJSONCache[moduleName];
if (mapJson && needMd5 && !config.pack) {
value = Util.getHashName(value, mapJson);
}

var pathname = path.join(path.join(config.cwd, moduleName, 'dist/output', originalPath), '..', path.basename(value)),
query = Util.getQueryObj(pathname),
size;
if (Util.regexps.images.test(path.basename(pathname))) {
if (Util.regexps.images.test(path.basename(pathname)) && Util.existsSync(pathname)) {
// 判断是否有__inline标或者是打开了base64开关被excluded
if (query.__inline || config.base64Opts.enable && _.isArray(config.base64Opts.exclude) && config.base64Opts.exclude.length > 0 && config.base64Opts.exclude.indexOf(originalPath) == -1) {
var baseExclude = config.base64Opts.exclude;
if (query.__inline || config.base64Opts.enable && (!baseExclude || _.isArray(baseExclude) && baseExclude.indexOf(originalPath) === -1)) {
var _pathname = pathname.split('?')[0];
try {
size = fs.statSync(_pathname).size;
Expand All @@ -259,12 +257,17 @@ function transmitResource(filename, value, resourcePrefix, config, includeJson,
}
catch (e) {
gutil.log(gutil.colors.red('无法转base64,文件' + _pathname + '没有找到!'));
return value;
}
}
}
if (!(config.pack || config.compress)) {
value = resourcePrefix + moduleName + '/' + value;
}
var mapJson = mapJSONCache[moduleName];
if (mapJson && needMd5 && !config.pack) {
value = Util.getHashName(value, mapJson);
}
}
value = vStart + value + vEnd;
return value;
Expand Down
22 changes: 12 additions & 10 deletions lib/build/athena_mate/athena_replace_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,9 @@ function splitValue(filename, value, resourcePrefix, config, includeJson, needMd
if (valueArr.length > 1) {
value = vStart + resourcePrefix + Util.urlJoin(comboConf.server.shortPath, comboConf.server.flag, valueArr.join(',')) + vEnd;
} else {
if (valueArr[0].indexOf('data:image') >= 0) {
return valueArr[0];
}
value = vStart + resourcePrefix + Util.urlJoin(comboConf.server.shortPath, valueArr.join(',')) + vEnd;
}
return value;
Expand All @@ -229,8 +232,6 @@ function transmitResource(filename, value, moduleName, config, includeJson, need
if (!mapJSONModuleCache || _.isEmpty(mapJSONModuleCache)) {
mapJSONCache[moduleName] = JSON.parse(fs.readFileSync(path.join(config.cwd, moduleName, 'dist', 'map.json')).toString());
}
var originalPath = path.join(config.cwd, moduleName, 'dist/output', value);

if (Util.regexps.media.test(value)) {
includeJson[filename] = includeJson[filename] ? includeJson[filename] : [];
var isResExist = false;
Expand All @@ -246,16 +247,13 @@ function transmitResource(filename, value, moduleName, config, includeJson, need
});
}
}
var mapJson = mapJSONCache[moduleName];
if (mapJson && needMd5) {
value = Util.getHashNameServer(value, mapJson);
}
var pathname = path.join(path.join(config.cwd, moduleName, 'dist/output', originalPath), '..', path.basename(value)),
var pathname = path.join(path.join(config.cwd, moduleName, 'dist', 'output', 's', value), '..', path.basename(value)),
query = Util.getQueryObj(pathname),
size;
if (Util.regexps.images.test(path.basename(pathname))) {
if (Util.regexps.images.test(path.basename(pathname)) && Util.existsSync(pathname)) {
// 判断是否有__inline标或者是打开了base64开关被excluded
if (query.__inline || config.base64Opts.enable && _.isArray(config.base64Opts.exclude) && config.base64Opts.exclude.length > 0 && config.base64Opts.exclude.indexOf(originalPath) == -1) {
var baseExclude = config.base64Opts.exclude;
if (query.__inline || config.base64Opts.enable && (!baseExclude || _.isArray(baseExclude) && baseExclude.indexOf(value) === -1)) {
var _pathname = pathname.split('?')[0];
try {
size = fs.statSync(_pathname).size;
Expand All @@ -265,10 +263,14 @@ function transmitResource(filename, value, moduleName, config, includeJson, need
}
catch (e) {
gutil.log(gutil.colors.red('无法转base64,文件' + _pathname + '没有找到!'));
return value;
}
}
}

var mapJson = mapJSONCache[moduleName];
if (mapJson && needMd5) {
value = Util.getHashNameServer(value, mapJson);
}
value = '/' + moduleName + '/' + value;
return value;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/util/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ var Util = {
// 注释
comment: /(?:\/\*(?:[\s\S]*?)\*\/)|(?:([\s;])+\/\/(?:.*)$)/gm,
// 图片
images: /\.(jpeg|jpg|gif|png|webp)(\?[\s\S]*)/,
images: /\.(jpeg|jpg|gif|png|webp)(\?[\s\S]*)?/,
// url
url: /^(?:(?:(?:https?|ftp):)?\/\/)(?:\S+(?::\S*)?@)?(?:(?!(?:10|127)(?:\.\d{1,3}){3})(?!(?:169\.254|192\.168)(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)*(?:\.(?:[a-z\u00a1-\uffff]{2,}))\.?)(?::\d{2,5})?(?:[/?#]\S*)?$/i,
// media
Expand Down

0 comments on commit 2135e64

Please sign in to comment.