-
Notifications
You must be signed in to change notification settings - Fork 1
/
can-import-module.js
107 lines (97 loc) · 2.49 KB
/
can-import-module.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
98
99
100
101
102
103
104
105
106
107
'use strict';
var namespace = require("can-namespace");
/**
* @module {function} can-util/js/import/import import
* @parent can-util/js
* @signature `importModule(moduleName, parentName)`
* @hide
*
* ```js
* var importModule = require("can-util/js/import/import");
*
* importModule("foo.stache").then(function(){
* // module was imported
* });
* ```
*
* @param {String} moduleName The module to be imported.
* @param {String} [parentName] A parent module that will be used as a reference for resolving relative module imports.
* @return {Promise} A Promise that will resolve when the module has been imported.
*/
// array of loader functions, last in first out
var loader = [];
/**
* add a loader-function to the list of loader
* the function should return a promise that resolves when the module has been loaded
* otherwise the loader function should return null or undefined
*
* @signature `import.addLoader(loader)`
* @param fn callable
*/
function addLoader(fn){
if(typeof fn === "function"){
loader.push(fn);
}
}
/**
* clear the list of loaders
*/
function flushLoader(){
loader = [];
}
/**
* a bunch of presets that can be used in a certain environment
*
* @param preset string
*/
function preset(preset){
flushLoader();
switch (preset){
case "stealjs":
addLoader(require("./loader/steal-optimized"));
addLoader(require("./loader/system"));
break;
case "ES2020":
case "es2020":
case "dynamic-import":
addLoader(require("./loader/es6"));
break;
case "node":
addLoader(require("./loader/node"));
break;
case "all":
default:
addLoader(require("./loader/steal-optimized"));
addLoader(require("./loader/es6"));
addLoader(require("./loader/node"));
addLoader(require("./loader/require"));
addLoader(require("./loader/system"));
break;
}
}
// by default, add all available loaders to the list
preset('all');
module.exports = namespace.import = function(moduleName, parentName) {
return new Promise(function(resolve, reject) {
try {
var loaderPromise;
// last added loader will be called first
for (var i = loader.length - 1; i >= 0; i--) {
loaderPromise = loader[i](moduleName, parentName);
if (loaderPromise) {
break;
}
}
if(loaderPromise){
loaderPromise.then(resolve, reject);
}else{
reject("no proper module-loader available");
}
} catch(err) {
reject(err);
}
});
};
module.exports.addLoader = addLoader;
module.exports.flushLoader = flushLoader;
module.exports.preset = preset;