feat(utils|types): add esmodule support for component meta resources (#2603)

* feat(utils): support script type param for assest loader

* feat(types): update AssestsJson type
This commit is contained in:
keuby 2023-11-07 17:20:13 +08:00 committed by GitHub
parent 97eb477746
commit e3611dcf06
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 16 additions and 9 deletions

View File

@ -1,17 +1,21 @@
import { Asset } from '../../assets';
import { IPublicTypeComponentMetadata, IPublicTypeReference } from './';
/**
*
*/
export interface IPublicTypeRemoteComponentDescription extends IPublicTypeComponentMetadata {
/**
* window[exportName] Object
*/
exportName?: string;
/**
*
*/
url?: string;
url?: Asset;
/**
* () npm
*/

View File

@ -214,6 +214,8 @@ function parseAsset(scripts: any, styles: any, asset: Asset | undefined | null,
}
export class AssetLoader {
private stylePoints = new Map<string, StylePoint>();
async load(asset: Asset) {
const styles: any = {};
const scripts: any = {};
@ -237,11 +239,9 @@ export class AssetLoader {
await Promise.all(
styleQueue.map(({ content, level, type, id }) => this.loadStyle(content, level!, type === AssetType.CSSUrl, id)),
);
await Promise.all(scriptQueue.map(({ content, type }) => this.loadScript(content, type === AssetType.JSUrl)));
await Promise.all(scriptQueue.map(({ content, type, scriptType }) => this.loadScript(content, type === AssetType.JSUrl, scriptType)));
}
private stylePoints = new Map<string, StylePoint>();
private loadStyle(content: string | undefined | null, level: AssetLevel, isUrl?: boolean, id?: string) {
if (!content) {
return;
@ -259,11 +259,11 @@ export class AssetLoader {
return isUrl ? point.applyUrl(content) : point.applyText(content);
}
private loadScript(content: string | undefined | null, isUrl?: boolean) {
private loadScript(content: string | undefined | null, isUrl?: boolean, scriptType?: string) {
if (!content) {
return;
}
return isUrl ? load(content) : evaluate(content);
return isUrl ? load(content, scriptType) : evaluate(content, scriptType);
}
// todo 补充类型

View File

@ -1,14 +1,15 @@
import { createDefer } from './create-defer';
export function evaluate(script: string) {
export function evaluate(script: string, scriptType?: string) {
const scriptEl = document.createElement('script');
scriptType && (scriptEl.type = scriptType);
scriptEl.text = script;
document.head.appendChild(scriptEl);
document.head.removeChild(scriptEl);
}
export function load(url: string) {
const node: any = document.createElement('script');
export function load(url: string, scriptType?: string) {
const node = document.createElement('script');
// node.setAttribute('crossorigin', 'anonymous');
@ -34,6 +35,8 @@ export function load(url: string) {
// `async=false` is required to make sure all js resources execute sequentially.
node.async = false;
scriptType && (node.type = scriptType);
document.head.appendChild(node);
return i.promise();