11# module-tsx
22
3- Run TypeScript (and React) module directly in the browser.
3+ Run TypeScript (and React) modules directly in the browser, without a build step .
44
5- # Usage
5+ ## Usage
66
77``` html
8- <!-- Load This Library -->
8+ <!-- Load this library -->
99<script type =" module" src =" https://esm.sh/module-tsx" ></script >
1010
1111<div id =" root" ></div >
1212
13- <!-- Write Your TypeScript Module -->
13+ <!-- Write your TypeScript/TSX inline -->
1414<script type =" module-tsx" >
15- import React from " react" ; // <- This will be converted to https://esm.sh/react
16- import { createRoot } from " react-dom/client" ; // <- This will also be converted automatically
17- import App from " ./src/App.tsx" ; // <- Your TypeScript module will also be compiled on the fly
18- import lib from " https://my.cdn.domain" ; // <- Direct http import will NOT be compiled
15+ import React from " react" ; // bare specifier → https://esm.sh/react
16+ import { createRoot } from " react-dom/client" ;
17+ import App from " ./src/App.tsx" ; // relative imports are fetched and compiled on the fly
1918
20- const root = document .getElementById (" root" ) as HTMLDivElement ; // <- You can write TypeScript directly
19+ const root = document .getElementById (" root" ) as HTMLDivElement ;
2120 createRoot (root).render (
2221 < React .StrictMode >
2322 < App / >
2423 < / React .StrictMode >
25- // ^ You can also write JSX/TSX directly
2624 );
2725 </script >
28- <!-- OR -->
26+
27+ <!-- OR load an external file -->
2928<script type =" module-tsx" src =" ./main.tsx" ></script >
3029```
3130
@@ -37,3 +36,50 @@ export default function App() {
3736 return <div >Hello, module-tsx!</div >;
3837}
3938```
39+
40+ ## Features
41+
42+ - ** TypeScript & JSX/TSX** — transpiled on the fly using the TypeScript compiler
43+ - ** Bare specifier resolution** — ` import "react" ` is automatically rewritten to a CDN URL (defaults to ` https://esm.sh/ ` )
44+ - ** Relative imports** — ` .ts ` /` .tsx ` files are fetched and compiled recursively
45+ - ** CSS imports** — ` import "./style.css" ` injects a ` <style> ` tag; ` import "./style.module.css" ` returns a CSS Modules object
46+ - ** Import map support** — respects ` <script type="importmap"> ` on the page for specifier overrides
47+ - ** Auto React import** — JSX is detected and ` import React from "react" ` is injected automatically if missing
48+
49+ ## Programmatic API
50+
51+ ``` ts
52+ import { ModuleTSX } from " module-tsx" ;
53+
54+ const m = new ModuleTSX ({
55+ baseUrl: location .href , // base for resolving relative specifiers
56+ importMap: { imports: { react: " https://esm.sh/react" } }, // merged with page importmaps
57+ resolveBareSpecifier: " https://cdn.jsdelivr.net/npm/" , // string prefix or function
58+ });
59+
60+ // Import a module by specifier
61+ const react = await m .import (" react" );
62+ const app = await m .import (" ./app.tsx" );
63+
64+ // Import from an in-memory string
65+ const { x } = await m .importCode (document .location .href , ` export const x: number = 1 ` );
66+
67+ // Events
68+ m .addEventListener (" import" , (e ) => console .log (" loading" , e .detail .id ));
69+ m .addEventListener (" import:error" , (e ) => console .error (" failed" , e .detail .id , e .detail .error ));
70+ m .addEventListener (" transform" , (e ) => console .log (" compiling" , e .detail .sourceUrl ));
71+ m .addEventListener (" transform:error" , (e ) => console .error (" compile error" , e .detail .sourceUrl , e .detail .error ));
72+ ```
73+
74+ ## CDN
75+
76+ ``` html
77+ <!-- ESM -->
78+ <script type =" module" src =" https://esm.sh/module-tsx" ></script >
79+
80+ <!-- ESM (self-contained, no external dependencies) -->
81+ <script type =" module" src =" https://raw.esm.sh/module-tsx/dist/index.mjs" ></script >
82+
83+ <!-- UMD (exposes window.ModuleTSX) -->
84+ <script src =" https://raw.esm.sh/module-tsx/dist/index.umd.js" ></script >
85+ ```
0 commit comments