Skip to content

Commit bebf5e1

Browse files
CompuIvesSaraVieira
authored andcommitted
Implement improvements from codesandbox#1599 (codesandbox#2173)
1 parent d534c02 commit bebf5e1

File tree

2 files changed

+33
-25
lines changed

2 files changed

+33
-25
lines changed

packages/app/src/sandbox/eval/npm/fetch-npm-module.ts

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,31 @@ export let combinedMetas: Meta = {};
3131
const normalizedMetas: { [key: string]: Meta } = {};
3232
const packages: Packages = {};
3333

34+
async function fetchWithRetries(url: string, retries = 3): Promise<string> {
35+
const doFetch = () =>
36+
window.fetch(url).then(x => {
37+
if (x.ok) {
38+
return x.text();
39+
}
40+
41+
throw new Error(`Could not fetch ${url}`);
42+
});
43+
44+
for (let i = 0; i < retries; i++) {
45+
try {
46+
// eslint-disable-next-line
47+
return await doFetch();
48+
} catch (e) {
49+
console.error(e);
50+
if (i === retries - 1) {
51+
throw e;
52+
}
53+
}
54+
}
55+
56+
throw new Error('Could not fetch');
57+
}
58+
3459
export function setCombinedMetas(givenCombinedMetas: Meta) {
3560
combinedMetas = givenCombinedMetas;
3661
}
@@ -77,8 +102,8 @@ function getUnpkgUrl(name: string, version: string, forceJsDelivr?: boolean) {
77102
const nameWithoutAlias = name.replace(ALIAS_REGEX, '');
78103

79104
return TEMP_USE_JSDELIVR || forceJsDelivr
80-
? `https://unpkg.com/${nameWithoutAlias}@${version}`
81-
: `https://cdn.jsdelivr.net/npm/${nameWithoutAlias}@${version}`;
105+
? `https://cdn.jsdelivr.net/npm/${nameWithoutAlias}@${version}`
106+
: `https://unpkg.com/${nameWithoutAlias}@${version}`;
82107
}
83108

84109
function getMeta(name: string, packageJSONPath: string, version: string) {
@@ -122,27 +147,13 @@ function downloadDependency(
122147
? `https://cdn.jsdelivr.net/gh/${depVersion}${relativePath}`
123148
: `${getUnpkgUrl(depName, depVersion)}${relativePath}`;
124149

125-
packages[path] = window
126-
.fetch(url)
127-
.then(x => {
128-
if (x.ok) {
129-
return x.text();
130-
}
131-
132-
throw new Error(`Could not find module ${path}`);
133-
})
150+
packages[path] = fetchWithRetries(url)
134151
.catch(err => {
135152
if (!isGitHub) {
136153
// Fallback to jsdelivr
137-
return fetch(
154+
return fetchWithRetries(
138155
`${getUnpkgUrl(depName, depVersion, true)}${relativePath}`
139-
).then(x2 => {
140-
if (x2.ok) {
141-
return x2.text();
142-
}
143-
144-
throw new Error(`Could not find module ${path}`);
145-
});
156+
);
146157
}
147158

148159
throw err;

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

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,7 @@ import { SourceMap } from './transpilers/utils/get-source-map';
1212
import ModuleError from './errors/module-error';
1313
import ModuleWarning from './errors/module-warning';
1414

15-
import {
16-
WarningStructure,
17-
buildWorkerWarning,
18-
} from './transpilers/utils/worker-warning-handler';
15+
import { WarningStructure } from './transpilers/utils/worker-warning-handler';
1916

2017
import resolveDependency from './loaders/dependency-resolver';
2118
import evaluate from './loaders/eval';
@@ -618,7 +615,6 @@ export default class TranspiledModule {
618615
} else {
619616
const transpilers = manager.preset.getLoaders(this.module, this.query);
620617

621-
const t = Date.now();
622618
for (let i = 0; i < transpilers.length; i += 1) {
623619
const transpilerConfig = transpilers[i];
624620
const loaderContext = this.getLoaderContext(
@@ -632,10 +628,12 @@ export default class TranspiledModule {
632628
.join('!');
633629

634630
try {
631+
const startTime = Date.now();
635632
const {
636633
transpiledCode,
637634
sourceMap,
638635
} = await transpilerConfig.transpiler.transpile(code, loaderContext); // eslint-disable-line no-await-in-loop
636+
debug(`Transpiled '${this.getId()}' in ${Date.now() - startTime}ms`);
639637

640638
if (this.errors.length) {
641639
throw this.errors[0];
@@ -655,7 +653,6 @@ export default class TranspiledModule {
655653

656654
throw e;
657655
}
658-
debug(`Transpiled '${this.getId()}' in ${Date.now() - t}ms`);
659656
}
660657

661658
this.logWarnings();

0 commit comments

Comments
 (0)