Skip to content

Commit c3d3f31

Browse files
committed
Fix dynamically downloading modules
1 parent d95b325 commit c3d3f31

File tree

3 files changed

+36
-27
lines changed

3 files changed

+36
-27
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -831,12 +831,12 @@ export default class Manager {
831831
const tModule =
832832
currentTModule || this.getTranspiledModule(this.modules['/package.json']); // Get arbitrary file from root
833833
try {
834-
return this.resolveTranspiledModule(
834+
const resolvedTModule = await this.resolveTranspiledModule(
835835
path,
836836
tModule.module.path,
837-
ignoredExtensions,
838-
true
837+
ignoredExtensions
839838
);
839+
return resolvedTModule;
840840
} catch (e) {
841841
if (e.type === 'module-not-found' && e.isDependency) {
842842
const { queryPath } = splitQueryFromPath(path);

packages/app/src/sandbox/eval/transpilers/babel/worker/babel-worker.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,8 @@ async function installPlugin(Babel, BFSRequire, plugin, currentPath, isV7) {
175175

176176
const pluginName = await resolveDependencyName(plugin, isV7);
177177

178+
await downloadPath(`/node_modules/${pluginName}`);
179+
178180
try {
179181
await downloadPath(pluginName);
180182
evaluatedPlugin = evaluateFromPath(

packages/app/src/sandbox/eval/transpilers/babel/worker/dynamic-download.ts

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,32 @@ export const resolveAsyncModule = (
9696
return downloadCache.get(modulePath);
9797
};
9898

99+
function downloadRequires(currentPath: string, code: string) {
100+
const requires = getRequireStatements(code);
101+
102+
// Download all other needed files
103+
return Promise.all(
104+
requires.map(async foundR => {
105+
if (foundR.type === 'direct') {
106+
if (foundR.path === 'babel-plugin-macros') {
107+
return;
108+
}
109+
110+
try {
111+
resolve.sync(foundR.path, {
112+
filename: currentPath,
113+
extensions: ['.js', '.json'],
114+
moduleDirectory: ['node_modules'],
115+
packageFilter,
116+
});
117+
} catch (e) {
118+
await downloadFromError(e);
119+
}
120+
}
121+
})
122+
);
123+
}
124+
99125
export async function downloadPath(
100126
absolutePath: string
101127
): Promise<{ code: string; path: string }> {
@@ -137,38 +163,19 @@ export async function downloadPath(
137163
/* ignore */
138164
}
139165

166+
const code = existingFile.toString();
167+
await downloadRequires(r.path, code);
168+
140169
return {
141-
code: existingFile,
170+
code,
142171
path: r.path,
143172
};
144173
}
145174

146175
mkDirByPathSync(path.dirname(r.path));
147176
fs.writeFileSync(r.path, r.code);
148177

149-
const requires = getRequireStatements(r.code);
150-
151-
// Download all other needed files
152-
await Promise.all(
153-
requires.map(async foundR => {
154-
if (foundR.type === 'direct') {
155-
if (foundR.path === 'babel-plugin-macros') {
156-
return;
157-
}
158-
159-
try {
160-
resolve.sync(foundR.path, {
161-
filename: r.path,
162-
extensions: ['.js', '.json'],
163-
moduleDirectory: ['node_modules'],
164-
packageFilter,
165-
});
166-
} catch (e) {
167-
await downloadFromError(e);
168-
}
169-
}
170-
})
171-
);
178+
await downloadRequires(r.path, r.code);
172179

173180
return r;
174181
}

0 commit comments

Comments
 (0)