Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions src/proxies/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,16 @@ const WorkerProxy = new Proxy<Worker>(originalWorker, {
}

if (url && !isSameOrigin(url)) {
let workerPath = scriptURL
// 如果 scriptURL 是跨域的,使用 Blob URL 加载并执行 worker
const script = `import "${scriptURL}";`
const workerPath = urlFromScript(script)
options.type = 'module'
// 应该遵总 worker 的 type 类型。classic worker
if (options.type === 'module') {
const script = `import "${scriptURL}";`
workerPath = urlFromScript(script)
} else {
const script = `importScripts("${scriptURL}");`
workerPath = urlFromScript(script)
}
return new Target(workerPath, options) as WorkerInstance
} else {
// 如果 scriptURL 是同源的,直接使用原生的 Worker 构造函数
Expand Down
10 changes: 10 additions & 0 deletions src/sandbox/with/window.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
import {
appInstanceMap,
} from '../../create_app'
import WorkerProxy from '../../proxies/worker'

/**
* patch window of child app
Expand Down Expand Up @@ -87,9 +88,18 @@ function createProxyWindow (
const rawWindow = globalEnv.rawWindow
const descriptorTargetMap = new Map<PropertyKey, 'target' | 'rawWindow'>()

Object.defineProperty(microAppWindow, 'Worker', {
value: WorkerProxy,
configurable: true,
writable: true,
})

const proxyWindow = new Proxy(microAppWindow, {
get: (target: microAppWindowType, key: PropertyKey): unknown => {
throttleDeferForSetAppName(appName)
if (key === 'Worker') {
return WorkerProxy
}
if (
Reflect.has(target, key) ||
(isString(key) && /^__MICRO_APP_/.test(key)) ||
Expand Down