Skip to content

Commit e3611dc

Browse files
author
keuby
authored
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
1 parent 97eb477 commit e3611dc

3 files changed

Lines changed: 16 additions & 9 deletions

File tree

packages/types/src/shell/type/remote-component-description.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
1+
import { Asset } from '../../assets';
12
import { IPublicTypeComponentMetadata, IPublicTypeReference } from './';
23

34
/**
45
* 远程物料描述
56
*/
67
export interface IPublicTypeRemoteComponentDescription extends IPublicTypeComponentMetadata {
8+
79
/**
810
* 组件描述导出名字,可以通过 window[exportName] 获取到组件描述的 Object 内容;
911
*/
1012
exportName?: string;
13+
1114
/**
1215
* 组件描述的资源链接;
1316
*/
14-
url?: string;
17+
url?: Asset;
18+
1519
/**
1620
* 组件 (库) 的 npm 信息;
1721
*/

packages/utils/src/asset.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,8 @@ function parseAsset(scripts: any, styles: any, asset: Asset | undefined | null,
214214
}
215215

216216
export class AssetLoader {
217+
private stylePoints = new Map<string, StylePoint>();
218+
217219
async load(asset: Asset) {
218220
const styles: any = {};
219221
const scripts: any = {};
@@ -237,11 +239,9 @@ export class AssetLoader {
237239
await Promise.all(
238240
styleQueue.map(({ content, level, type, id }) => this.loadStyle(content, level!, type === AssetType.CSSUrl, id)),
239241
);
240-
await Promise.all(scriptQueue.map(({ content, type }) => this.loadScript(content, type === AssetType.JSUrl)));
242+
await Promise.all(scriptQueue.map(({ content, type, scriptType }) => this.loadScript(content, type === AssetType.JSUrl, scriptType)));
241243
}
242244

243-
private stylePoints = new Map<string, StylePoint>();
244-
245245
private loadStyle(content: string | undefined | null, level: AssetLevel, isUrl?: boolean, id?: string) {
246246
if (!content) {
247247
return;
@@ -259,11 +259,11 @@ export class AssetLoader {
259259
return isUrl ? point.applyUrl(content) : point.applyText(content);
260260
}
261261

262-
private loadScript(content: string | undefined | null, isUrl?: boolean) {
262+
private loadScript(content: string | undefined | null, isUrl?: boolean, scriptType?: string) {
263263
if (!content) {
264264
return;
265265
}
266-
return isUrl ? load(content) : evaluate(content);
266+
return isUrl ? load(content, scriptType) : evaluate(content, scriptType);
267267
}
268268

269269
// todo 补充类型

packages/utils/src/script.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
import { createDefer } from './create-defer';
22

3-
export function evaluate(script: string) {
3+
export function evaluate(script: string, scriptType?: string) {
44
const scriptEl = document.createElement('script');
5+
scriptType && (scriptEl.type = scriptType);
56
scriptEl.text = script;
67
document.head.appendChild(scriptEl);
78
document.head.removeChild(scriptEl);
89
}
910

10-
export function load(url: string) {
11-
const node: any = document.createElement('script');
11+
export function load(url: string, scriptType?: string) {
12+
const node = document.createElement('script');
1213

1314
// node.setAttribute('crossorigin', 'anonymous');
1415

@@ -34,6 +35,8 @@ export function load(url: string) {
3435
// `async=false` is required to make sure all js resources execute sequentially.
3536
node.async = false;
3637

38+
scriptType && (node.type = scriptType);
39+
3740
document.head.appendChild(node);
3841

3942
return i.promise();

0 commit comments

Comments
 (0)