Currently there is no native way to bundle modules in JavaScript. You can only import from single files thus making it necessary to use bundlers like webpack.
We should have a way to bundle modules either using a single zip/wbn file or from a directory - in both cases by referencing an external import map.
I realize bundling is not the primary concern for this repo - but the main idea here is relying on external import maps rather than the concept of bundling itself.
What does it look like?
We can use the fragment identifier to refer to modules from external import maps.
This will resolve the bundle specifier and will do either of 2 things:
If the specifier points to an .importmap file then it will attempt to resolve the main module described inside of that import map.
If the specifier points to a .zip file then it will fetch the zip file, extract the contents, make sure the root contains a .importmap file, and attempt to resolve the main module described inside of that import map.
Directly referencing an import map can be the way to work during development mode.
How it will work for zips
For production we'd create a zip file containing our JS modules along with a .importmap file at the root with the import maps of all contained modules.
For relative imports it will be relative to base URL of the .importmap file or within the zip file.
Resolution of bare imports will first do a lookup of the external import map and then fallback to its parent level import map and keep working up the tree.
So, for the following external import maps and main.js file:
The attempt to import vendor#lodash within main.js should result in importing the ./node_modules/lodash.js file that is described in 'vendor.importmap'.
Dynamic imports
Since we're just using regular import syntax and import maps - dynamic imports should work the same way for bundled modules.
Scoping/Remapping
Not sure if we should allow remapping imports from within scope of bundles on account of deeply nested import maps.
Problem
Currently there is no native way to bundle modules in JavaScript. You can only import from single files thus making it necessary to use bundlers like webpack.
We should have a way to bundle modules either using a single zip/wbn file or from a directory - in both cases by referencing an external import map.
I realize bundling is not the primary concern for this repo - but the main idea here is relying on external import maps rather than the concept of bundling itself.
What does it look like?
We can use the fragment identifier to refer to modules from external import maps.
This will resolve the
bundle
specifier and will do either of 2 things:.importmap
file then it will attempt to resolve themain
module described inside of that import map..zip
file then it will fetch the zip file, extract the contents, make sure the root contains a.importmap
file, and attempt to resolve themain
module described inside of that import map.How it will work for directories
Directly referencing an import map can be the way to work during development mode.
How it will work for zips
For production we'd create a zip file containing our JS modules along with a
.importmap
file at the root with the import maps of all contained modules.Module resolution
For relative imports it will be relative to base URL of the
.importmap
file or within thezip
file.Resolution of bare imports will first do a lookup of the external import map and then fallback to its parent level import map and keep working up the tree.
So, for the following external import maps and
main.js
file:used inside of this HTML:
The attempt to import
vendor#lodash
withinmain.js
should result in importing the./node_modules/lodash.js
file that is described in 'vendor.importmap'.Dynamic imports
Since we're just using regular
import
syntax and import maps - dynamic imports should work the same way for bundled modules.Scoping/Remapping
Not sure if we should allow remapping imports from within scope of bundles on account of deeply nested import maps.