mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-19 17:38:19 +08:00
ui: lazyload api
This commit is contained in:
3 files changed
+47
-40
No files matched your search
@@ -16,38 +16,10 @@ export { default as React } from 'react';
|
||||
export { default as ReactDOM } from 'react-dom/client';
|
||||
export * from './misc/Page';
|
||||
export { initPageLoader } from './hydro';
|
||||
export * from './lazyload';
|
||||
import { load } from './lazyload';
|
||||
|
||||
const lazyModules = {};
|
||||
const features = {};
|
||||
export default async function load(name: string) {
|
||||
if (window.node_modules[name]) return window.node_modules[name];
|
||||
if (name === 'echarts') return import('echarts');
|
||||
if (name === 'moment') return import('moment');
|
||||
if (!window.lazyloadMetadata?.[`${name}.lazy.js`]) throw new Error(`Module ${name} not found`);
|
||||
if (lazyModules[name]) return lazyModules[name];
|
||||
const tag = document.createElement('script');
|
||||
tag.src = `/lazy/${window.lazyloadMetadata[`${name}.lazy.js`]}/${name}.lazy.js`;
|
||||
lazyModules[name] = new Promise((resolve, reject) => {
|
||||
tag.onerror = reject;
|
||||
const timeout = setTimeout(reject, 30000);
|
||||
window.lazyModuleResolver[name] = (item) => {
|
||||
clearTimeout(timeout);
|
||||
resolve(item);
|
||||
};
|
||||
});
|
||||
document.body.appendChild(tag);
|
||||
return lazyModules[name];
|
||||
}
|
||||
export async function getFeatures(name: string) {
|
||||
const legacy = Object.keys(window.externalModules).filter((i) => i === name || i.startsWith(`${name}@`));
|
||||
const c = Object.keys(features).filter((i) => i === name || i.startsWith(`${name}@`));
|
||||
return legacy.concat(c);
|
||||
}
|
||||
|
||||
export function provideFeature(name: string, content: string) {
|
||||
features[name] = content;
|
||||
}
|
||||
|
||||
export default load;
|
||||
export interface EventMap { }
|
||||
|
||||
import AutoComplete from './components/autocomplete';
|
||||
@@ -57,7 +29,7 @@ import ProblemSelectAutoComplete from './components/autocomplete/ProblemSelectAu
|
||||
import UserSelectAutoComplete from './components/autocomplete/UserSelectAutoComplete';
|
||||
|
||||
export {
|
||||
load, AutoComplete, UserSelectAutoComplete, ProblemSelectAutoComplete, DomainSelectAutoComplete, CustomSelectAutoComplete,
|
||||
AutoComplete, UserSelectAutoComplete, ProblemSelectAutoComplete, DomainSelectAutoComplete, CustomSelectAutoComplete,
|
||||
};
|
||||
export function addPage(page: import('./misc/Page').Page | (() => Promise<void> | void)) {
|
||||
window.Hydro.extraPages.push(page);
|
||||
@@ -82,7 +54,7 @@ import ReactDOM from 'react-dom/client';
|
||||
import * as redux from 'react-redux';
|
||||
|
||||
const modules = {
|
||||
_, $, React, redux, ReactDOM, load,
|
||||
_, $, React, redux, ReactDOM,
|
||||
};
|
||||
|
||||
declare global {
|
||||
|
||||
Reference in new issue
Block a user