// Copyright 2024 The Lynx Authors. All rights reserved. // Licensed under the Apache License Version 2.0 that can be found in the // LICENSE file in the root directory of this source tree. import { loadLazyBundle } from './lazy-bundle.js'; export function loadDynamicJS(url: string): Promise { if (__LEPUS__) { _ReportError( new Error(`A dynamic import (to "${url}") is leaked to Lepus bundle.`), { errorCode: 202 }, ); // eslint-disable-next-line @typescript-eslint/prefer-promise-reject-errors return Promise.reject(); } return new Promise((resolve, reject) => { lynx.requireModuleAsync(url, (err, data) => { if (err) { reject(err); } else { resolve(data as T); } }); }); } export function __dynamicImport( url: string, options?: { with?: { type?: 'component' | 'tsx' | 'jsx'; mode?: 'sync' | 'async' } }, ): Promise { const w = options?.with; const t = w?.type; if (t === 'component' || t === 'tsx' || t === 'jsx') { return loadLazyBundle(url, w?.mode); } else { return loadDynamicJS(url); } }