Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Feature detect ES modules: both static import and dynamic import()
<!--
Complete feature detection for ES modules. Covers:
1. Static import: import * from './foo.js';
2. Dynamic import(): import('./foo.js').then(module => {...});
Demo: http://jsbin.com/tilisaledu/1/edit?html,output
Thanks to @_gsathya, @kevincennis, @rauschma, @malyw for the help.
-->
<body></body>
<!-- Remember: static modules have a fallback! -->
<script type="module">
console.log('This browser supports <script type="module">');
</script>
<script nomodule>
console.log('This browser DOES NOT support <script type="module">');
</script>
<script>
// Feature detect static imports.
function supportsStaticImport() {
const script = document.createElement('script');
return 'noModule' in script;
}
// Feature detect dynamic import().
function supportsDynamicImport() {
try {
new Function('import("")');
return true;
} catch (err) {
return false;
}
}
// Usage.
let el = document.createElement('pre');
el.textContent = `
Supports ES module static import: ${supportsStaticImport()}
Supports dynamic ES module import(): ${supportsDynamicImport()}
`
document.body.appendChild(el);
</script>
@nicolasparada

This comment has been minimized.

Copy link

nicolasparada commented Nov 23, 2017

With this test, Chrome 62 and Edge 16 passes for dynamic import while they don't support it really.

@tschoartschi

This comment has been minimized.

Copy link

tschoartschi commented Jun 25, 2018

@nicolasparada, @ebidel is this feature-check still state of the art? How can I implement a working feature-check for Chrome 62 and Edge 16? Offtopic: how can I test Chrome 62 when I have Chrome 67 installed? Is there a trustworthy download source for older versions of Chrome or do I have to download it from some dubious website?

@ebidel

This comment has been minimized.

Copy link
Owner Author

ebidel commented Jul 2, 2018

You can get older versions of Chromium from: https://download-chromium.appspot.com/. Those are official builds.

Why do we need to test Chrome 62? That's pretty old at this point.

@tschoartschi

This comment has been minimized.

Copy link

tschoartschi commented Jul 3, 2018

@ebidel thanks for the link. I'll try it out later. I don't want to test Chrome 62 in depth but I think a feature-detection should be tested as much as possible. We load different bundles depending on the features a browser supports. We want to always ship the bundle which fits best for the browser and not a transpiled file which is only transpiled because it works on IE10. So if the feature-detection is wrong, the client will get a bundle which is broken on his machine and I want to avoid that.

This is why I wanted to check if @nicolasparada is correct and if the feature detection is really broken in Chrome 62 and Edge 16.

@subversivo58

This comment has been minimized.

Copy link

subversivo58 commented Oct 13, 2018

@ebidel thanks, this is beautiful. There is a license to freely use the code of this gist?

@jcdalton2201

This comment has been minimized.

Copy link

jcdalton2201 commented Jan 24, 2019

@ebidel what is a good polyfill for dynamic import?

@derappelt

This comment has been minimized.

@vokeio

This comment has been minimized.

Copy link

vokeio commented Sep 23, 2019

@ebidel

The use of new Function creates a new function but does not invoke this new function. I am pretty sure the current supportsDynamicImport() always returns true. The fix for this issue would need to add () after new Function.

This would NOT execute.

new Function('import("")');

This would execute.

new Function('import("")')();

I would also suggest catching the uncaught promise error. So line 32 could look as follows.

new Function('import("").catch(function(){})')();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.