Skip to content

Commit f7e4efe

Browse files
authored
Don't reuse cache of a module if there are new globals (codesandbox#3897)
* Don't reuse cache of a module if there are new globals * Make param optional
1 parent dc2c9e8 commit f7e4efe

File tree

3 files changed

+46
-12
lines changed

3 files changed

+46
-12
lines changed

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ export default class Manager {
326326

327327
evaluateModule(
328328
module: Module,
329-
{ force = false, globals = {} }: { force?: boolean; globals?: object } = {}
329+
{ force = false, globals }: { force?: boolean; globals?: object } = {}
330330
) {
331331
if (this.hardReload && !this.isFirstLoad) {
332332
// Do a hard reload
@@ -364,7 +364,7 @@ export default class Manager {
364364
evaluateTranspiledModule(
365365
transpiledModule: TranspiledModule,
366366
initiator?: TranspiledModule,
367-
{ force = false, globals = {} }: { force?: boolean; globals?: object } = {}
367+
{ force = false, globals }: { force?: boolean; globals?: object } = {}
368368
) {
369369
if (force && transpiledModule.compilation) {
370370
transpiledModule.compilation = null;

packages/app/src/sandbox/eval/tests/jest-lite.ts

+18-1
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ export default class TestRunner {
144144
test.skip = skip;
145145

146146
const it = test;
147-
const { window: jsdomWindow } = this.dom;
147+
const jsdomWindow = this.dom.window.document.defaultView;
148148
const { document: jsdomDocument } = jsdomWindow;
149149

150150
// Set the modules that are not set on JSDOM
@@ -258,6 +258,20 @@ export default class TestRunner {
258258
});
259259
}
260260

261+
oldEnvVars: { [key: string]: string };
262+
async setup() {
263+
this.oldEnvVars = { ...this.manager.envVariables };
264+
this.manager.envVariables.NODE_ENV = 'test';
265+
}
266+
267+
async teardown() {
268+
const global = this.dom.window.document.defaultView;
269+
global.close();
270+
Object.defineProperty(global, 'document', { value: null });
271+
this.dom = null;
272+
this.manager.envVariables = this.oldEnvVars;
273+
}
274+
261275
/* istanbul ignore next */
262276
async runTests(force: boolean = false) {
263277
if (!this.watching && !force) {
@@ -313,6 +327,8 @@ export default class TestRunner {
313327

314328
resetTestState();
315329

330+
await this.setup();
331+
316332
await Promise.all(
317333
tests.map(async t => {
318334
dispatch(actions.error.clear(t.path, 'jest'));
@@ -341,6 +357,7 @@ export default class TestRunner {
341357
);
342358

343359
await run();
360+
await this.teardown();
344361

345362
setTimeout(() => {
346363
this.sendMessage(messages.TOTAL_TEST_END);

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

+26-9
Original file line numberDiff line numberDiff line change
@@ -140,9 +140,10 @@ export type LoaderContext = {
140140
};
141141
/* eslint-enable */
142142

143-
type Compilation = {
143+
export type Compilation = {
144144
id: string;
145145
exports: any;
146+
globals: object | undefined;
146147
hot: {
147148
accept: (() => void) | ((arg: string | string[], cb: () => void) => void);
148149
decline: (path: string | Array<string>) => void;
@@ -770,12 +771,24 @@ export default class TranspiledModule {
770771
}
771772
};
772773

774+
isCompilationCached = (globals?: object) => {
775+
if (!this.compilation || !this.compilation.exports) {
776+
return false;
777+
}
778+
779+
if (this.compilation.globals === globals) {
780+
return true;
781+
}
782+
783+
return false;
784+
};
785+
773786
evaluate(
774787
manager: Manager,
775788
{
776789
asUMD = false,
777790
force = false,
778-
globals = {},
791+
globals,
779792
}: { asUMD?: boolean; force?: boolean; globals?: any } = {},
780793
initiator?: TranspiledModule
781794
) {
@@ -845,7 +858,7 @@ export default class TranspiledModule {
845858
) {
846859
return this.compilation.exports;
847860
}
848-
} else if (this.compilation && this.compilation.exports && !this.isEntry) {
861+
} else if (this.isCompilationCached(globals) && !this.isEntry) {
849862
return this.compilation.exports;
850863
}
851864

@@ -864,7 +877,8 @@ export default class TranspiledModule {
864877
this.compilation = this.compilation || {
865878
id: this.getId(),
866879
exports: {},
867-
hot: {
880+
globals,
881+
hot: manager.envVariables.NODE_ENV !== 'test' && {
868882
accept: (path: string | Array<string>, cb) => {
869883
if (
870884
typeof path === 'undefined' ||
@@ -926,7 +940,9 @@ export default class TranspiledModule {
926940
removeStatusHandler: manager.removeStatusHandler,
927941
},
928942
};
929-
this.compilation.hot.data = hotData;
943+
if (this.compilation.hot) {
944+
this.compilation.hot.data = hotData;
945+
}
930946

931947
const transpiledModule = this;
932948

@@ -985,7 +1001,7 @@ export default class TranspiledModule {
9851001
// of that compilation
9861002
const cache = requiredTranspiledModule.compilation;
9871003

988-
return cache
1004+
return requiredTranspiledModule.isCompilationCached(globals)
9891005
? cache.exports
9901006
: manager.evaluateTranspiledModule(
9911007
requiredTranspiledModule,
@@ -1001,15 +1017,16 @@ export default class TranspiledModule {
10011017
return foundModule.path;
10021018
};
10031019

1004-
globals.__dirname = pathUtils.dirname(this.module.path);
1005-
globals.__filename = this.module.path;
1020+
const usedGlobals = globals || {};
1021+
usedGlobals.__dirname = pathUtils.dirname(this.module.path);
1022+
usedGlobals.__filename = this.module.path;
10061023

10071024
const exports = evaluate(
10081025
this.source.compiledCode,
10091026
require,
10101027
this.compilation,
10111028
manager.envVariables,
1012-
globals,
1029+
usedGlobals,
10131030
{ asUMD }
10141031
);
10151032

0 commit comments

Comments
 (0)