Enhance browser-JavaScript support to include JSX/Typescript syntax for websites utilizing importmaps.
npm install @zachsa/esm-xEnhance browser-JavaScript support to include JSX/Typescript syntax for websites utilizing importmaps.
- Motivation
- How it works
- Usage
- Options
- Importmaps
- External importmaps
- Local development
- Publish
- Install
With all major browsers now supporting importmaps, bundle-free web development workflows have become both feasible and incredibly convenient. This project seeks to bring that convenience to React/Typescript projects.
Leverages importmaps in conjunction with the ES Module Shim library. With shimMode forced to true, all Source Code from the website origin is transpiled using Babel, while prior-optimized imports originating from a module CDN (such as the excellent JSPM CDN) are loaded directly.
Include the esm-x library (328kB gzipped) as the first script in your HTML file, and include at least one . Scripts of type="esm-x" will be transpiled and executed in the order they are included in the HTML page.
Here is an example of a simple React application with the react and react-dom library imports defined via an importmap. Copy this file into index.html, and serve via a web server (i.e. npx http-server -c-1). There is an example of an @mui/material app in the dev directory of this repository.
esm-x works without an importmap, but ES Module Shims is required.
``html
id="esm-x"
compiler="babel"
loading="circular"
src="https://www.unpkg.com/@zachsa/esm-x@1.1.2/dist/index.js"
>
async
src="https://ga.jspm.io/npm:es-module-shims@2.6.2/dist/es-module-shims.js"
crossorigin="anonymous"
>
Options
Configure the ESM-X script by including HTML tag id and other attributes:
`html
`Importmaps
Head to the JSPM generator to quickly generate an importmap. Any importmap configuration should be supported - huge importmaps are fine, make sure to take advantage of dynamic imports / code splitting (for example,
Suspense/lazy when using React), etc. (and obviously don't preload scripts)$3
To use an external importmap, make sure that your importmap tag is of type "importmap-shim", and that the src url
endsWith('importmap') || endsWith('importmap.json'). For example:`html
`jspm CLI.Local development
Start a local web server and navigate to localhost:3000/dev:
`sh
npx http-server --port 3000 -c-1
chomp --watch
`Test the following cases:
- index.html (default)
- no-importmap.html
- tsx.html
- empty-importmap.html
- docs.html (copy this to README.md)
Publish
`sh
npm publish --access public
``