-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
dynamic-import-maps.js
83 lines (71 loc) · 2.14 KB
/
dynamic-import-maps.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
const appendImportMap = function (moduleId, src) {
const script = document.createElement('script');
script.type = 'systemjs-importmap';
if (src) {
script.async = true; // avoid theoretical timing issues
script.src = src;
}
if (moduleId) {
var map = { imports: {} };
map.imports[moduleId] = 'fixtures/esm.js';
script.textContent = JSON.stringify(map);
}
container.appendChild(script);
return script;
};
const importAfterImportMap = function (id) {
return new Promise(function (resolve) { setTimeout(resolve, 10); }).then(function () {
return System.import(id);
});
};
const importNonExistent = function (id) {
return System.import(id)
.catch(function (err) { return err })
.then(function (result) {
if (!(result instanceof Error)) {
throw new Error('Unexpected mapping found for module "' + id + '"');
}
});
}
let container;
suite('Dynamic import maps', function () {
suiteSetup(function () {
return System.import('../../dist/extras/dynamic-import-maps.js')
});
setup(function () {
container = document.createElement('div');
container.id = 'importmap-container';
document.body.appendChild(container);
});
teardown(function () {
container.parentNode.removeChild(container);
});
test('Loading newly added elements (inline)', function () {
const moduleId = 'dynamic-inline-map-1';
return importNonExistent(moduleId)
.then(function () {
appendImportMap(moduleId);
return importAfterImportMap(moduleId);
});
});
test('Loading newly added elements (external)', function () {
const moduleId = 'dynamic-external-map-1';
return importNonExistent(moduleId)
.then(function () {
appendImportMap(null, 'fixtures/browser/dynamic-importmap1.json');
return importAfterImportMap(moduleId);
});
});
test('Loading manually added import map', function () {
const moduleId = 'manual-map-1';
return importNonExistent(moduleId)
.then(function () {
System.addImportMap({
"imports": {
[moduleId]: "./fixtures/browser/esm.js"
}
})
return importAfterImportMap(moduleId);
});
});
});