YES! You can now serve .stx files directly, similar to how you can with JSX/TSX files.
# Serve all .stx files in a directory
bun-plugin-stx serve pages/*.stx
# Serve specific files
bun-plugin-stx serve index.stx about.stx
# Serve a directory
bun-plugin-stx serve pages/
# Custom port
bun-plugin-stx serve pages/ --port 3000How it works:
- Automatically discovers and builds your .stx files
- Starts a development server
- Smart routing:
home.stx→/,about.stx→/about - Live 404 page with navigation
- Create
bunfig.tomlin your project:
preload = ["bun-plugin-stx/preload"]- Now you can import
.stxfiles in your code:
// server.ts
import homeContent from './pages/home.stx'
import aboutContent from './pages/about.stx'
Bun.serve({
port: 3000,
fetch(req) {
const url = new URL(req.url)
if (url.pathname === '/') {
return new Response(homeContent, {
headers: { 'Content-Type': 'text/html' }
})
}
if (url.pathname === '/about') {
return new Response(aboutContent, {
headers: { 'Content-Type': 'text/html' }
})
}
return new Response('Not Found', { status: 404 })
}
})Then run:
bun run server.ts# Serve with preload
bun --preload bun-plugin-stx/preload your-server.ts
# Or use bunfig.toml and just:
bun your-server.tscd demo-project
bun-plugin-stx serve pages/*.stxOutput:
🚀 Starting stx development server...
📄 Found 3 .stx files:
- pages/home.stx
- pages/about.stx
- pages/contact.stx
🔨 Building...
✅ Build complete
🌐 Server running at: http://localhost:3456
📚 Available routes:
http://localhost:3456/
http://localhost:3456/about
http://localhost:3456/contact
// serve.ts
import { Glob } from 'bun'
// Auto-discover all .stx files
const glob = new Glob('pages/**/*.stx')
const files = await Array.fromAsync(glob.scan('.'))
// Build route map
const routes = new Map()
for (const file of files) {
const name = file.replace('pages/', '').replace('.stx', '')
const route = name === 'home' ? '/' : `/${name}`
// Import the .stx file (plugin processes it)
const content = await import(`./${file}`)
routes.set(route, content.default)
}
// Serve
Bun.serve({
port: 3456,
fetch(req) {
const url = new URL(req.url)
const content = routes.get(url.pathname)
if (content) {
return new Response(content, {
headers: { 'Content-Type': 'text/html' }
})
}
return new Response('404', { status: 404 })
}
})With bunfig.toml:
preload = ["bun-plugin-stx/preload"]Run it:
bun serve.tsThe absolute simplest way:
# In your project directory
echo 'preload = ["bun-plugin-stx/preload"]' > bunfig.toml
# Create a simple server
cat > serve.ts << 'EOF'
import home from './pages/home.stx'
Bun.serve({
fetch: () => new Response(home, {
headers: { 'Content-Type': 'text/html' }
})
})
EOF
# Run it
bun serve.ts# Bun can directly run JSX
bun app.tsx# Option 1: Use the CLI
bun-plugin-stx serve pages/*.stx
# Option 2: Use bunfig.toml preload
bun --preload bun-plugin-stx/preload app.ts
# Option 3: Configure bunfig.toml once, then:
bun app.ts # Just works!If you haven't already:
bun add bun-plugin-stxOr in a monorepo/workspace:
{
"dependencies": {
"bun-plugin-stx": "workspace:*"
}
}bun-plugin-stx serve pages/ --port 8080The CLI doesn't have watch mode yet, but you can use Bun's --watch:
bun --watch serve.tsCreate type declarations for .stx files:
// stx.d.ts
declare module '*.stx' {
const content: string
export default content
}Add to tsconfig.json:
{
"include": ["**/*.ts", "**/*.stx", "stx.d.ts"]
}Now TypeScript won't complain about .stx imports!
import express from 'express'
import homeContent from './pages/home.stx'
const app = express()
app.get('/', (req, res) => {
res.type('html').send(homeContent)
})
app.listen(3000)✅ Direct serving - No manual build step needed ✅ Import .stx files - Use them like any other module ✅ Hot reloading - Changes reflect immediately (with --watch) ✅ Simple deployment - One command to serve your site ✅ Development speed - Fast iteration with Bun's speed ✅ Type safety - TypeScript support with declarations
"Command not found: bun-plugin-stx"
Make sure the plugin is installed and built:
cd packages/bun-plugin
bun run buildOr use the full path:
bun packages/bun-plugin/dist/serve.js pages/*.stx"Cannot find module"
Make sure bunfig.toml is in your project root with the preload configured.
".stx files not processing"
Verify the plugin is loaded:
bun --preload bun-plugin-stx/preload -e "console.log('Plugin loaded')"You should see: ✅ stx plugin loaded - .stx files can now be imported
See the demo-project/ directory for a complete working example with:
- Multiple pages
- Navigation
- Styled components
- Route handling
Run it:
cd demo-project
bun-plugin-stx serve pages/*.stxSummary: You can now serve .stx files directly using bun-plugin-stx serve or by configuring bunfig.toml with the preload option! 🎉