From Observable’s Not JavaScript:
Since everything in Observable is inherently dynamic, there’s not really a need for static ES imports—though, we might add support in the future.
I’d like to disagree with this. More and more open source libraries are published as ES-Module-only, meaning you can’t use require
, and it doesn’t feel good to use it in a modern ES environment like Observable. Dynamic imports on the other hand feel quite awkward and inconvenient compared to static imports syntax in several ways:
a) If the library exposes itself through a default export (which is the case for a lot of libraries, if not the majority), you’re forced to write this construct that’s unfriendly and unexpected to most users:
Delatin = (await import('https://unpkg.com/delatin')).default
b) With static imports of Observable cells, you can import multiple utility functions at once:
import {slider, button} from '@jashkenas/inputs'
With dynamic imports, you’re forced to namespace them:
inputs = import('https://unpkg.com/foobar')
c) Inconsistency. It would be easier for beginners if they didn’t have to remember that you can only use static imports for cells, and dynamic imports for external modules, and this choice contradicts with this argument:
Since everything in Observable is inherently dynamic, there’s not really a need for static ES imports
If everything in Observable is inherently dynamic and there’s no need for static imports, why are they used for importing cells? I understand that there was a need to differentiate between resolving non-URL names to Observable vs NPM, but using static vs dynamic imports for this doesn’t feel right.
Technically, you could expand static imports syntax so that it resolves URLs to external modules and non-URLs to Observable notebooks. Limiting NPM name resolution to “legacy” require
feels right — see also Pika Web and Deno for examples where explicit resolution was chosen as preferred, inspired by browser ESM implementation.
It’s a subtle thing and not a deal breaker for me, but thought I’d my two cents because I think static imports of external modules would be a really awesome improvement!