From e3bafd8af031a012059c31a11ad1c9b064029d50 Mon Sep 17 00:00:00 2001 From: Treecat Date: Wed, 19 Apr 2023 09:54:07 +0800 Subject: [PATCH] KERNEL-14316 fix:worker polyfill --- .../platform/web/jquery/jquery.polyfill.js | 155 ++++++++++-------- packages/fineui/webpack/webpack.prod.js | 1 + 2 files changed, 86 insertions(+), 70 deletions(-) diff --git a/packages/fineui/src/core/platform/web/jquery/jquery.polyfill.js b/packages/fineui/src/core/platform/web/jquery/jquery.polyfill.js index 7564b7855..ba2a8d60b 100644 --- a/packages/fineui/src/core/platform/web/jquery/jquery.polyfill.js +++ b/packages/fineui/src/core/platform/web/jquery/jquery.polyfill.js @@ -2,80 +2,95 @@ * 用于 jquery 在 worker 环境或者 V8 引擎的 polyfill */ import { _global } from "@/core/0.foundation"; -if (!_global.window) { - _global.window = _global; - const document = (_global.document = {}); - const fakeElement = Object.create(document); - Object.assign(document, { - parentNode: null, - nodeType: 9, - head: fakeElement, - body: fakeElement, - ownerDocument: document, - documentElement: document, - toString() { - return "FakeDocument"; - }, - appendChild(child) { - return child; - }, - implementation: { - createHTMLDocument() { - return { - body: { - childNodes: [], - }, - }; - }, - createDocumentFragment() { - return this; - }, - }, - getElementById() { - return fakeElement; - }, - createElement() { - return fakeElement; +// const _global = globalThis; + +const document = {}; +const fakeElement = Object.create(document); + +Object.assign(document, { + parentNode: null, + nodeType: 9, + head: fakeElement, + body: fakeElement, + ownerDocument: document, + documentElement: document, + toString() { + return "FakeDocument"; + }, + appendChild(child) { + return child; + }, + implementation: { + createHTMLDocument() { + return { + body: { + childNodes: [], + }, + }; }, createDocumentFragment() { return this; }, - cloneNode() { - return this; - }, - getElementsByTagName() { - return [fakeElement]; - }, - getElementsByClassName() { - return [fakeElement]; - }, - setAttribute() { - return null; - }, - getAttribute() { - return null; - }, - removeChild() { - return null; - }, - addEventListener() { - return null; - }, - removeEventListener() { - return null; - }, - }); + }, + getElementById() { + return fakeElement; + }, + createElement() { + return fakeElement; + }, + createDocumentFragment() { + return this; + }, + cloneNode() { + return this; + }, + getElementsByTagName() { + return [fakeElement]; + }, + getElementsByClassName() { + return [fakeElement]; + }, + setAttribute() { + return null; + }, + getAttribute() { + return null; + }, + removeChild() { + return null; + }, + addEventListener() { + return null; + }, + removeEventListener() { + return null; + }, +}); - Object.assign(fakeElement, { - nodeType: 1, - style: {}, - ownerDocument: document, - parentNod: fakeElement, - firstChild: fakeElement, - lastChild: fakeElement, - toString() { - return "FakeElement"; - }, - }); +Object.assign(fakeElement, { + nodeType: 1, + style: {}, + ownerDocument: document, + parentNod: fakeElement, + firstChild: fakeElement, + lastChild: fakeElement, + toString() { + return "FakeElement"; + }, +}); + +function polyfillProps(key, obj) { + _global[key] = _global[key] || obj; + if (typeof obj === "object") { + try { + Object.assign(_global[key], obj); + } catch(err) { + // _global[key] 可能只有 get 方法 + } + } } + +_global.window = _global; +polyfillProps("XMLHttpRequest", function () {}); +polyfillProps("document", document); diff --git a/packages/fineui/webpack/webpack.prod.js b/packages/fineui/webpack/webpack.prod.js index 991d535f2..36aa0544a 100644 --- a/packages/fineui/webpack/webpack.prod.js +++ b/packages/fineui/webpack/webpack.prod.js @@ -19,6 +19,7 @@ module.exports = merge(common, { entry: { "fineui.min": attachments.fineui, "fineui.worker.min": attachments.fineuiForWorker, + "fineui.worker": attachments.fineuiForWorker }, output: {