-
Notifications
You must be signed in to change notification settings - Fork 6
/
rjs.js
64 lines (60 loc) · 1.62 KB
/
rjs.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/*
* @package Micro-RequireJs
* @author sheiko
* @license MIT
* @copyright (c) Dmitry Sheiko http://www.dsheiko.com
*/
window.rjs = (function( window, undefined ) {
"use strict";
var loadStrategy = {
js: function( src ) {
var el = document.createElement( "script" );
el.src = src;
el.async = true;
return el;
},
css: function ( src ) {
var el = document.createElement( "link" );
el.type = "text/css";
el.rel = "stylesheet";
el.href = src;
return el;
}
};
/**
* Load resource
* @param {String} src
* @param {Object} [options]
*/
function load( src, options ) {
return new Promise(function ( resolve, reject ) {
var ext = src.split( "." ).pop().toLowerCase();
if ( !( ext in loadStrategy ) ) {
throw new Error( "Invalid file extension in ", src );
}
var el = loadStrategy[ ext ]( src );
el.onload = resolve;
el.onerror = reject;
options && Object.keys( options ).forEach(function( key ){
el.setAttribute( key, options[ key ] );
});
document.head.appendChild( el );
});
}
/**
* @param {String|Array} src
* @param {Object} [options]
*/
return function() {
var args = arguments;
if ( typeof args[ 0 ] === "string" ) {
return load.apply( null, args );
}
return Promise.all( args[ 0 ].map(function( src ){
if ( typeof src === "string" ) {
return load( src );
}
return load.apply( null, src );
}) );
}
}( this ));