Skip to content

Commit 1e15fea

Browse files
committed
fix: use custom set in transpiled module to bypass polyfill of Array,from
1 parent 52d0d01 commit 1e15fea

File tree

3 files changed

+79
-37
lines changed

3 files changed

+79
-37
lines changed

packages/sandpack-core/src/manager.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -626,11 +626,13 @@ export default class Manager implements IEvaluator {
626626
}
627627

628628
verifyTreeTranspiled() {
629-
return Promise.all(
630-
this.getTranspiledModules()
631-
.filter(tModule => tModule.shouldTranspile())
632-
.map(tModule => tModule.transpile(this))
633-
);
629+
const promises = [];
630+
for (const tModule of this.getTranspiledModules()) {
631+
if (tModule.shouldTranspile()) {
632+
promises.push(tModule.transpile(this));
633+
}
634+
}
635+
return Promise.all(promises);
634636
}
635637

636638
clearCompiledCache() {
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// This is a hack to work around some polyfills
2+
export class CustomSet<T> {
3+
private internalSet: Set<T>;
4+
5+
constructor() {
6+
this.internalSet = new Set();
7+
}
8+
9+
size(): number {
10+
return this.internalSet.size;
11+
}
12+
13+
clear() {
14+
this.internalSet.clear();
15+
}
16+
17+
add(val: T): void {
18+
this.internalSet.add(val);
19+
}
20+
21+
delete(val: T): void {
22+
this.internalSet.delete(val);
23+
}
24+
25+
values(): Array<T> {
26+
const result = [];
27+
for (const val of this.internalSet) {
28+
result.push(val);
29+
}
30+
return result;
31+
}
32+
}

packages/sandpack-core/src/transpiled-module/transpiled-module.ts

Lines changed: 40 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import HMR from './hmr';
2323
import { splitQueryFromPath } from './utils/query-path';
2424
import { getModuleUrl } from './module-url';
2525
import delay from '../utils/delay';
26+
import { CustomSet } from './CustomSet';
2627

2728
declare const BrowserFS: any;
2829

@@ -163,11 +164,11 @@ export class TranspiledModule {
163164
errors: Array<ModuleError>;
164165
warnings: Array<ModuleWarning>;
165166
compilation: Compilation | null = null;
166-
initiators: Set<TranspiledModule>; // eslint-disable-line no-use-before-define
167-
dependencies: Set<TranspiledModule>; // eslint-disable-line no-use-before-define
167+
initiators: CustomSet<TranspiledModule>; // eslint-disable-line no-use-before-define
168+
dependencies: CustomSet<TranspiledModule>; // eslint-disable-line no-use-before-define
168169
asyncDependencies: Array<Promise<TranspiledModule>>; // eslint-disable-line no-use-before-define
169-
transpilationDependencies: Set<TranspiledModule>;
170-
transpilationInitiators: Set<TranspiledModule>;
170+
transpilationDependencies: CustomSet<TranspiledModule>;
171+
transpilationInitiators: CustomSet<TranspiledModule>;
171172

172173
// Unique identifier
173174
hash: string;
@@ -196,11 +197,11 @@ export class TranspiledModule {
196197
this.errors = [];
197198
this.warnings = [];
198199
this.childModules = [];
199-
this.transpilationDependencies = new Set();
200-
this.dependencies = new Set();
200+
this.transpilationDependencies = new CustomSet();
201+
this.dependencies = new CustomSet();
201202
this.asyncDependencies = [];
202-
this.transpilationInitiators = new Set();
203-
this.initiators = new Set();
203+
this.transpilationInitiators = new CustomSet();
204+
this.initiators = new CustomSet();
204205
this.isEntry = false;
205206
this.isTestFile = false;
206207

@@ -220,7 +221,7 @@ export class TranspiledModule {
220221
this.reset();
221222

222223
// Reset parents
223-
this.initiators.forEach(tModule => {
224+
this.initiators.values().forEach(tModule => {
224225
tModule.resetTranspilation();
225226
});
226227

@@ -251,7 +252,8 @@ export class TranspiledModule {
251252
}
252253

253254
resetTranspilation() {
254-
Array.from(this.transpilationInitiators)
255+
this.transpilationInitiators
256+
.values()
255257
.filter(t => t.source)
256258
.forEach(dep => {
257259
dep.resetTranspilation();
@@ -263,7 +265,7 @@ export class TranspiledModule {
263265
this.errors = [];
264266
this.warnings = [];
265267

266-
Array.from(this.dependencies).forEach(t => {
268+
this.dependencies.values().forEach(t => {
267269
t.initiators.delete(this);
268270
});
269271
// Don't do it for transpilation dependencies, since those cannot be traced back since we also reset transpilation of them.
@@ -281,13 +283,15 @@ export class TranspiledModule {
281283
if (this.hmrConfig && this.hmrConfig.isHot()) {
282284
this.hmrConfig.setDirty(true);
283285
} else {
284-
Array.from(this.initiators)
286+
this.initiators
287+
.values()
285288
.filter(t => t.compilation)
286289
.forEach(initiator => {
287290
initiator.resetCompilation();
288291
});
289292

290-
Array.from(this.transpilationInitiators)
293+
this.transpilationInitiators
294+
.values()
291295
.filter(t => t.compilation)
292296
.forEach(dep => {
293297
dep.resetCompilation();
@@ -296,7 +300,8 @@ export class TranspiledModule {
296300
// If this is an entry we want all direct entries to be reset as well.
297301
// Entries generally have side effects
298302
if (this.isEntry) {
299-
Array.from(this.dependencies)
303+
this.dependencies
304+
.values()
300305
.filter(t => t.compilation && t.isEntry)
301306
.forEach(dep => {
302307
dep.resetCompilation();
@@ -313,7 +318,7 @@ export class TranspiledModule {
313318
return (
314319
!this.source &&
315320
!this.isTestFile &&
316-
!(this.initiators.size === 0 && this.transpilationInitiators.size > 0)
321+
!(this.initiators.size() === 0 && this.transpilationInitiators.size() > 0)
317322
);
318323
}
319324

@@ -561,15 +566,16 @@ export class TranspiledModule {
561566

562567
// Remove this module from the initiators of old deps, so we can populate a
563568
// fresh cache
564-
this.dependencies.forEach(tModule => {
569+
this.dependencies.values().forEach(tModule => {
565570
tModule.initiators.delete(this);
566571
});
567-
this.transpilationDependencies.forEach(tModule => {
572+
this.transpilationDependencies.values().forEach(tModule => {
568573
tModule.transpilationInitiators.delete(this);
569574
});
570575
this.childModules.forEach(tModule => {
571576
tModule.dispose(manager);
572577
});
578+
573579
this.dependencies.clear();
574580
this.transpilationDependencies.clear();
575581
this.childModules.length = 0;
@@ -691,10 +697,8 @@ export class TranspiledModule {
691697
this.asyncDependencies = [];
692698

693699
await Promise.all([
694-
...Array.from(this.transpilationInitiators).map(t =>
695-
t.transpile(manager)
696-
),
697-
...Array.from(this.dependencies).map(t => t.transpile(manager)),
700+
...this.transpilationInitiators.values().map(t => t.transpile(manager)),
701+
...this.dependencies.values().map(t => t.transpile(manager)),
698702
]);
699703

700704
return this;
@@ -940,7 +944,8 @@ export class TranspiledModule {
940944
this.hmrConfig = this.hmrConfig || new HMR();
941945

942946
// We have to bubble up, so reset compilation of parents
943-
Array.from(this.initiators)
947+
this.initiators
948+
.values()
944949
.filter(t => t.compilation)
945950
.forEach(dep => {
946951
dep.resetCompilation();
@@ -1086,8 +1091,8 @@ export class TranspiledModule {
10861091

10871092
postTranspile(manager: Manager) {
10881093
if (
1089-
this.initiators.size === 0 &&
1090-
this.transpilationInitiators.size === 0 &&
1094+
this.initiators.size() === 0 &&
1095+
this.transpilationInitiators.size() === 0 &&
10911096
!this.isEntry &&
10921097
!manager.isFirstLoad &&
10931098
// Don't delete stubbed modules, they are here for a reason, most probably
@@ -1121,6 +1126,7 @@ export class TranspiledModule {
11211126
const sourceEqualsCompiled = Boolean(
11221127
this.source && this.source.sourceEqualsCompiled
11231128
);
1129+
11241130
const serializableObject: SerializedTranspiledModule = {
11251131
query: this.query,
11261132
module: this.module,
@@ -1129,14 +1135,16 @@ export class TranspiledModule {
11291135

11301136
sourceEqualsCompiled,
11311137
childModules: this.childModules.map(m => m.getId()),
1132-
dependencies: Array.from(this.dependencies).map(m => m.getId()),
1133-
initiators: Array.from(this.initiators).map(m => m.getId()),
1134-
transpilationDependencies: Array.from(
1135-
this.transpilationDependencies
1136-
).map(m => m.getId()),
1137-
transpilationInitiators: Array.from(this.transpilationInitiators).map(m =>
1138-
m.getId()
1139-
),
1138+
dependencies: this.dependencies.values().map(m => {
1139+
return m.getId();
1140+
}),
1141+
initiators: this.initiators.values().map(m => m.getId()),
1142+
transpilationDependencies: this.transpilationDependencies
1143+
.values()
1144+
.map(m => m.getId()),
1145+
transpilationInitiators: this.transpilationInitiators
1146+
.values()
1147+
.map(m => m.getId()),
11401148
asyncDependencies: await Promise.all(
11411149
Array.from(this.asyncDependencies).map(m => m.then(x => x.getId()))
11421150
),

0 commit comments

Comments
 (0)