Skip to content

Commit 9aefe83

Browse files
samthoralexeagle
authored andcommitted
feat(@angular-devkit/build-angular): add safari-nomodule snippet
1 parent 801f667 commit 9aefe83

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* Safari 10.1 supports modules, but does not support the `nomodule` attribute - it will
3+
* load <script nomodule> anyway. This snippet solve this problem, but only for script
4+
* tags that load external code, e.g.: <script nomodule src="nomodule.js"></script>
5+
*
6+
* Again: this will **not** prevent inline script, e.g.:
7+
* <script nomodule>alert('no modules');</script>.
8+
*
9+
* This workaround is possible because Safari supports the non-standard 'beforeload' event.
10+
* This allows us to trap the module and nomodule load.
11+
*
12+
* Note also that `nomodule` is supported in later versions of Safari - it's just 10.1 that
13+
* omits this attribute.
14+
*/
15+
(function() {
16+
var check = document.createElement('script');
17+
if (!('noModule' in check) && 'onbeforeload' in check) {
18+
var support = false;
19+
document.addEventListener('beforeload', function(e) {
20+
if (e.target === check) {
21+
support = true;
22+
} else if (!e.target.hasAttribute('nomodule') || !support) {
23+
return;
24+
}
25+
e.preventDefault();
26+
}, true);
27+
28+
check.type = 'module';
29+
check.src = '.';
30+
document.head.appendChild(check);
31+
check.remove();
32+
}
33+
}());
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
(function() {
2+
var d = document;
3+
var c = d.createElement('script');
4+
if (!('noModule' in c) && 'onbeforeload' in c) {
5+
var s = false;
6+
d.addEventListener('beforeload', function(e) {
7+
if (e.target === c) {
8+
s = true;
9+
} else if (!e.target.hasAttribute('nomodule') || !s) {
10+
return;
11+
}
12+
e.preventDefault();
13+
}, true);
14+
15+
c.type = 'module';
16+
c.src = '.';
17+
d.head.appendChild(c);
18+
c.remove();
19+
}
20+
}());

0 commit comments

Comments
 (0)