diff --git a/lib/util/relate-context.js b/lib/util/relate-context.js index 43d085ea..3bea5f8e 100644 --- a/lib/util/relate-context.js +++ b/lib/util/relate-context.js @@ -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(