Skip to content

Commit 0ca690d

Browse files
committed
Use parse-error-free feature detection, stream-line fallback
1 parent 9b9bda9 commit 0ca690d

File tree

1 file changed

+27
-31
lines changed

1 file changed

+27
-31
lines changed

public/index.html

Lines changed: 27 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -24,36 +24,32 @@ <h1>Rollup code-splitting demo</h1>
2424
<div data-used-by='both'></div>
2525
</div>
2626

27-
<!-- for modern browsers -->
28-
<script type='module'>
29-
import('/module/main-a.js');
30-
import('/module/main-b.js');
31-
window.hasDynamicImport = true;
32-
</script>
33-
34-
<!-- for older browsers -->
35-
<script nomodule src='https://unpkg.com/systemjs@0.21.0/dist/system-production.js'></script>
36-
<script nomodule>
37-
System.import('nomodule/main-a.js');
38-
System.import('nomodule/main-b.js');
39-
</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>
27+
<script nomodule>
28+
// If a browser does not support modules, feature detection may not not work properly below, so note this here
29+
window.loadEntryPoints = null;
30+
</script>
31+
<script>
32+
if (window.loadEntryPoints !== null) {
33+
try {
34+
// this will throw a runtime exception in browsers that support "import" but not "import()"
35+
window.loadEntryPoints = new Function('import("/module/main-a.js");import("/module/main-b.js");');
36+
} catch (err) {
37+
window.loadEntryPoints = null;
38+
}
39+
}
40+
if (window.loadEntryPoints) {
41+
// browser supports dynamic import
42+
window.loadEntryPoints();
43+
} else {
44+
// browser does not support dynamic import
45+
const systemJsLoaderTag = document.createElement('script');
46+
systemJsLoaderTag.src = 'https://unpkg.com/systemjs@0.21.4/dist/system-production.js';
47+
systemJsLoaderTag.addEventListener('load', function () {
48+
System.import('./nomodule/main-a.js');
49+
System.import('./nomodule/main-b.js');
50+
});
51+
document.head.appendChild(systemJsLoaderTag);
52+
}
53+
</script>
5854
</body>
5955
</html>

0 commit comments

Comments
 (0)