bimba-cli 0.7.12 → 0.7.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +5 -5
- package/package.json +1 -1
- package/serve.js +60 -88
package/README.md
CHANGED
|
@@ -37,9 +37,9 @@ bunx bimba src/index.imba --serve --port 5200 --html public/index.html
|
|
|
37
37
|
**How it works:**
|
|
38
38
|
- Serves your HTML file and compiles `.imba` files on demand (no bundling step)
|
|
39
39
|
- Watches `src/` for changes and pushes updates over WebSocket
|
|
40
|
-
-
|
|
40
|
+
- Rewrites bare package imports in served JS modules to `__bimba_vendor__/*` URLs
|
|
41
41
|
- CSS files imported from JS (e.g. `import 'some-lib/styles.css'`) are automatically wrapped as JS modules that inject `<style>` tags
|
|
42
|
-
- npm packages
|
|
42
|
+
- npm packages are bundled on demand by Bun (`target: "browser"`), so Bun owns `exports`, `browser`, CommonJS interop, and nested dependency resolution
|
|
43
43
|
- Injects an HMR client that swaps component prototypes without a full page reload
|
|
44
44
|
|
|
45
45
|
**HMR internals:**
|
|
@@ -58,7 +58,7 @@ Duplicate root elements (caused by `imba.mount()` running again on re-import) ar
|
|
|
58
58
|
|
|
59
59
|
For a deep dive into how Imba compiles tags, how the render cache works, and how bimba hooks into it — see [INTERNALS.md](INTERNALS.md).
|
|
60
60
|
|
|
61
|
-
**HTML setup:** add a `data-entrypoint` attribute to the script tag that loads your bundle. The dev server will replace it with your `.imba` entrypoint and
|
|
61
|
+
**HTML setup:** add a `data-entrypoint` attribute to the script tag that loads your bundle. The dev server will replace it with your `.imba` entrypoint and remove existing import maps, since package imports are rewritten in served modules instead:
|
|
62
62
|
|
|
63
63
|
```html
|
|
64
64
|
<script type='module' src="./js/index.js" data-entrypoint></script>
|
|
@@ -72,9 +72,9 @@ For a deep dive into how Imba compiles tags, how the render cache works, and how
|
|
|
72
72
|
|
|
73
73
|
`--html <path>` — path to your HTML file (auto-detected from `./index.html`, `./public/index.html`, `./src/index.html` if omitted)
|
|
74
74
|
|
|
75
|
-
Static files are resolved relative to the HTML file's directory first, then from the project root (for `node_modules`, `src`, etc.). Extensionless imports
|
|
75
|
+
Static files are resolved relative to the HTML file's directory first, then from the project root (for `node_modules`, `src`, etc.). Extensionless imports are resolved by trying `.imba`, `.js`, and `.mjs` extensions automatically.
|
|
76
76
|
|
|
77
|
-
**npm package resolution:** The dev server
|
|
77
|
+
**npm package resolution:** The dev server scans each served JS module and rewrites bare imports such as `imba/runtime`, `@scope/pkg`, and `pkg/subpath` to `__bimba_vendor__/*` URLs. Those vendor URLs are bundled on demand with Bun (`target: "browser"`). Imba source files still compile separately for HMR, while Bun owns dependency resolution, `exports`, `browser` fields, nested `node_modules`, and CommonJS interop.
|
|
78
78
|
|
|
79
79
|
---
|
|
80
80
|
|
package/package.json
CHANGED
package/serve.js
CHANGED
|
@@ -255,6 +255,7 @@ const hmrClient = `
|
|
|
255
255
|
const _compileCache = new Map() // filepath → { mtime, result }
|
|
256
256
|
const _prevJs = new Map() // filepath → compiled js — for change detection
|
|
257
257
|
const _prevSlots = new Map() // filepath → previous symbol slot count
|
|
258
|
+
const _importScanner = new Bun.Transpiler({ loader: 'js' })
|
|
258
259
|
|
|
259
260
|
// Imba compiles tag render-cache slots as anonymous local Symbols at module top
|
|
260
261
|
// level: `var $4 = Symbol(), $11 = Symbol(), ...; let c$0 = Symbol();`. Each
|
|
@@ -299,6 +300,40 @@ function _normalizeResult(result, extras) {
|
|
|
299
300
|
}
|
|
300
301
|
}
|
|
301
302
|
|
|
303
|
+
function isBareSpecifier(specifier) {
|
|
304
|
+
if (!specifier) return false
|
|
305
|
+
if (specifier.startsWith('.') || specifier.startsWith('/')) return false
|
|
306
|
+
if (/^[a-zA-Z][a-zA-Z\d+.-]*:/.test(specifier)) return false
|
|
307
|
+
if (specifier.startsWith('#')) return false
|
|
308
|
+
return true
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
function escapeRegExp(value) {
|
|
312
|
+
return value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
function rewriteBareImports(js) {
|
|
316
|
+
let imports = []
|
|
317
|
+
try {
|
|
318
|
+
imports = _importScanner.scanImports(js)
|
|
319
|
+
} catch (_) {
|
|
320
|
+
return js
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
for (const specifier of new Set(imports.map(item => item.path).filter(isBareSpecifier))) {
|
|
324
|
+
const target = vendorUrl(specifier)
|
|
325
|
+
const escaped = escapeRegExp(specifier)
|
|
326
|
+
|
|
327
|
+
const staticPattern = new RegExp(`((?:import|export)\\s+(?:[^'"]*?\\s+from\\s*)?)(['"])${escaped}\\2`, 'g')
|
|
328
|
+
js = js.replace(staticPattern, (_match, prefix, quote) => `${prefix}${quote}${target}${quote}`)
|
|
329
|
+
|
|
330
|
+
const dynamicPattern = new RegExp(`(\\bimport\\s*\\(\\s*)(['"])${escaped}\\2`, 'g')
|
|
331
|
+
js = js.replace(dynamicPattern, (_match, prefix, quote) => `${prefix}${quote}${target}${quote}`)
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
return js
|
|
335
|
+
}
|
|
336
|
+
|
|
302
337
|
async function compileFile(filepath) {
|
|
303
338
|
const abs = path.resolve(filepath)
|
|
304
339
|
const file = Bun.file(filepath)
|
|
@@ -318,7 +353,7 @@ async function compileFile(filepath) {
|
|
|
318
353
|
const errors = result.errors || []
|
|
319
354
|
if (!errors.length && result.js) {
|
|
320
355
|
const { js, slotCount } = stabilizeSymbols(result.js, abs)
|
|
321
|
-
result.js = js
|
|
356
|
+
result.js = rewriteBareImports(js)
|
|
322
357
|
const prev = _prevSlots.get(abs)
|
|
323
358
|
result.slots = (prev === undefined || prev === slotCount) ? 'stable' : 'shifted'
|
|
324
359
|
_prevSlots.set(abs, slotCount)
|
|
@@ -345,11 +380,7 @@ function findHtml(flagHtml) {
|
|
|
345
380
|
const _vendorCache = new Map() // entrypoint → { mtime, code }
|
|
346
381
|
|
|
347
382
|
function vendorUrl(specifier) {
|
|
348
|
-
return '/__bimba_vendor__/' + specifier
|
|
349
|
-
}
|
|
350
|
-
|
|
351
|
-
function vendorPrefixUrl(specifier) {
|
|
352
|
-
return vendorUrl(specifier) + '/'
|
|
383
|
+
return '/__bimba_vendor__/' + encodeURIComponent(specifier)
|
|
353
384
|
}
|
|
354
385
|
|
|
355
386
|
function resolveFileCandidate(filepath) {
|
|
@@ -453,86 +484,22 @@ async function bundleVendor(entrypoint) {
|
|
|
453
484
|
}
|
|
454
485
|
}
|
|
455
486
|
|
|
456
|
-
async function
|
|
457
|
-
const
|
|
458
|
-
|
|
459
|
-
try {
|
|
460
|
-
const pkg = JSON.parse(await Bun.file('./package.json').text())
|
|
461
|
-
for (const field of ['dependencies', 'devDependencies', 'peerDependencies', 'optionalDependencies']) {
|
|
462
|
-
for (const name of Object.keys(pkg[field] || {})) {
|
|
463
|
-
imports[name] ||= vendorUrl(name)
|
|
464
|
-
imports[name + '/'] ||= vendorPrefixUrl(name)
|
|
465
|
-
}
|
|
466
|
-
}
|
|
467
|
-
} catch(_) { /* no package.json */ }
|
|
468
|
-
|
|
469
|
-
imports['imba'] ||= vendorUrl('imba')
|
|
470
|
-
imports['imba/'] ||= vendorPrefixUrl('imba')
|
|
471
|
-
imports['imba/runtime'] ||= vendorUrl('imba/runtime')
|
|
472
|
-
|
|
473
|
-
return { imports }
|
|
474
|
-
}
|
|
475
|
-
|
|
476
|
-
function extractUserImportMap(html) {
|
|
477
|
-
const merged = { imports: {}, scopes: {} }
|
|
478
|
-
|
|
479
|
-
html = html.replace(/<script\s+type=["']importmap["'][^>]*>([\s\S]*?)<\/script>/gi, (_match, json) => {
|
|
480
|
-
try {
|
|
481
|
-
const parsed = JSON.parse(json)
|
|
482
|
-
Object.assign(merged.imports, parsed.imports || {})
|
|
483
|
-
for (const [scope, specifiers] of Object.entries(parsed.scopes || {})) {
|
|
484
|
-
merged.scopes[scope] = { ...(merged.scopes[scope] || {}), ...specifiers }
|
|
485
|
-
}
|
|
486
|
-
} catch (_) {
|
|
487
|
-
// ignore invalid import maps and keep serving the page
|
|
488
|
-
}
|
|
489
|
-
return ''
|
|
490
|
-
})
|
|
491
|
-
|
|
492
|
-
if (!Object.keys(merged.scopes).length) delete merged.scopes
|
|
493
|
-
return { html, importMap: merged }
|
|
494
|
-
}
|
|
495
|
-
|
|
496
|
-
function mergeImportMaps(generated, user) {
|
|
497
|
-
const merged = {
|
|
498
|
-
imports: { ...(generated.imports || {}), ...(user.imports || {}) },
|
|
499
|
-
}
|
|
500
|
-
|
|
501
|
-
const scopeKeys = new Set([
|
|
502
|
-
...Object.keys(generated.scopes || {}),
|
|
503
|
-
...Object.keys(user.scopes || {}),
|
|
504
|
-
])
|
|
505
|
-
|
|
506
|
-
if (scopeKeys.size) {
|
|
507
|
-
merged.scopes = {}
|
|
508
|
-
for (const key of scopeKeys) {
|
|
509
|
-
merged.scopes[key] = {
|
|
510
|
-
...((generated.scopes || {})[key] || {}),
|
|
511
|
-
...((user.scopes || {})[key] || {}),
|
|
512
|
-
}
|
|
513
|
-
}
|
|
514
|
-
}
|
|
515
|
-
|
|
516
|
-
return merged
|
|
517
|
-
}
|
|
518
|
-
|
|
519
|
-
function renderImportMapTag(importMap) {
|
|
520
|
-
return `\t\t<script type="importmap">\n\t\t\t${JSON.stringify(importMap, null, '\t\t\t\t')}\n\t\t</script>`
|
|
487
|
+
async function serveJavaScriptFile(filepath) {
|
|
488
|
+
const js = rewriteBareImports(await Bun.file(filepath).text())
|
|
489
|
+
return new Response(js, { headers: { 'Content-Type': 'application/javascript' } })
|
|
521
490
|
}
|
|
522
491
|
|
|
523
492
|
// Rewrite production HTML for the dev server:
|
|
524
|
-
// strips existing importmap + data-entrypoint script, injects
|
|
493
|
+
// strips existing importmap + data-entrypoint script, then injects the Imba
|
|
525
494
|
// entrypoint module + HMR client before </head>.
|
|
526
|
-
function transformHtml(html, entrypoint
|
|
527
|
-
|
|
528
|
-
html = extracted.html
|
|
495
|
+
function transformHtml(html, entrypoint) {
|
|
496
|
+
html = html.replace(/<script\s+type=["']importmap["'][^>]*>[\s\S]*?<\/script>/gi, '')
|
|
529
497
|
html = html.replace(/<script([^>]*)\bdata-entrypoint\b([^>]*)><\/script>/gi, '')
|
|
530
498
|
|
|
531
499
|
const entryUrl = '/' + entrypoint.replace(/^\.\//, '').replaceAll('\\', '/')
|
|
532
|
-
const importMapTag = renderImportMapTag(mergeImportMaps(generatedImportMap, extracted.importMap))
|
|
533
500
|
|
|
534
501
|
html = html.replace('</head>',
|
|
535
|
-
|
|
502
|
+
`\t\t<script type='module' src='${entryUrl}'></script>\n${hmrClient}\n\t</head>`
|
|
536
503
|
)
|
|
537
504
|
return html
|
|
538
505
|
}
|
|
@@ -545,7 +512,6 @@ export function serve(entrypoint, flags) {
|
|
|
545
512
|
const htmlDir = path.dirname(htmlPath)
|
|
546
513
|
const srcDir = path.dirname(entrypoint)
|
|
547
514
|
const sockets = new Set()
|
|
548
|
-
let generatedImportMap = null
|
|
549
515
|
|
|
550
516
|
// ── Status line (prints current compile result, fades out on success) ──────
|
|
551
517
|
|
|
@@ -679,8 +645,7 @@ export function serve(entrypoint, flags) {
|
|
|
679
645
|
if (pathname === '/' || pathname.endsWith('.html')) {
|
|
680
646
|
const htmlFile = pathname === '/' ? htmlPath : '.' + pathname
|
|
681
647
|
let html = await Bun.file(htmlFile).text()
|
|
682
|
-
|
|
683
|
-
return new Response(transformHtml(html, entrypoint, generatedImportMap), {
|
|
648
|
+
return new Response(transformHtml(html, entrypoint), {
|
|
684
649
|
headers: { 'Content-Type': 'text/html' },
|
|
685
650
|
})
|
|
686
651
|
}
|
|
@@ -713,8 +678,13 @@ export function serve(entrypoint, flags) {
|
|
|
713
678
|
// Without this, `import './styles.css'` inside an ESM package fails because
|
|
714
679
|
// the browser expects a JS module response, not raw CSS.
|
|
715
680
|
if (pathname.endsWith('.css')) {
|
|
716
|
-
const
|
|
717
|
-
|
|
681
|
+
const cssPath = resolveFileCandidate(path.join(htmlDir, pathname)) || resolveFileCandidate('.' + pathname)
|
|
682
|
+
const cssFile = cssPath ? Bun.file(cssPath) : null
|
|
683
|
+
if (cssFile && await cssFile.exists()) {
|
|
684
|
+
if (req.headers.get('sec-fetch-dest') === 'style') {
|
|
685
|
+
return new Response(cssFile, { headers: { 'Content-Type': 'text/css' } })
|
|
686
|
+
}
|
|
687
|
+
|
|
718
688
|
const css = await cssFile.text()
|
|
719
689
|
const id = JSON.stringify(pathname)
|
|
720
690
|
const js = [
|
|
@@ -727,6 +697,11 @@ export function serve(entrypoint, flags) {
|
|
|
727
697
|
}
|
|
728
698
|
}
|
|
729
699
|
|
|
700
|
+
if (!pathname.startsWith('/node_modules/') && (pathname.endsWith('.js') || pathname.endsWith('.mjs'))) {
|
|
701
|
+
const jsFile = resolveFileCandidate(path.join(htmlDir, pathname)) || resolveFileCandidate('.' + pathname)
|
|
702
|
+
if (jsFile) return serveJavaScriptFile(jsFile)
|
|
703
|
+
}
|
|
704
|
+
|
|
730
705
|
// Direct node_modules URLs (from user import maps or explicit imports)
|
|
731
706
|
// are bundled through Bun too, so browser/cjs/exports handling stays
|
|
732
707
|
// in one place.
|
|
@@ -763,18 +738,15 @@ export function serve(entrypoint, flags) {
|
|
|
763
738
|
if (!out.errors?.length) return new Response(out.js, { headers: { 'Content-Type': 'application/javascript' } })
|
|
764
739
|
}
|
|
765
740
|
for (const ext of ['.js', '.mjs']) {
|
|
766
|
-
const withExt =
|
|
767
|
-
if (
|
|
768
|
-
headers: { 'Content-Type': 'application/javascript' },
|
|
769
|
-
})
|
|
741
|
+
const withExt = '.' + pathname + ext
|
|
742
|
+
if (existsSync(withExt)) return serveJavaScriptFile(withExt)
|
|
770
743
|
}
|
|
771
744
|
}
|
|
772
745
|
|
|
773
746
|
// SPA fallback for extension-less paths
|
|
774
747
|
if (!lastSegment.includes('.')) {
|
|
775
748
|
let html = await Bun.file(htmlPath).text()
|
|
776
|
-
|
|
777
|
-
return new Response(transformHtml(html, entrypoint, generatedImportMap), {
|
|
749
|
+
return new Response(transformHtml(html, entrypoint), {
|
|
778
750
|
headers: { 'Content-Type': 'text/html' },
|
|
779
751
|
})
|
|
780
752
|
}
|