|
| 1 | +import JSZip from "jszip"; |
| 2 | +import {base64ToBlob} from "@/utils/base64"; |
| 3 | + |
| 4 | +export const loadRemoteComponent = (scope: string, module: string) => { |
| 5 | + return new Promise(async (resolve, reject) => { |
| 6 | + try { |
| 7 | + // Initialize the sharing scope (shared modules like react, etc.) |
| 8 | + //@ts-ignore |
| 9 | + await __webpack_init_sharing__('default'); |
| 10 | + //@ts-ignore |
| 11 | + const container = window[scope]; // Get the container loaded on the window object |
| 12 | + if (!container) { |
| 13 | + reject(new Error(`Remote scope ${scope} not found on window.`)); |
| 14 | + return; |
| 15 | + } |
| 16 | + |
| 17 | + //@ts-ignore |
| 18 | + await container.init(__webpack_share_scopes__.default); // Initialize the container |
| 19 | + const factory = await container.get(module); // Get the module factory |
| 20 | + resolve(factory()); // Get the actual module |
| 21 | + } catch (e) { |
| 22 | + reject(e); |
| 23 | + } |
| 24 | + }); |
| 25 | +}; |
| 26 | + |
| 27 | +export const loadRemoteScript = (url: string): Promise<void> => { |
| 28 | + return new Promise((resolve, reject) => { |
| 29 | + try { |
| 30 | + const script = document.createElement('script'); |
| 31 | + script.src = url; |
| 32 | + script.onload = (e) => { |
| 33 | + resolve(); |
| 34 | + }; |
| 35 | + script.onerror = reject; |
| 36 | + document.head.appendChild(script); |
| 37 | + } catch (e) { |
| 38 | + reject(e); |
| 39 | + } |
| 40 | + }); |
| 41 | +}; |
| 42 | + |
| 43 | + |
| 44 | +export const loadFileScript = (content: string): Promise<void> => { |
| 45 | + return new Promise((resolve, reject) => { |
| 46 | + try { |
| 47 | + try { |
| 48 | + eval(content); |
| 49 | + } catch (e) { |
| 50 | + resolve(); |
| 51 | + return; |
| 52 | + } |
| 53 | + const encoder = new TextEncoder(); |
| 54 | + const encodedContent = encoder.encode(content); |
| 55 | + const blob = new Blob([encodedContent], {type: 'application/javascript'}); |
| 56 | + const url = URL.createObjectURL(blob); |
| 57 | + const script = document.createElement('script'); |
| 58 | + script.src = url; |
| 59 | + script.onload = () => { |
| 60 | + resolve(); |
| 61 | + }; |
| 62 | + script.onerror = (e) => { |
| 63 | + reject(e); |
| 64 | + }; |
| 65 | + document.head.appendChild(script); |
| 66 | + } catch (e) { |
| 67 | + reject(e); |
| 68 | + } |
| 69 | + }); |
| 70 | +}; |
| 71 | + |
| 72 | + |
| 73 | +export const loadZipJsFileScript = async (base64: string): Promise<void> => { |
| 74 | + return new Promise((resolve, reject) => { |
| 75 | + const file = base64ToBlob(base64, 'application/zip'); |
| 76 | + if (file) { |
| 77 | + const zip = new JSZip(); |
| 78 | + const content = file.arrayBuffer(); |
| 79 | + zip.loadAsync(content).then((unzipped:any) => { |
| 80 | + const jsFiles: { relativePath: string, content: string }[] = []; |
| 81 | + |
| 82 | + const filePromises: Promise<void>[] = []; |
| 83 | + unzipped.forEach((relativePath:any, file:any) => { |
| 84 | + if (relativePath.endsWith(".js")) { |
| 85 | + const filePromise = file.async('text').then((text:any) => { |
| 86 | + jsFiles.push({relativePath, content: text}); |
| 87 | + }); |
| 88 | + filePromises.push(filePromise); |
| 89 | + } |
| 90 | + }); |
| 91 | + |
| 92 | + Promise.all(filePromises).then(() => { |
| 93 | + jsFiles.reduce((prevPromise: any, jsFile) => { |
| 94 | + return prevPromise.then(() => { |
| 95 | + return loadFileScript(jsFile.content).then(() => { |
| 96 | + console.log('Load success file:', jsFile.relativePath); |
| 97 | + }); |
| 98 | + }); |
| 99 | + }, Promise.resolve()).then(() => { |
| 100 | + resolve(); |
| 101 | + }).catch(reject); |
| 102 | + }).catch(reject); |
| 103 | + }).catch(reject); |
| 104 | + } else { |
| 105 | + reject(new Error('Failed to convert base64 to Blob.')); |
| 106 | + } |
| 107 | + }); |
| 108 | +}; |
0 commit comments