Skip to content
This repository was archived by the owner on Jul 13, 2020. It is now read-only.

module tag support #156

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 49 additions & 28 deletions lib/system.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,42 +224,63 @@

// <script type="module"> support
// allow a data-init function callback once loaded
if (isBrowser) {
var curScript = document.getElementsByTagName('script');
curScript = curScript[curScript.length - 1];
if (!isBrowser) {
if (typeof exports === 'object')
module.exports = System;
return;
}
var curScript = document.getElementsByTagName('script');
curScript = curScript[curScript.length - 1];

function completed() {
document.removeEventListener( "DOMContentLoaded", completed, false );
window.removeEventListener( "load", completed, false );
ready();
function completed() {
document.removeEventListener( "DOMContentLoaded", completed, false );
window.removeEventListener( "load", completed, false );
ready();
}
function hideModules() {
var head = document.getElementsByTagName("head")[0];
var styleElt = document.createElement("style");
styleElt.innerText = 'module {display: none;}';
head.appendChild(styleElt);
}
function ready() {
var scripts = document.getElementsByTagName('script');
var mods = document.getElementsByTagName('module');
var allScripts = [];
var j = 0;
while (j < scripts.length) {
allScripts[allScripts.length++] = scripts[j++];
}

function ready() {
var scripts = document.getElementsByTagName('script');

for (var i = 0; i < scripts.length; i++) {
var script = scripts[i];
if (script.type == 'module') {
var source = script.innerHTML;
System.module(source)['catch'](function(err) { setTimeout(function() { throw err; }); });
}
j = 0;
if (mods.length) {
hideModules();
while (j < mods.length) {
allScripts[allScripts.length++] = mods[j++];
}
}

// DOM ready, taken from https://github.com/jquery/jquery/blob/master/src/core/ready.js#L63
if (document.readyState === 'complete') {
setTimeout(ready);
}
else if (document.addEventListener) {
document.addEventListener('DOMContentLoaded', completed, false);
window.addEventListener('load', completed, false);
var script, source;
for (var i = 0; i < allScripts.length; i++) {
script = allScripts[i];
if (script.type == 'module' || script.tagName.toLocaleLowerCase() === 'module') {
source = script.innerHTML;
System.module(source)['catch'](function(err) { setTimeout(function() { throw err; }); });
}
}
}

// run the data-init function on the script tag
if (curScript.getAttribute('data-init'))
window[curScript.getAttribute('data-init')]();
// DOM ready, taken from https://github.com/jquery/jquery/blob/master/src/core/ready.js#L63
if (document.readyState === 'complete') {
setTimeout(ready);
}
else if (document.addEventListener) {
document.addEventListener('DOMContentLoaded', completed, false);
window.addEventListener('load', completed, false);
}

// run the data-init function on the script tag
if (curScript.getAttribute('data-init'))
window[curScript.getAttribute('data-init')]();

if (typeof exports === 'object')
module.exports = System;

Expand Down
12 changes: 11 additions & 1 deletion test/test.html
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,24 @@
}
}
</script>
<module>
window.anon2 = class {
constructor() {

}
}
</module>


<script>
setTimeout(function() {
test('Anonymous &lt;script type="module"> tag', function(assert) {
assert(typeof window.anon, 'function');
});
}, 500);
test('Anonymous &lt;module> tag', function(assert) {
assert(typeof window.anon2, 'function');
});
}, 500);
</script>