Skip to content
This repository has been archived by the owner on Oct 9, 2020. It is now read-only.

Commit

Permalink
exact path matching fix for getCacnonicalName
Browse files Browse the repository at this point in the history
  • Loading branch information
guybedford committed Aug 23, 2015
1 parent 1e93abb commit 290b84b
Showing 1 changed file with 38 additions and 19 deletions.
57 changes: 38 additions & 19 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,17 @@ exports.coercePath = coercePath;

var absURLRegEx = /^[^\/]+:\/\//;

function normalizePath(loader, path) {
var curPath;
if (loader.paths[path][0] == '.')
curPath = decodeURI(url.resolve(toFileURL(process.cwd()) + '/', loader.paths[path]));
else
curPath = decodeURI(url.resolve(loader.baseURL, loader.paths[path]));
if (loader.defaultJSExtensions && curPath.substr(curPath.length - 3, 3) != '.js')
curPath += '.js';
return curPath;
}

exports.getCanonicalName = getCanonicalName;
function getCanonicalName(loader, normalized) {
// remove the plugin part first
Expand All @@ -55,26 +66,35 @@ function getCanonicalName(loader, normalized) {
}

// now just reverse apply paths rules to get canonical name
var pathMatch, pathMatchLength = 0;
var curMatchlength;
var pathMatch;

// first check exact path matches
for (var p in loader.paths) {
// normalize the output path
var curPath
if (loader.paths[p][0] == '.')
curPath = decodeURI(url.resolve(toFileURL(process.cwd()) + '/', loader.paths[p]));
else
curPath = decodeURI(url.resolve(loader.baseURL, loader.paths[p]));

// do reverse match
var wIndex = curPath.indexOf('*');
if (wIndex === -1) {
if (normalized === curPath) {
// always stop on first exact match
pathMatch = p;
break;
}
if (loader.paths[p].indexOf('*') != -1)
continue;

var curPath = normalizePath(loader, p);

if (normalized === curPath) {
// always stop on first exact match
pathMatch = p;
break;
}
else {
}

// then wildcard matches
var pathMatchLength = 0;
var curMatchlength;
if (!pathMatch)
for (var p in loader.paths) {
if (loader.paths[p].indexOf('*') == -1)
continue;

// normalize the output path
var curPath = normalizePath(loader, p);

// do reverse match
var wIndex = curPath.indexOf('*');
if (normalized.substr(0, wIndex) === curPath.substr(0, wIndex)
&& normalized.substr(normalized.length - curPath.length + wIndex + 1) === curPath.substr(wIndex + 1)) {
curMatchLength = curPath.split('/').length;
Expand All @@ -84,7 +104,6 @@ function getCanonicalName(loader, normalized) {
}
}
}
}

// when no path was matched, act like the standard rule is *: baseURL/*
if (!pathMatch) {
Expand Down

0 comments on commit 290b84b

Please sign in to comment.