-
Notifications
You must be signed in to change notification settings - Fork 20
/
modular.js
97 lines (80 loc) · 3.22 KB
/
modular.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/***
* __ __ _______ ______ __ __ ___ _______ ______ ___ _______
* | |_| || || | | | | || | | _ || _ | | || |
* | || _ || _ || | | || | | |_| || | || | || _____|
* | || | | || | | || |_| || | | || |_||_ | || |_____
* | || |_| || |_| || || |___ | || __ | ___ ___| ||_____ |
* | ||_|| || || || || || _ || | | || | | | _____| |
* |_| |_||_______||______| |_______||_______||__| |__||___| |_||___| |_______||_______|
*
* A modular javascript and css loading library written in vanilla javascript
* Created by Sem Voigtländer. Licensed under the MIT-License
*/
var ModularJS = {}; //struct init;
//Initialisation
ModularJS = {
Author: "Sem Voigtländer",
Description: "ModularJS is a vanilla javascript loader for javascript and css includes",
Version: 1.0,
};
//Configuration
ModularJS.config = {
vebosity: 100,
debug: true,
modulebase: "/"
};
//Logging functionality
ModularJS.error = function(msg) {
throw new error(msg);
};
ModularJS.warn = function(msg) {
console.log('%c'+msg.toString(), 'background: orange; color: black;');
};
ModularJS.log = function(msg) {
console.log('%c'+msg.toString(), 'background: white; color: grey;');
};
ModularJS.load_resource = function(url, type) {
try {
//load resource needs the URL parameter
if(!url) {
ModularJS.error("[ModularJS][LoadResource]: Invalid Arguments: No url provided");
return false;
}
url = url.toString(); //makes sure the url will be a string
//checks whether modulebase is set and checks for explicit protocol definiton
if(ModularJS.config.modulebase && url.indexOf('://') === -1) url = ModularJS.config.modulebase + "/" + url;
if(!type) type = 'script'; //The default type of a module is javascript
if(type == 'script') {
var tag = document.createElement("script");
tag.src = url + ".module.js";
tag.type = "text/javascript";
tag.id = "script-"+new Date().toString();
tag.addEventListener('load', function(){
ModularJS.log('[ModularJS][LoadResource]: ' + tag.src);
}, false);
document.head.appendChild(tag);
document.getElementById(tag.id).remove();
}
//In case of processing a stylesheet
else if(type == 'style') {
var tag = document.createElement('style');
tag.href = url + ".module.css";
tag.type = 'text/css';
tag.id = 'style'+new Date().toString();
tag.addEventListener('load', function(){
ModularJS.log('[ModularJS][LoadResource]: ' + tag.src);
}, false);
document.head.appendChild(tag);
document.getElementById(tag.id).remove();
}
else {
ModularJS.error('[Modular][LoadResource]: '+ 'Unknown type');
}
} catch (ex) {
ModularJS.error('[ModularJS][Unknown]: A fatal Runtime Error occured.');
}
};
var Using, using = ModularJS.load_resource;
var Module, module = ModularJS.load_resource;
var Include, include = ModularJS.load_resource;
var Import = ModularJS.load_resource;