bimba-cli 0.7.1 → 0.7.2
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/package.json +1 -1
- package/serve.js +34 -23
package/package.json
CHANGED
package/serve.js
CHANGED
|
@@ -524,7 +524,9 @@ function resolveEntry(depPkg) {
|
|
|
524
524
|
}
|
|
525
525
|
|
|
526
526
|
// Build an ES import map from package.json dependencies.
|
|
527
|
-
//
|
|
527
|
+
// The import map is intentionally simple — it just maps bare specifiers
|
|
528
|
+
// to /node_modules/ URLs. All the smart resolution (conditional exports,
|
|
529
|
+
// CJS→ESM, entry points, extensions) happens on the server side.
|
|
528
530
|
async function buildImportMap() {
|
|
529
531
|
const imports = {
|
|
530
532
|
'imba/runtime': 'https://esm.sh/imba/runtime',
|
|
@@ -534,26 +536,8 @@ async function buildImportMap() {
|
|
|
534
536
|
const pkg = JSON.parse(await Bun.file('./package.json').text());
|
|
535
537
|
for (const [name] of Object.entries(pkg.dependencies || {})) {
|
|
536
538
|
if (name === 'imba') continue;
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
const entry = resolveEntry(depPkg);
|
|
540
|
-
if (entry && !entry.startsWith('http')) {
|
|
541
|
-
// Local package — serve from node_modules
|
|
542
|
-
imports[name] = `/node_modules/${name}/${entry}`;
|
|
543
|
-
// Always add trailing-slash mapping for deep/subpath imports
|
|
544
|
-
// (e.g. highlight.js/lib/languages/javascript). The browser
|
|
545
|
-
// doesn't enforce Node's `exports` restrictions, it just needs
|
|
546
|
-
// a URL prefix to resolve bare specifiers like "pkg/sub/path".
|
|
547
|
-
imports[name + '/'] = `/node_modules/${name}/`;
|
|
548
|
-
} else {
|
|
549
|
-
// No resolvable entry — use esm.sh to auto-wrap
|
|
550
|
-
imports[name] = `https://esm.sh/${name}`;
|
|
551
|
-
imports[name + '/'] = `https://esm.sh/${name}/`;
|
|
552
|
-
}
|
|
553
|
-
} catch(_) {
|
|
554
|
-
imports[name] = `https://esm.sh/${name}`;
|
|
555
|
-
imports[name + '/'] = `https://esm.sh/${name}/`;
|
|
556
|
-
}
|
|
539
|
+
imports[name] = `/node_modules/${name}/`;
|
|
540
|
+
imports[name + '/'] = `/node_modules/${name}/`;
|
|
557
541
|
}
|
|
558
542
|
} catch(_) { /* no package.json */ }
|
|
559
543
|
|
|
@@ -766,10 +750,37 @@ export function serve(entrypoint, flags) {
|
|
|
766
750
|
}
|
|
767
751
|
}
|
|
768
752
|
|
|
769
|
-
// node_modules:
|
|
770
|
-
//
|
|
753
|
+
// node_modules: all smart resolution happens here.
|
|
754
|
+
// The import map just maps bare specifiers to /node_modules/pkg/ URLs.
|
|
755
|
+
// This block handles: entry point resolution, conditional exports
|
|
756
|
+
// (CJS→ESM), subpath resolution, extensionless paths.
|
|
771
757
|
if (pathname.startsWith('/node_modules/')) {
|
|
772
758
|
const filepath = '.' + pathname
|
|
759
|
+
|
|
760
|
+
// Resolve entry point for root package requests (/node_modules/pkg/ or /node_modules/pkg)
|
|
761
|
+
const parts = pathname.slice(1).split('/') // ['node_modules', 'pkg', ...]
|
|
762
|
+
const isScoped = parts[1]?.startsWith('@')
|
|
763
|
+
const pkgParts = isScoped ? 3 : 2 // node_modules/@scope/pkg or node_modules/pkg
|
|
764
|
+
const subParts = parts.slice(pkgParts)
|
|
765
|
+
const isRootRequest = subParts.length === 0 || (subParts.length === 1 && subParts[0] === '')
|
|
766
|
+
|
|
767
|
+
if (isRootRequest) {
|
|
768
|
+
// Bare import: resolve entry point from package.json
|
|
769
|
+
const pkgDir = './' + parts.slice(0, pkgParts).join('/')
|
|
770
|
+
const depPkg = await readPkgJson(pkgDir)
|
|
771
|
+
if (depPkg) {
|
|
772
|
+
const entry = resolveEntry(depPkg)
|
|
773
|
+
if (entry) {
|
|
774
|
+
const entryPath = path.join(pkgDir, entry)
|
|
775
|
+
const file = Bun.file(entryPath)
|
|
776
|
+
if (await file.exists()) return new Response(file, {
|
|
777
|
+
headers: { 'Content-Type': 'application/javascript' },
|
|
778
|
+
})
|
|
779
|
+
}
|
|
780
|
+
}
|
|
781
|
+
}
|
|
782
|
+
|
|
783
|
+
// Subpath: resolve via conditional exports (CJS→ESM)
|
|
773
784
|
const esmPath = await resolveNodeModuleESM(filepath)
|
|
774
785
|
if (esmPath) {
|
|
775
786
|
const file = Bun.file(esmPath)
|