Skip to content

A fast & powerful Bun templating engine. Laravel Blade-like.

License

Notifications You must be signed in to change notification settings

stacksjs/bun-plugin-stx

Repository files navigation

Social Card of Bun Plugin blade

npm version GitHub Actions Commitizen friendly npm downloads

bun-plugin-stx

A Blade-like template engine plugin for Bun, enabling simple and powerful templating with .stx files.

Features

  • 🦋 Laravel Blade-like syntax
  • 🚀 Fast and lightweight
  • 📦 Zero config

Installation

bun add bun-plugin-stx

Setup

Add the plugin to your bunfig.toml:

preload = [ "bun-plugin-stx" ]

# or as a serve plugin
[serve.static]
plugins = [ "bun-plugin-stx" ]

Or register the plugin in your build script:

import { build } from 'bun'
import stxPlugin from 'bun-plugin-stx'

await build({
  entrypoints: ['./src/index.ts', './templates/home.stx'],
  outdir: './dist',
  plugins: [stxPlugin],
})

Usage with ESM

1. Configure Bun to use the plugin

In your build script or Bun configuration:

// build.js
import { build } from 'bun'
import stxPlugin from 'bun-plugin-stx'

await build({
  entrypoints: ['./src/index.ts', './templates/home.stx'],
  outdir: './dist',
  plugins: [stxPlugin],
})

2. Import and use .stx files directly

You can import .stx files directly in your ESM code:

// app.js
import homeTemplate from './templates/home.stx'

// Use the processed HTML content
document.body.innerHTML = homeTemplate

3. Use with Bun's server

You can serve .stx files directly with Bun's server:

// server.js
import { serve } from 'bun'
import homeTemplate from './home.stx'

serve({
  port: 3000,
  fetch(req) {
    return new Response(homeTemplate, {
      headers: { 'Content-Type': 'text/html' }
    })
  }
})

Or use as route handlers:

import about from './about.stx'
// server.js
import home from './home.stx'

export default {
  port: 3000,
  routes: {
    '/': home,
    '/about': about
  }
}

STX Template Syntax

STX templates use a syntax inspired by Laravel Blade. Templates can contain HTML with special directives for rendering dynamic content.

Basic Example

<!DOCTYPE html>
<html>
<head>
  <title>STX Example</title>
  <script>
    // Define your data as an ESM export
    export const title = "Hello World";
    export const items = ["Apple", "Banana", "Cherry"];
    export const showFooter = true;
  </script>
</head>
<body>
  <h1>{{ title }}</h1>

  <ul>
    @foreach (items as item)
      <li>{{ item }}</li>
    @endforeach
  </ul>

  @if (showFooter)
    <footer>Copyright 2023</footer>
  @endif
</body>
</html>

Data Export Options

There are two ways to expose data in your STX templates:

1. ESM exports (recommended)

<script>
  // Modern ESM named exports
  export const title = "Hello World";
  export const count = 42;

  // Export functions
  export function getFullName(first, last) {
    return `${first} ${last}`;
  }

  // Export default object
  export default {
    items: ["Apple", "Banana", "Cherry"],
    showDetails: true
  };
</script>

2. Legacy CommonJS (module.exports)

<script>
  // Legacy CommonJS exports
  module.exports = {
    title: "Hello World",
    items: ["Apple", "Banana", "Cherry"],
    showFooter: true
  };
</script>

Template Directives

Variables

Display content with double curly braces:

<h1>{{ title }}</h1>
<p>{{ user.name }}</p>

Conditionals

Use @if, @elseif, and @else for conditional rendering:

@if (user.isAdmin)
  <div class="admin-panel">Admin content</div>
@elseif (user.isEditor)
  <div class="editor-tools">Editor tools</div>
@else
  <div class="user-view">Regular user view</div>
@endif

Loops

Iterate over arrays with @foreach:

<ul>
  @foreach (items as item)
    <li>{{ item }}</li>
  @endforeach
</ul>

Use @for for numeric loops:

<ol>
  @for (let i = 1; i <= 5; i++)
    <li>Item {{ i }}</li>
  @endfor
</ol>

Raw HTML

Output unescaped HTML content:

{!! rawHtmlContent !!}

TypeScript Support

STX includes TypeScript declarations for importing .stx files. Make sure your tsconfig.json includes the necessary configuration:

{
  "compilerOptions": {
    // ... your other options
    "types": ["bun"]
  },
  "files": ["src/stx.d.ts"],
  "include": ["**/*.ts", "**/*.d.ts", "*.stx", "./**/*.stx"]
}

Create a declaration file (src/stx.d.ts):

// Allow importing .stx files
declare module '*.stx';

Example Server

Run a development server with your STX templates:

// serve.ts
import home from './home.stx'

const server = Bun.serve({
  routes: {
    '/': home,
  },
  development: true,

  fetch(req) {
    return new Response('Not Found', { status: 404 })
  },
})

console.log(`Listening on ${server.url}`)

Testing This Plugin

To test the plugin with the included examples:

  1. Build the test file:
bun run test-build.ts
  1. Run the test server:
bun run serve-test.ts
  1. Open your browser to the displayed URL (typically http://localhost:3000).

How It Works

The plugin works by:

  1. Extracting script tags from .stx files
  2. Creating an execution context with variables from the script
  3. Processing Blade-like directives (@if, @foreach, etc.) into HTML
  4. Processing variable tags ({{ var }}) with their values
  5. Returning the processed HTML content

Testing

bun test

Changelog

Please see our releases page for more information on what has changed recently.

Contributing

Please review the Contributing Guide for details.

Community

For help, discussion about best practices, or any other conversation that would benefit from being searchable:

Discussions on GitHub

For casual chit-chat with others using this package:

Join the Stacks Discord Server

Postcardware

You will always be free to use any of the Stacks OSS software. We would also love to see which parts of the world Stacks ends up in. Receiving postcards makes us happy—and we will publish them on our website.

Our address: Stacks.js, 12665 Village Ln #2306, Playa Vista, CA 90094, United States 🌎

Sponsors

We would like to extend our thanks to the following sponsors for funding Stacks development. If you are interested in becoming a sponsor, please reach out to us.

Credits

Many thanks to the following core technologies & people who have contributed to this package:

License

The MIT License (MIT). Please see LICENSE for more information.

Made with 💙

About

A fast & powerful Bun templating engine. Laravel Blade-like.

Topics

Resources

License

Code of conduct

Security policy

Stars

Watchers

Forks

Releases

No releases published

Sponsor this project