-
Notifications
You must be signed in to change notification settings - Fork 14
/
html.js
105 lines (91 loc) · 2.71 KB
/
html.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
/*
HTML Import plugin
Adapted from systemjs/plugin-css
https://github.com/systemjs/plugin-css
*/
if (typeof window === 'undefined') {
exports.fetch = function (load) {
load.metadata.build = true;
load.metadata.format = 'defined';
return '';
};
exports.instantiate = function () {};
exports.bundle = function (loads, opts) {
var loader = this;
if (loader.buildHTML === false) {
return '';
}
return loader['import']('./html-builder', {name: module.id}).then(function (builder) {
return builder.call(loader, loads, opts);
}, function (err) {
if (err.toString().indexOf('Cannot find module \'vulcanize\'') !== -1) {
throw new Error('Install Polymer/vulcanize via `npm install vulcanize --save-dev` for HTML build support. Set System.buildHTML = false to skip HTML builds.');
}
throw err;
});
};
} else {
exports.build = false;
exports.fetch = function (load) {
return importHref(load);
};
exports.instantiate = function (load) {
return load.metadata.link.import;
};
}
var waitSeconds = 100;
var head = (typeof document === 'undefined') ? null : document.getElementsByTagName('head')[0];
function errCallback(err) {
setTimeout(function () {
throw err;
});
}
// from https://github.com/ModuleLoader/es6-module-loader/issues/95#issuecomment-98705035
function processScript(script) {
var source = script.innerHTML.substr(1);
return System.module(source).catch(errCallback);
}
function processDocument(e) {
var Q = [];
// process modules in this document
var scripts = e.querySelectorAll('script[type="module"]');
for (var i = 0; i < scripts.length; i++) {
Q.push(processScript(scripts[i]));
}
// process imports (not yet working as expected)
var links = e.querySelectorAll('link[rel="import"]');
for (var j = 0; j < links.length; j++) {
Q.push(processDocument(links[j].import));
}
return Promise.all(Q);
}
function importHref(load) {
return new Promise(function (resolve, reject) {
var link = load.metadata.link = document.createElement('link');
link.rel = 'import';
link.href = load.address;
var timeout = setTimeout(function () {
reject('Unable to load HTML');
}, waitSeconds * 1000);
var _callback = function (error) {
clearTimeout(timeout);
link.onload = link.onerror = function () {};
setTimeout(function () {
if (error) {
reject(error);
} else {
resolve('');
}
}, 7);
};
link.onload = function () {
processDocument(link.import).then(function () {
_callback();
});
};
link.onerror = function (event) {
_callback(event.error);
};
head.appendChild(link);
});
}