Skip to content

Commit 5ff8c75

Browse files
authored
Merge pull request #470 from nomcopter/optimize-relate-context-performance
fix: optimize contextNormalPath #469
2 parents 3c4fbb3 + cb03413 commit 5ff8c75

File tree

1 file changed

+24
-8
lines changed

1 file changed

+24
-8
lines changed

lib/util/relate-context.js

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -76,23 +76,39 @@ const relateNormalPathSet = (exports.relateNormalPathSet = function relateNormal
7676
*
7777
*/
7878

79+
// Cache whether we need to replace path.sep because contextNormalPath is called _very_ frequently
80+
const resolveRelativeCompilerContext =
81+
'/' === path.sep
82+
? function(context, key) {
83+
return path.resolve(context, key);
84+
}
85+
: function(context, key) {
86+
return path.resolve(context, key).replace(/\//g, path.sep);
87+
};
88+
7989
const contextNormalPath = (exports.contextNormalPath = function contextNormalPath(
8090
compiler,
8191
key,
8292
) {
83-
if (typeof key !== 'string') {
93+
if (typeof key !== 'string' || key === '') {
8494
return key;
8595
}
96+
97+
const context = compilerContext(compiler);
8698
if (key === '.') {
87-
return compilerContext(compiler);
99+
return context;
88100
}
89-
if (key === '') {
90-
return '';
101+
102+
const markIndex = key.indexOf('?');
103+
if (markIndex === -1) {
104+
return resolveRelativeCompilerContext(context, key);
91105
}
92-
const abs = path.resolve(compilerContext(compiler), key.split('?')[0]);
93-
return [abs.replace(/\//g, path.sep)]
94-
.concat(key.split('?').slice(1))
95-
.join('?');
106+
107+
const abs = resolveRelativeCompilerContext(
108+
context,
109+
key.substring(0, markIndex),
110+
);
111+
return abs + '?' + key.substring(markIndex + 1);
96112
});
97113

98114
const contextNormalRequest = (exports.contextNormalRequest = function contextNormalRequest(

0 commit comments

Comments
 (0)