Skip to content

Commit

Permalink
Merge pull request #470 from nomcopter/optimize-relate-context-perfor…
Browse files Browse the repository at this point in the history
…mance

fix: optimize contextNormalPath #469
  • Loading branch information
mzgoddard committed Dec 9, 2018
2 parents 3c4fbb3 + cb03413 commit 5ff8c75
Showing 1 changed file with 24 additions and 8 deletions.
32 changes: 24 additions & 8 deletions lib/util/relate-context.js
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 5ff8c75

Please sign in to comment.