|
1 |
| -import resolve from 'browser-resolve'; |
2 | 1 | import { getGlobal } from '@codesandbox/common/lib/utils/global';
|
| 2 | +import { ModuleNotFoundError } from 'sandpack-core/lib/resolver/errors/ModuleNotFound'; |
| 3 | + |
3 | 4 | import getRequireStatements from './simple-get-require-statements';
|
4 |
| -import { packageFilter } from '../../../utils/resolve-utils'; |
5 | 5 | import { convertEsModule } from '../ast/convert-esmodule';
|
6 | 6 | import { generateCode, parseModule } from '../ast/utils';
|
7 | 7 | import { ChildHandler } from '../../worker-transpiler/child-handler';
|
| 8 | +import { resolve } from './utils/resolve'; |
8 | 9 |
|
9 | 10 | const global = getGlobal();
|
10 | 11 | const path = global.BrowserFS.BFSRequire('path');
|
@@ -110,11 +111,9 @@ function downloadRequires(
|
110 | 111 | }
|
111 | 112 |
|
112 | 113 | try {
|
113 |
| - resolve.sync(foundR.path, { |
| 114 | + resolve(foundR.path, { |
114 | 115 | filename: currentPath,
|
115 | 116 | extensions: ['.js', '.json'],
|
116 |
| - moduleDirectory: ['node_modules'], |
117 |
| - packageFilter, |
118 | 117 | });
|
119 | 118 | } catch (err) {
|
120 | 119 | await downloadFromError({
|
@@ -218,22 +217,34 @@ export async function downloadPath(
|
218 | 217 | return r;
|
219 | 218 | }
|
220 | 219 |
|
| 220 | +function extractPathFromError(err: Error | ModuleNotFoundError): string { |
| 221 | + if (err instanceof ModuleNotFoundError) { |
| 222 | + return err.filepath; |
| 223 | + } |
| 224 | + |
| 225 | + if (err.message.indexOf('Cannot find module') > -1) { |
| 226 | + const dep = err.message.match(/Cannot find module '(.*?)'/)[1]; |
| 227 | + const from = err.message.match(/from '(.*?)'/)[1]; |
| 228 | + const absolutePath = dep.startsWith('.') ? path.join(from, dep) : dep; |
| 229 | + |
| 230 | + return absolutePath; |
| 231 | + } |
| 232 | + |
| 233 | + return null; |
| 234 | +} |
| 235 | + |
221 | 236 | export function downloadFromError(opts: {
|
222 | 237 | error: Error;
|
223 | 238 | childHandler: ChildHandler;
|
224 | 239 | loaderContextId: number;
|
225 | 240 | }) {
|
226 | 241 | const { error, childHandler, loaderContextId } = opts;
|
227 |
| - if (error.message.indexOf('Cannot find module') > -1) { |
228 |
| - const dep = error.message.match(/Cannot find module '(.*?)'/)[1]; |
229 |
| - const from = error.message.match(/from '(.*?)'/)[1]; |
230 |
| - const absolutePath = dep.startsWith('.') ? path.join(from, dep) : dep; |
231 |
| - |
232 |
| - return downloadPath(absolutePath, { |
| 242 | + const moduleSpecifier = extractPathFromError(error); |
| 243 | + if (moduleSpecifier) { |
| 244 | + return downloadPath(moduleSpecifier, { |
233 | 245 | childHandler,
|
234 | 246 | loaderContextId,
|
235 | 247 | });
|
236 | 248 | }
|
237 |
| - |
238 | 249 | return Promise.resolve();
|
239 | 250 | }
|
0 commit comments