Skip to content

Commit

Permalink
feat(utils): support script type param for assest loader
Browse files Browse the repository at this point in the history
  • Loading branch information
keuby committed Nov 1, 2023
1 parent 95a48e0 commit 54997e6
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
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);
}

// todo 补充类型
Expand Down
6 changes: 4 additions & 2 deletions packages/utils/src/script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ export function evaluate(script: string) {
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 +34,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

0 comments on commit 54997e6

Please sign in to comment.