Skip to content

Instalação e Setup

Terminal window
bun add @ezbug/slash
Terminal window
npm install @ezbug/slash
Terminal window
pnpm add @ezbug/slash
Terminal window
yarn add @ezbug/slash

Slash é TypeScript-first e requer TypeScript 5.0+. Configure seu tsconfig.json com as opções recomendadas:

{
"compilerOptions": {
"target": "ES2022",
"module": "ESNext",
"moduleResolution": "bundler",
"lib": ["ES2022", "DOM", "DOM.Iterable"],
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "preserve",
"jsxImportSource": "@ezbug/slash",
"types": ["bun-types"]
}
}
  • target: "ES2022": Slash utiliza features modernas do JavaScript
  • strict: true: Type safety completo
  • jsx: "preserve": Para uso com HTM (não é necessário transpilação JSX)
  • moduleResolution: "bundler": Recomendado para Bun e bundlers modernos

Estrutura mínima para uma SPA:

my-slash-app/
├── src/
│ ├── main.ts # Entry point
│ ├── App.ts # Root component
│ └── components/
│ └── Counter.ts
├── index.html
├── package.json
└── tsconfig.json
<!DOCTYPE html>
<html lang="pt-BR">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>My Slash App</title>
</head>
<body>
<div id="app"></div>
<script type="module" src="/src/main.ts"></script>
</body>
</html>
import { render } from '@ezbug/slash'
import { App } from './App'
const root = document.getElementById('app')
if (root) {
render(App(), root)
}
import { html } from '@ezbug/slash'
import { Counter } from './components/Counter'
export const App = () => html`
<div>
<h1>Welcome to Slash!</h1>
${Counter()}
</div>
`
import { html, createState } from '@ezbug/slash'
export const Counter = () => {
const count = createState(0)
return html`
<div>
<p>Count: ${count}</p>
<button onclick=${() => count.set(count.get() + 1)}>
Increment
</button>
</div>
`
}

Estrutura para aplicação com SSR:

my-slash-ssr/
├── src/
│ ├── server.ts # Server entry (Bun/Node)
│ ├── client.ts # Client entry (hydration)
│ ├── App.ts # Shared root component
│ └── components/
│ └── Counter.ts
├── public/
│ └── index.html
├── package.json
└── tsconfig.json
import { renderToString } from '@ezbug/slash'
import { App } from './App'
const server = Bun.serve({
port: 3000,
async fetch(req) {
const html = renderToString(App())
return new Response(`
<!DOCTYPE html>
<html lang="pt-BR">
<head>
<meta charset="UTF-8">
<title>My SSR App</title>
</head>
<body>
<div id="app">${html}</div>
<script type="module" src="/client.js"></script>
</body>
</html>
`, {
headers: { 'Content-Type': 'text/html' }
})
}
})
console.log(`Server running at http://localhost:${server.port}`)
import { render } from '@ezbug/slash'
import { App } from './App'
const root = document.getElementById('app')
if (root) {
// Hydrate existing DOM from server
render(App(), root)
}

Slash fornece templates prontos para uso:

Terminal window
# Clone o template
git clone https://github.com/ezbug/slash-spa my-app
cd my-app
# Instale dependências
bun install
# Execute em desenvolvimento
bun run dev
# Build para produção
bun run build

Localização no monorepo: packages/slash-spa

Terminal window
# Clone o template
git clone https://github.com/ezbug/slash-ssr my-ssr-app
cd my-ssr-app
# Instale dependências
bun install
# Execute servidor de desenvolvimento
bun run dev
# Build e serve em produção
bun run build
bun run start

Localização no monorepo: packages/slash-ssr

Bun tem suporte nativo para Slash através do export "bun" no package.json:

{
"exports": {
".": {
"bun": "./src/index.ts",
"import": "./dist/index.mjs"
}
}
}

Quando usar Bun como runtime, o source TypeScript é carregado diretamente sem build.

Terminal window
bun add -D vite
vite.config.ts
import { defineConfig } from 'vite'
export default defineConfig({
esbuild: {
jsxFactory: 'h',
jsxFragment: 'Fragment',
jsxInject: `import { h } from '@ezbug/slash'`
}
})
Terminal window
bun add -D esbuild
build.js
import * as esbuild from 'esbuild'
await esbuild.build({
entryPoints: ['src/main.ts'],
bundle: true,
outfile: 'dist/bundle.js',
format: 'esm',
target: 'es2022'
})

Crie um arquivo de teste para verificar se tudo está funcionando:

test.ts
import { h, html, createState, render } from '@ezbug/slash'
console.log('✅ Imports OK')
const state = createState(42)
console.log('✅ State created:', state.get())
const element = html`<div>Hello Slash!</div>`
console.log('✅ HTM working:', element)
const hElement = h('div', null, 'Hello from h()')
console.log('✅ Hyperscript working:', hElement)

Execute:

Terminal window
bun run test.ts

Saída esperada:

✅ Imports OK
✅ State created: 42
✅ HTM working: [object HTMLDivElement]
✅ Hyperscript working: [object HTMLDivElement]

Erro: Cannot find module ‘@ezbug/slash’

Section titled “Erro: Cannot find module ‘@ezbug/slash’”

Solução: Verifique se a instalação foi concluída:

Terminal window
bun install

Solução: Adicione "types": ["bun-types"] no tsconfig.json e rode:

Terminal window
bun install @types/bun --dev

Solução: Certifique-se de importar html de @ezbug/slash:

import { html } from '@ezbug/slash'

Solução: Use Bun para desenvolvimento (carrega TypeScript diretamente):

Terminal window
bun run src/main.ts

Agora que seu ambiente está configurado, aprenda a:

  1. Renderização Básica - Criar e renderizar elementos
  2. Sistema de Estado - Gerenciar estado reativo
  3. Componentes - Construir componentes reutilizáveis