Skip to content

Commit 11e2679

Browse files
authored
Merge pull request #5 from rollup/nomodule-dynamic-import
nomodule dynamic import support
2 parents 16eeaa2 + 5fd54e0 commit 11e2679

File tree

1 file changed

+24
-3
lines changed

1 file changed

+24
-3
lines changed

public/index.html

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,35 @@ <h1>Rollup code-splitting demo</h1>
2525
</div>
2626

2727
<!-- for modern browsers -->
28-
<script type='module' src='module/main-a.js'></script>
29-
<script type='module' src='module/main-b.js'></script>
28+
<script type='module'>
29+
import('/module/main-a.js');
30+
import('/module/main-b.js');
31+
window.hasDynamicImport = true;
32+
</script>
3033

3134
<!-- for older browsers -->
3235
<script nomodule src='https://unpkg.com/systemjs@0.21.0/dist/system-production.js'></script>
3336
<script nomodule>
3437
System.import('nomodule/main-a.js');
3538
System.import('nomodule/main-b.js');
3639
</script>
40+
41+
<!-- this snippet ensures nomodule fallback for browsers supporting modules but not dynamic import -->
42+
<script type="module">
43+
if (!window.hasDynamicImport) {
44+
const noms = Array.prototype.filter.call(document.getElementsByTagName('script'), x => x.hasAttribute('nomodule'));
45+
function nextLoad (nom, s) {
46+
if (!(nom = noms.shift())) return;
47+
s = document.createElement('script');
48+
if (nom.src)
49+
s.src = nom.src, s.addEventListener('load', nextLoad), s.addEventListener('error', nextLoad);
50+
else
51+
s.innerHTML = nom.innerHTML;
52+
document.head.appendChild(s);
53+
if (!nom.src) nextLoad();
54+
}
55+
nextLoad();
56+
}
57+
</script>
3758
</body>
38-
</html>
59+
</html>

0 commit comments

Comments
 (0)