TO ADD
tl;dr
https://www.thedreaming.org/2017/04/28/es6-imports-babel/
Example - import leaflet in an .astro file
Leaflet uses ECMAScript modules
Benefits
- Organization - Explicit dependency declaration includes version number
- Better code -
<script>
type imports global scope, can cause issues with your and other code. Wheras module files are encapsulated
- Performance - Import and ship only what you need
import { thing1, thing2 } from 'package'
.
- Performance - Modules declare what they depend on (import) they can share dependencies and you aren't importing the same script (latency and pollution)
<script src="https://unpkg.com/leaflet@1.9.4/dist/leaflet.js"></script>
const L = require('leaflet');
<script>
import 'leaflet/dist/leaflet.css'
import 'leaflet'
</script>