The `import()` Syntax of ES2020 Looks Promising; How to Detect if Such Feature Available Then?
Taufik Nurrohman

Taufik Nurrohman @taufik_nurrohman

About: I have a dream, but it won’t make me rich.

Location:
Banyumas, Indonesia
Joined:
Jun 20, 2019

The `import()` Syntax of ES2020 Looks Promising; How to Detect if Such Feature Available Then?

Publish Date: Jun 20 '20
5 0

With the plan, import() is proposed to work in both modules and scripts, giving script code an easy asynchronous entry point into the module world and allowing it to start running module code.

https://www.infoworld.com/article/3538809/ecmascript-2020-spec-for-javascript-approved.amp.html

Can I check the existence of this feature as it was a function? And if that is possible, is it conform to the standard?

What I am thinking:

if ('function' === typeof import) {
    import('taufik-nurrohman/f3h.js').then(F3H => {
        let f3h = new F3H;
    });
}
Enter fullscreen mode Exit fullscreen mode

Also, how do I expose module in the file to import, without causing syntax errors in the window environment?

// :|
if ('function' === typeof import) {
    export default F3H;
} else {
    window.F3H = F3H;
}
Enter fullscreen mode Exit fullscreen mode

Thanks!

Comments 0 total

    Add comment