Skip to content

Commit d2df917

Browse files
committed
Improve alias finding
1 parent 3cc6909 commit d2df917

File tree

3 files changed

+26
-6
lines changed

3 files changed

+26
-6
lines changed

packages/app/src/sandbox/eval/manager.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,12 @@ export default class Manager {
445445
this.webpackHMR = true;
446446
}
447447

448+
getPresetAliasedPath(path: string) {
449+
return this.preset
450+
.getAliasedPath(path)
451+
.replace(/.*\{\{sandboxRoot\}\}/, '');
452+
}
453+
448454
resolveModule(
449455
path: string,
450456
currentPath: string,
@@ -462,9 +468,7 @@ export default class Manager {
462468
if (cachedPath) {
463469
resolvedPath = cachedPath;
464470
} else {
465-
const presetAliasedPath = this.preset
466-
.getAliasedPath(path)
467-
.replace(/.*\{\{sandboxRoot\}\}/, '');
471+
const presetAliasedPath = this.getPresetAliasedPath(path);
468472

469473
const aliasedPath = this.getAliasedDependencyPath(
470474
presetAliasedPath,

packages/app/src/sandbox/eval/presets/index.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ export default class Preset {
7373

7474
this.hasDotEnv = hasDotEnv || false;
7575
this.alias = alias || {};
76+
this.aliasedPathCache = {};
7677
this.defaultAliases = alias || {};
7778
this.ignoredExtensions = ignoredExtensions || ['js', 'jsx', 'json'];
7879

@@ -84,13 +85,21 @@ export default class Preset {
8485

8586
setAdditionalAliases = (aliases: Object) => {
8687
this.alias = { ...this.defaultAliases, ...aliases };
88+
this.aliasedPathCache = {};
8789
};
8890

91+
aliasedPathCache = {};
8992
/**
9093
* Checks if there is an alias given for the path, if there is it will return
9194
* the altered path, otherwise it will just return the known path.
9295
*/
9396
getAliasedPath(path: string): string {
97+
if (this.aliasedPathCache[path] === null) {
98+
return path;
99+
} else if (this.aliasedPathCache[path]) {
100+
return this.aliasedPathCache[path];
101+
}
102+
94103
const aliases = Object.keys(this.alias);
95104

96105
const exactAliases = aliases.filter(a => a.endsWith('$'));
@@ -105,10 +114,11 @@ export default class Preset {
105114
});
106115

107116
if (exactFoundAlias) {
117+
this.aliasedPathCache[path] = this.alias[exactFoundAlias];
108118
return this.alias[exactFoundAlias];
109119
}
110120

111-
const pathParts = path.split('/'); // eslint-disable-line prefer-const
121+
const pathParts = path.split('/');
112122

113123
// Find matching aliases
114124
const foundAlias = orderBy(aliases, a => -a.split('/').length).find(a => {
@@ -117,10 +127,14 @@ export default class Preset {
117127
});
118128

119129
if (foundAlias) {
130+
const replacedPath = path.replace(foundAlias, this.alias[foundAlias]);
131+
this.aliasedPathCache[path] = replacedPath;
120132
// if an alias is found we will replace the path with the alias
121-
return path.replace(foundAlias, this.alias[foundAlias]);
133+
return replacedPath;
122134
}
123135

136+
this.aliasedPathCache[path] = null;
137+
124138
return path;
125139
}
126140

packages/app/src/sandbox/eval/transpiled-module.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -798,7 +798,9 @@ export default class TranspiledModule {
798798
try {
799799
// eslint-disable-next-line no-inner-declarations
800800
function require(path: string) {
801-
const bfsModule = BrowserFS.BFSRequire(path);
801+
const usedPath = manager.getPresetAliasedPath(path);
802+
803+
const bfsModule = BrowserFS.BFSRequire(usedPath);
802804

803805
if (bfsModule) {
804806
return bfsModule;

0 commit comments

Comments
 (0)