From cb03413792965a396e1b64c4abc30123bec0aeb5 Mon Sep 17 00:00:00 2001 From: Kevin Verdieck Date: Fri, 16 Nov 2018 15:22:17 -0800 Subject: [PATCH] fix: optimize contextNormalPath mzgoddard/hard-source-webpack-plugin#469 --- lib/util/relate-context.js | 32 ++++++++++++++++++++++++-------- 1 file changed, 24 insertions(+), 8 deletions(-) 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(