Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from
Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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 信息;
*/
Expand Down
10 changes: 5 additions & 5 deletions packages/utils/src/asset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {};
Expand All @@ -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;
Expand All @@ -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 补充类型
Expand Down
9 changes: 6 additions & 3 deletions packages/utils/src/script.ts
Original file line number Diff line number Diff line change
@@ -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');

Expand All @@ -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();
Expand Down
Loading