-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
195 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
import { rm, stat } from "node:fs/promises"; | ||
import type { ServeOptions } from "bun"; | ||
import * as path from "path"; | ||
|
||
import cosmosConfig from "./cosmos.config.json"; | ||
|
||
const PROJECT_ROOT = import.meta.dir; | ||
const BUILD_DIR = path.resolve(PROJECT_ROOT, "build"); | ||
|
||
const waitForCosmosImports = async () => { | ||
const fpath = `${PROJECT_ROOT}/cosmos.imports.ts`; | ||
try { | ||
const cosmosImports = await stat(fpath); | ||
if (!cosmosImports.isFile()) { | ||
throw new Error(` | ||
file doesnt exist yet | ||
`); | ||
} | ||
} catch { | ||
return new Promise((resolve) => { | ||
setTimeout(() => resolve(waitForCosmosImports()), 1000); | ||
}); | ||
} | ||
}; | ||
|
||
const buildApp = async () => | ||
rm(BUILD_DIR, { force: true, recursive: true }).then(() => | ||
Bun.build({ | ||
entrypoints: ["./cosmos.entrypoint.tsx"], | ||
target: "browser", | ||
outdir: "build", | ||
}) | ||
.then((output) => output) | ||
.catch((e) => { | ||
console.info("\n\n error in build", e); | ||
}) | ||
); | ||
|
||
await waitForCosmosImports(); | ||
await buildApp().then((output) => { | ||
if (output.success) | ||
console.info( | ||
`app built: ${output.success}; ${output.outputs.length} files ` | ||
); | ||
else { | ||
for (const message of output.logs) { | ||
// Bun will pretty print the message object | ||
console.error(message); | ||
} | ||
throw new Error(`build failed`); | ||
} | ||
}); | ||
|
||
const returnIndex = () => { | ||
const index = ` | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<body> | ||
<script src="${BUILD_DIR}/cosmos.entrypoint.js" type="module"> | ||
</script> | ||
</body> | ||
</html> | ||
`; | ||
|
||
return new Response(index, { | ||
headers: { | ||
"Content-Type": "text/html", | ||
"Access-Control-Allow-Origin": "*", | ||
}, | ||
}); | ||
}; | ||
|
||
async function serveFromDir(config: { | ||
directory?: string; | ||
path: string; | ||
}): Promise<Response | null> { | ||
const filepath = path.join(config.directory || "", config.path); | ||
|
||
try { | ||
const fd = await stat(filepath); | ||
if (fd && fd.isFile()) { | ||
return new Response(Bun.file(filepath), { | ||
headers: { "Access-Control-Allow-Origin": "*" }, | ||
}); | ||
} | ||
} catch (err) {} | ||
|
||
return null; | ||
} | ||
export default { | ||
port: cosmosConfig.rendererUrl.split(":").pop(), | ||
hostname: "0.0.0.0", | ||
async fetch(req) { | ||
const reqPath = new URL(req.url).pathname; | ||
console.log(req.method, reqPath); | ||
|
||
if (reqPath === "/") return returnIndex(); | ||
else { | ||
const filepath = req.url.replace(cosmosConfig.rendererUrl, ""); | ||
|
||
const exactResponse = await serveFromDir({ path: filepath }); | ||
if (exactResponse) return exactResponse; | ||
|
||
const buildResponse = await serveFromDir({ | ||
directory: BUILD_DIR, | ||
path: filepath, | ||
}); | ||
if (buildResponse) return buildResponse; | ||
|
||
return new Response("File not found", { | ||
status: 404, | ||
}); | ||
} | ||
}, | ||
} satisfies ServeOptions; | ||
|
||
// watch imports | ||
await import("./cosmos.imports.ts").catch((e) => e); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"port": 5000, | ||
"rendererUrl": "http://0.0.0.0:5050" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import { mountDomRenderer } from "react-cosmos-dom"; | ||
import * as mountArgs from "./cosmos.imports"; | ||
|
||
mountDomRenderer(mountArgs); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// This file is automatically generated by Cosmos. Add it to .gitignore and | ||
// only edit if you know what you're doing. | ||
|
||
import { RendererConfig, UserModuleWrappers } from 'react-cosmos-core'; | ||
|
||
import * as fixture0 from './src/fixtures/example.fixture'; | ||
|
||
export const rendererConfig: RendererConfig = { | ||
"playgroundUrl": "http://localhost:5000", | ||
"rendererUrl": "http://0.0.0.0:5050" | ||
}; | ||
|
||
const fixtures = { | ||
'src/fixtures/example.fixture.tsx': { module: fixture0 } | ||
}; | ||
|
||
const decorators = {}; | ||
|
||
export const moduleWrappers: UserModuleWrappers = { | ||
lazy: false, | ||
fixtures, | ||
decorators | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { ReactFP, FPContainer, FPItem } from "src/index"; | ||
|
||
export default function App() { | ||
return ( | ||
<ReactFP> | ||
<FPContainer> | ||
<FPItem | ||
style={{ | ||
backgroundColor: "lime", | ||
height: "80vh", // defaults to 100vh | ||
padding: "1em", | ||
}} | ||
> | ||
1 | ||
</FPItem> | ||
|
||
<FPItem | ||
style={{ | ||
backgroundColor: "coral", | ||
padding: "1em", | ||
}} | ||
> | ||
2 | ||
</FPItem> | ||
|
||
<FPItem | ||
style={{ | ||
backgroundColor: "firebrick", | ||
padding: "1em", | ||
}} | ||
> | ||
3 | ||
</FPItem> | ||
</FPContainer> | ||
</ReactFP> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters