Skip to content

Commit

Permalink
Merge pull request #2 from nomcopter/optimize-relate-context-performance
Browse files Browse the repository at this point in the history
fix: optimize contextNormalPath mzgoddard#469
  • Loading branch information
nomcopter authored Nov 16, 2018
2 parents dddfeed + cb03413 commit 4dd881c
Showing 1 changed file with 24 additions and 8 deletions.
32 changes: 24 additions & 8 deletions lib/util/relate-context.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,23 +76,39 @@ const relateNormalPathSet = (exports.relateNormalPathSet = function relateNormal
*
*/

// Cache whether we need to replace path.sep because contextNormalPath is called _very_ frequently
const resolveRelativeCompilerContext =
'/' === path.sep
? function(context, key) {
return path.resolve(context, key);
}
: function(context, key) {
return path.resolve(context, key).replace(/\//g, path.sep);
};

const contextNormalPath = (exports.contextNormalPath = function contextNormalPath(
compiler,
key,
) {
if (typeof key !== 'string') {
if (typeof key !== 'string' || key === '') {
return key;
}

const context = compilerContext(compiler);
if (key === '.') {
return compilerContext(compiler);
return context;
}
if (key === '') {
return '';

const markIndex = key.indexOf('?');
if (markIndex === -1) {
return resolveRelativeCompilerContext(context, key);
}
const abs = path.resolve(compilerContext(compiler), key.split('?')[0]);
return [abs.replace(/\//g, path.sep)]
.concat(key.split('?').slice(1))
.join('?');

const abs = resolveRelativeCompilerContext(
context,
key.substring(0, markIndex),
);
return abs + '?' + key.substring(markIndex + 1);
});

const contextNormalRequest = (exports.contextNormalRequest = function contextNormalRequest(
Expand Down

0 comments on commit 4dd881c

Please sign in to comment.