Skip to content

Commit

Permalink
fix: don\'t clean global vars in umd mode
Browse files Browse the repository at this point in the history
  • Loading branch information
knight.chen committed Aug 15, 2022
1 parent 45d8c3d commit 8dff3e1
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 14 deletions.
16 changes: 13 additions & 3 deletions src/__tests__/sandbox/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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/`)
Expand Down
2 changes: 1 addition & 1 deletion src/create_app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down
20 changes: 11 additions & 9 deletions src/sandbox/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion typings/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 8dff3e1

Please sign in to comment.