diff --git a/src/__tests__/sandbox/index.test.ts b/src/__tests__/sandbox/index.test.ts index deae019ab..142fb0099 100644 --- a/src/__tests__/sandbox/index.test.ts +++ b/src/__tests__/sandbox/index.test.ts @@ -185,7 +185,7 @@ describe('sandbox', () => { expect(sandbox.escapeKeys.size).toBe(3) // @ts-ignore expect(sandbox.active).toBeTruthy() - sandbox.stop() + sandbox.stop(false) // @ts-ignore expect(sandbox.escapeKeys.size).toBe(0) // @ts-ignore @@ -322,8 +322,8 @@ describe('sandbox', () => { proxyWindow.notExecute = 'notExecute-value' expect(proxyWindow.notExecute).toBe('notExecute-value') - sandbox.stop() - sandbox.stop() // 多次执行start无效 + sandbox.stop(false) + sandbox.stop(false) // 多次执行start无效 }) // proxyWindow没有此变量而rawWindow有,则优先使用rawWindow的descriptor @@ -391,6 +391,16 @@ describe('sandbox', () => { expect(proxyWindow.Image).toBe('new-image') }) + test('don\'t clean global vars in umd mode', () => { + const sandbox = new Sandbox('test-set-eval&Image', `http://127.0.0.1:${ports.sandbox}/common/`) + sandbox.start('') + const proxyWindow: any = sandbox.proxyWindow + + proxyWindow.normalProperty1 = 1 + sandbox.stop(true) + expect(proxyWindow.normalProperty1).toBe(1) + }) + // 分支覆盖 proxyWindow getter方法 test('coverage: proxyWindow getter', () => { const sandbox = new Sandbox('test-coverage-proxy-getter', `http://127.0.0.1:${ports.sandbox}/common/`) diff --git a/src/create_app.ts b/src/create_app.ts index 7347cb4f1..470d4d93c 100644 --- a/src/create_app.ts +++ b/src/create_app.ts @@ -296,7 +296,7 @@ export default class CreateApp implements AppInterface { } // this.container maybe contains micro-app element, stop sandbox should exec after cloneContainer - this.sandBox?.stop() + this.sandBox?.stop(this.umdMode) if (!getActiveApps().length) { releasePatchSetAttribute() } diff --git a/src/sandbox/index.ts b/src/sandbox/index.ts index 06ce9f609..40e229401 100644 --- a/src/sandbox/index.ts +++ b/src/sandbox/index.ts @@ -103,22 +103,24 @@ export default class SandBox implements SandBoxInterface { } } - stop (): void { + stop (umdMode: boolean): void { if (this.active) { this.active = false this.releaseEffect() this.microAppWindow.microApp.clearDataListener() this.microAppWindow.microApp.clearGlobalDataListener() - this.injectedKeys.forEach((key: PropertyKey) => { - Reflect.deleteProperty(this.microAppWindow, key) - }) - this.injectedKeys.clear() + if (!umdMode) { + this.injectedKeys.forEach((key: PropertyKey) => { + Reflect.deleteProperty(this.microAppWindow, key) + }) + this.injectedKeys.clear() - this.escapeKeys.forEach((key: PropertyKey) => { - Reflect.deleteProperty(globalEnv.rawWindow, key) - }) - this.escapeKeys.clear() + this.escapeKeys.forEach((key: PropertyKey) => { + Reflect.deleteProperty(globalEnv.rawWindow, key) + }) + this.escapeKeys.clear() + } if (--SandBox.activeCount === 0) { releaseEffectDocumentEvent() diff --git a/typings/global.d.ts b/typings/global.d.ts index 5efcc451d..61e1c5553 100644 --- a/typings/global.d.ts +++ b/typings/global.d.ts @@ -9,7 +9,7 @@ declare module '@micro-app/types' { proxyWindow: WindowProxy microAppWindow: Window // Proxy target start (baseRoute: string): void - stop (): void + stop (umdMode: boolean): void // record umd snapshot before the first execution of umdHookMount recordUmdSnapshot (): void // rebuild umd snapshot before remount umd app