(asset, onload, onerror)
| 536 | * @category Assets |
| 537 | */ |
| 538 | export function load(asset, onload, onerror) { |
| 539 | // make sure all parsers have been initialized |
| 540 | if (parserInitialized === false) { |
| 541 | initParsers(); |
| 542 | } |
| 543 | |
| 544 | // strip url() wrapper for fontface assets so baseURL can be prepended to the raw path |
| 545 | if (asset.type === "fontface" && typeof asset.src === "string") { |
| 546 | const urlMatch = asset.src.match(/^url\(\s*['"]?(.*?)['"]?\s*\)$/); |
| 547 | if (urlMatch) { |
| 548 | asset.src = urlMatch[1]; |
| 549 | } |
| 550 | } |
| 551 | |
| 552 | // transform the url if necessary (skip for local() font sources and data URIs) |
| 553 | if ( |
| 554 | typeof baseURL[asset.type] !== "undefined" && |
| 555 | typeof asset.src === "string" && |
| 556 | !asset.src.startsWith("local(") && |
| 557 | !asset.src.startsWith("data:") |
| 558 | ) { |
| 559 | asset.src = baseURL[asset.type] + asset.src; |
| 560 | } |
| 561 | |
| 562 | const parser = parsers.get(asset.type); |
| 563 | |
| 564 | if (typeof parser === "undefined") { |
| 565 | throw new Error("load : unknown or invalid resource type : " + asset.type); |
| 566 | } |
| 567 | |
| 568 | const settings = { |
| 569 | nocache: nocache, |
| 570 | crossOrigin: crossOrigin, |
| 571 | withCredentials: withCredentials, |
| 572 | }; |
| 573 | |
| 574 | // Promise form: with no callbacks, resolve once the asset (and any |
| 575 | // sub-resources) have loaded — for a one-off `await loader.load(asset)`. |
| 576 | // The legacy callback form below still returns the resource count. |
| 577 | if (onload === undefined && onerror === undefined) { |
| 578 | return new Promise((resolve, reject) => { |
| 579 | // parser returns the amount of asset to be loaded (usually 1, more |
| 580 | // if it splits into several); 0 means already cached → resolve now. |
| 581 | const count = parser.call( |
| 582 | this, |
| 583 | asset, |
| 584 | () => { |
| 585 | resolve(); |
| 586 | }, |
| 587 | reject, |
| 588 | settings, |
| 589 | ); |
| 590 | if (count === 0) { |
| 591 | resolve(); |
| 592 | } |
| 593 | }); |
| 594 | } |
| 595 |
no test coverage detected