Skip to content

Commit

Permalink
feat: add output: 'deno' (#657)
Browse files Browse the repository at this point in the history
* docs: add deno docs

* fix: avoid warning action log in deno

* fix: change some vars to support deno
  • Loading branch information
aralroca authored Dec 1, 2024
1 parent f7e72d2 commit 32336a6
Show file tree
Hide file tree
Showing 36 changed files with 366 additions and 120 deletions.
28 changes: 14 additions & 14 deletions docs/api-reference/compiler-apis/compileWC.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ The `compileWC` function transpiles a JSX web component to be compatible with th

#### Returns

- The transpiled code of the web component.
- The transpiled code of the web component.

## Example

Expand All @@ -51,7 +51,7 @@ console.log(finalCode);

## Outside Bun.js

This function is intended to be used within the [Bun](https://bun.sh/) runtime, as it uses the [Bun transpiler](https://bun.sh/docs/api/transpiler) to convert TSX to JS. However, if you want to use it in other environments, such as Node.js or in the browser, you can do so, but you will need to transpile the TSX to JS beforehand, for example with [`@swc/wasm-web`](https://swc.rs/docs/usage/wasm).
This function is intended to be used within the [Bun](https://bun.sh/) runtime, as it uses the [Bun transpiler](https://bun.sh/docs/api/transpiler) to convert TSX to JS. However, if you want to use it in other environments, such as Node.js, Deno or in the browser, you can do so, but you will need to transpile the TSX to JS beforehand, for example with [`@swc/wasm-web`](https://swc.rs/docs/usage/wasm).

> [!IMPORTANT]
>
Expand All @@ -64,21 +64,21 @@ import { compileWC } from "brisa/compiler";
import initSwc, { transformSync } from "@swc/wasm-web";

async function main() {
await initSwc();
const code = `
await initSwc();
const code = `
export default function MyComponent() {
return <div>Hello World</div>;
}
`;
const transpiledCode = transformSync(code, {
jsc: {
parser: {
syntax: "typescript",
tsx: true,
},
},
});
const finalCode = compileWC(transpiledCode.code);
console.log(finalCode);
const transpiledCode = transformSync(code, {
jsc: {
parser: {
syntax: "typescript",
tsx: true,
},
},
});
const finalCode = compileWC(transpiledCode.code);
console.log(finalCode);
}
```
20 changes: 9 additions & 11 deletions docs/api-reference/server-apis/fileSystemRouter.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pages

The API of `fileSystemRouter` is the same API of [`Bun.FileSystemRouter`](https://bun.sh/docs/api/file-system-router) with some differences:

- You can also use the `fileSystemRouter` API in Node.js.
- You can also use the `fileSystemRouter` API in Node.js or Deno.
- There are some fixes and improvements in the API.
- The `fileSystemRouter` API (with Bun runtime) is faster than the `Bun.FileSystemRouter` API.

Expand Down Expand Up @@ -47,17 +47,17 @@ The `fileSystemRouter` function creates a new instance of the file system router
## Example usage

```tsx
import path from 'node:path';
import { fileSystemRouter } from 'brisa/server';
import path from "node:path";
import { fileSystemRouter } from "brisa/server";

const router = fileSystemRouter({
dir: path.join(import.meta.dirname, 'pages'),
dir: path.join(import.meta.dirname, "pages"),
});

const matchedRoute = router.match('/blog/hello-world?foo=bar');
const matchedRoute = router.match("/blog/hello-world?foo=bar");

if (matchedRoute) {
console.log(matchedRoute);
console.log(matchedRoute);
// {
// filePath: 'pages/blog/[slug].tsx',
// kind: 'dynamic',
Expand Down Expand Up @@ -95,11 +95,11 @@ The `routes` property is the entries of routes and file paths.
> `routes` entries are in alphabetical order
```tsx
import path from 'node:path';
import { fileSystemRouter } from 'brisa/server';
import path from "node:path";
import { fileSystemRouter } from "brisa/server";

const router = fileSystemRouter({
dir: path.join(import.meta.dirname, 'pages'),
dir: path.join(import.meta.dirname, "pages"),
});

console.log(router.routes);
Expand All @@ -111,5 +111,3 @@ console.log(router.routes);
// ['/settings', '/Users/aralroca/my-app/src/pages/settings.tsx'],
// ]
```


49 changes: 49 additions & 0 deletions docs/building-your-application/building/deno-server.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
---
description: Learn how build a Deno Web Service App in Brisa
---

# Deno Server

Brisa enables starting as a [Deno](https://deno.com/) Server to serve your app by changing the `output` to `deno`. It generates a [Deno](https://deno.com/) server that serves your application on the port 3000 by default, it can be changed with the flag `--port`.

This server is capable of serving your application with all the features that Brisa offers, such as i18n, routing, server actions and middleware.

> [!NOTE]
>
> You need a different `output` type than `bun` since during the build your application is optimized to be served on a Deno server. This means that we use `Deno.serve` and you can use `deno.json` at the root of your project to configure your Deno server and it will be moved inside the build folder.
## Configuration _(Optional)_

To enable a web service application, change the output mode inside [`brisa.config.ts`](/building-your-application/configuring/brisa-config-js):

```ts filename="brisa.config.ts"
import type { Configuration } from "brisa";

export default {
output: "deno",
} satisfies Configuration;
```

After running `brisa build`, Brisa will generate a Deno server that serves your application on the port 3000 by default.

## Changing the port

To change the port, you can use the flag `--port`:

```sh
brisa start --port 8080
```

> [!NOTE]
>
> The default port is `process.env.PORT` or `3000`.
After running `brisa build`, Brisa will generate a Bun server that serves your application on the port 8080.

> [!TIP]
>
> Although you can still use the Bun tooling to start your application in Deno, if you want, you can use `NODE_ENV=production deno build/server.js` to start your application with Deno without Brisa CLI.
## Custom server

If you want to use a custom server, you can follow this guide: [Custom Server](/building-your-application/configuring/custom-server#custom-server).
13 changes: 8 additions & 5 deletions docs/building-your-application/building/index.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
---
description: Learn how to build your Brisa app to production
prev:
text: 'Integrating Tailwind CSS'
link: '/building-your-application/integrations/tailwind-css'
text: "Integrating Tailwind CSS"
link: "/building-your-application/integrations/tailwind-css"
next:
text: 'Web Service App'
link: '/building-your-application/building/web-service-app'
text: "Web Service App"
link: "/building-your-application/building/web-service-app"
---

# Building
Expand Down Expand Up @@ -37,12 +37,15 @@ Brisa can be deployed to any hosting provider that supports Bun. Ensure your `pa

Then, run `bun run build` to build your application. Finally, run `bun run start` to start the Bun server. This server supports all Brisa features.

But if you need Node.js or Deno, we support that too, you just have to configure it. See the different build strategies in the next point.

## App Strategy (Static, Server, Desktop, Android, iOS)

Brisa supports multiple [output](/building-your-application/configuring/output) strategies to build your application. You can choose between:

- [Build a Bun Server App](/building-your-application/building/bun-server)
- [Build a Node.js Server App](/building-your-application/building/node-server)
- [Build a Deno Server App](/building-your-application/building/deno-server)
- [Build a Static Site App](/building-your-application/building/static-site-app)
- [Build a Desktop App](/building-your-application/building/desktop-app)
- [Build a Android App](/building-your-application/building/android-app)
Expand All @@ -52,4 +55,4 @@ Brisa supports multiple [output](/building-your-application/configuring/output)

Brisa is more than a framework; it is a **Web Component Compiler**. You can create web components using Brisa and build them to create a library.

- [Web Component Compiler](/building-your-application/building/web-component-compiler)
- [Web Component Compiler](/building-your-application/building/web-component-compiler)
20 changes: 18 additions & 2 deletions docs/building-your-application/configuring/output.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ description: Learn how to build with different types of outputs

# Output

Brisa, offers versatile output configuration options to tailor your build output according to your deployment needs. The `output` configuration property in `brisa.config.ts` allows you to define the type of output you desire, with options such as `server`, `static`, and `desktop`.
Brisa, offers versatile output configuration options to tailor your build output according to your deployment needs. The `output` configuration property in `brisa.config.ts` allows you to define the type of output you desire, with options such as `bun`, `node`, `deno`, `static`, `desktop`, `android` and `ios`.

## Understanding Output Types

Expand Down Expand Up @@ -41,6 +41,22 @@ export default {
>
> You can use specific Bun.js features only related with building the application, like Macros, but not runtime features like [`bun:ffi`](/building-your-application/configuring/zig-rust-c-files), for that you need to find the equivalent in Node.js.
### 2. Deno Server Output (`deno`)

The `deno` output type is designed for creating deployable Deno applications. To configure your Brisa project for Deno server output, adjust your `brisa.config.ts` as follows:

```ts
import type { Configuration } from "brisa";

export default {
output: "deno",
} satisfies Configuration;
```

> [!NOTE]
>
> You can use specific Bun.js features only related with building the application, like Macros, but not runtime features like [`bun:ffi`](/building-your-application/configuring/zig-rust-c-files), for that you need to find the equivalent in Deno.
### 3. Static Output (`static`)

The `static` output type creates a static export suitable for deployment to static hosting services. To configure your Brisa project for static output, adjust your `brisa.config.ts` as follows:
Expand Down Expand Up @@ -158,4 +174,4 @@ To view the changes in the output, run the `brisa build` command. In the case of
> [!TIP]
>
> If your application requires server-side features, like middleware or API endpoints, opt for the `server` output. For static websites, select the `static` output, and for desktop applications integrated with Tauri, use the `desktop` output.
> If your application requires server-side features, like middleware or API endpoints, opt for the `bun` / `node` / `deno` output. For static websites, select the `static` output, and for desktop / mobile applications integrated with Tauri, use the `desktop` / `ios` / `android` output.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub extern "C" fn add(a: isize, b: isize) -> isize {

> [!CAUTION]
>
> If you are using `node` as output, this documentation does not apply. You need to find the equivalent in Node.js.
> If you are using `node` or `deno` as output, this documentation does not apply. You need to find the equivalent in the specific JS runtime.
## Compile Your Files

Expand Down
26 changes: 13 additions & 13 deletions docs/building-your-application/routing/websockets.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Brisa supports server-side WebSockets, with on-the-fly compression, TLS support,
> [!CAUTION]
>
> This WebSockets documentation **only works** with the **Bun.js runtime**. If you want to use Node.js as output you will have to implement it with some library in this same file, [look here how to do it](#websockets-in-node-js).
> This WebSockets documentation **only works** with the **Bun.js runtime**. If you want to use Node.js or Deno as output you will have to implement it with some library in this same file, [look here how to do it](#websockets-in-node-js).
## Start a WebSocket server API

Expand Down Expand Up @@ -250,37 +250,37 @@ ws.close();
ws.close(1000, "Closing connection gracefully");
```

## WebSockets in Node.js
## WebSockets in other JS runtimes (Node / Deno)

If you want to use WebSockets in a Node.js environment, you can use the [ws](https://github.com/websockets/ws) library. Here is an example of how to create a WebSocket server using the `ws` library in the `src/websocket.ts` file:
If you want to use WebSockets in a Node.js or Deno environment, you can use the [ws](https://github.com/websockets/ws) library. Here is an example of how to create a WebSocket server using the `ws` library in the `src/websocket.ts` file:

**`src/websocket.ts`**

```ts
const WebSocket = require('ws');
const WebSocket = require("ws");

// Create a WebSocket server
const wss = new WebSocket.Server({ port: 8080 });

wss.on('connection', function connection(ws) {
wss.on("connection", function connection(ws) {
// Log a message when a new client connects
console.log('A new client connected!');
console.log("A new client connected!");

// Receive messages from the client
ws.on('message', function incoming(message) {
console.log('Received:', message);
ws.on("message", function incoming(message) {
console.log("Received:", message);

// Send a response back to the client
ws.send(`You said: ${message}`);
});

// Log a message when the client disconnects
ws.on('close', () => {
console.log('Client has disconnected');
ws.on("close", () => {
console.log("Client has disconnected");
});
});
```

> [!NOTE]
>
> This `src/websocket.ts` file is loaded once when the server is created, if the module exports the `attach`, `message`, `open`, `close`, and `drain` functions, they will only be used in Bun.js, but when the file is executed in Node.js, the `ws` functions can be used to create a WebSocket server in Node.js.
> This `src/websocket.ts` file is loaded once when the server is created, if the module exports the `attach`, `message`, `open`, `close`, and `drain` functions, they will only be used in Bun.js, but when the file is executed in other JS runtimes, the `ws` functions can be used to create a WebSocket server.
8 changes: 4 additions & 4 deletions examples/with-api-routes/src/pages/about/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,10 @@ export default function About() {
<p>
Bun is great and improves the development experience a lot. Although
we recommend Bun.js also as runtime, as output you can use Node.js
if you want, generate a static output and upload it to a CDN, or
generate an executable app for Android (.apk),  iOS (.ipa),  Windows
(.exe), Mac (.dmg), or Linux (.deb). Yes, Brisa is multiplatform
thanks to its integration with Tauri.
or Deno if you want, generate a static output and upload it to a
CDN, or generate an executable app for Android (.apk),  iOS (.ipa), 
Windows (.exe), Mac (.dmg), or Linux (.deb). Yes, Brisa is
multiplatform thanks to its integration with Tauri.
</p>

<p>
Expand Down
8 changes: 4 additions & 4 deletions examples/with-elysia/src/pages/about/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,10 @@ export default function About() {
<p>
Bun is great and improves the development experience a lot. Although
we recommend Bun.js also as runtime, as output you can use Node.js
if you want, generate a static output and upload it to a CDN, or
generate an executable app for Android (.apk),  iOS (.ipa),  Windows
(.exe), Mac (.dmg), or Linux (.deb). Yes, Brisa is multiplatform
thanks to its integration with Tauri.
or Deno if you want, generate a static output and upload it to a
CDN, or generate an executable app for Android (.apk),  iOS (.ipa), 
Windows (.exe), Mac (.dmg), or Linux (.deb). Yes, Brisa is
multiplatform thanks to its integration with Tauri.
</p>

<p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,10 @@ export default function About() {
<p>
Bun is great and improves the development experience a lot. Although
we recommend Bun.js also as runtime, as output you can use Node.js
if you want, generate a static output and upload it to a CDN, or
generate an executable app for Android (.apk),  iOS (.ipa),  Windows
(.exe), Mac (.dmg), or Linux (.deb). Yes, Brisa is multiplatform
thanks to its integration with Tauri.
or Deno if you want, generate a static output and upload it to a
CDN, or generate an executable app for Android (.apk),  iOS (.ipa), 
Windows (.exe), Mac (.dmg), or Linux (.deb). Yes, Brisa is
multiplatform thanks to its integration with Tauri.
</p>

<p>
Expand Down
2 changes: 1 addition & 1 deletion examples/with-i18n/src/i18n/messages/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export default {
'Brisa not only uses Hypermedia, it streams it over the wire.',
'Brisa is also the only framework with full Internationalization support. not only routing, but you can also translate your pages and the URL pathnames if you need it. If you use i18n, the server components are 0 Bytes, but in Web Components are 800 B. At the end we use the Web Platform; we make a bridge with the ECMAScript Intl API to make it easier for you to translate your Web Components.',
"In Brisa we like the idea that all the tooling is integrated, that's why Brisa is made with Bun we have extended the Matchers and added an API so you can run with Bun the tests of your Components.",
'Bun is great and improves the development experience a lot. Although we recommend Bun.js also as runtime, as output you can use Node.js if you want, generate a static output and upload it to a CDN, or generate an executable app for Android (.apk),  iOS (.ipa),  Windows (.exe), Mac (.dmg), or Linux (.deb). Yes, Brisa is multiplatform thanks to its integration with Tauri.',
'Bun is great and improves the development experience a lot. Although we recommend Bun.js also as runtime, as output you can use Node.js or Deno if you want, generate a static output and upload it to a CDN, or generate an executable app for Android (.apk),  iOS (.ipa),  Windows (.exe), Mac (.dmg), or Linux (.deb). Yes, Brisa is multiplatform thanks to its integration with Tauri.',
'We support Partial Prerendering, you can prerender only a part of your page, such as the footer.',
'We also support many more features like middleware, layouts, WebSockets, API routes, suspense, etc.',
'Brisa is the future of the Web, and the future is now. We invite you to try it and contribute to the community.',
Expand Down
2 changes: 1 addition & 1 deletion examples/with-i18n/src/i18n/messages/es.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export default {
'Brisa no solo usa Hypermedia, sino que la transmite por la red.',
'Brisa también es el único framework con soporte completo de Internacionalización. No solo en el enrutamiento, sino que también puedes traducir tus páginas y los nombres de las rutas si lo necesitas. Si usas i18n, los componentes del servidor ocupan 0 Bytes, pero en los Web Components son 800 B. Al final, usamos la Plataforma Web; hacemos un puente con la API Intl de ECMAScript para facilitar la traducción de tus Web Components.',
'En Brisa nos gusta la idea de que todas las herramientas estén integradas, por eso Brisa está hecho con Bun. Hemos ampliado los Matchers y añadido una API para que puedas ejecutar con Bun los tests de tus Componentes.',
'Bun es genial y mejora mucho la experiencia de desarrollo. Aunque recomendamos Bun.js también como runtime, como salida puedes usar Node.js si lo prefieres, generar una salida estática y subirla a un CDN, o generar una app ejecutable para Android (.apk), iOS (.ipa), Windows (.exe), Mac (.dmg), o Linux (.deb). Sí, Brisa es multiplataforma gracias a su integración con Tauri.',
'Bun es genial y mejora mucho la experiencia de desarrollo. Aunque recomendamos Bun.js también como runtime, como salida puedes usar Node.js o Deno si lo prefieres, generar una salida estática y subirla a un CDN, o generar una app ejecutable para Android (.apk), iOS (.ipa), Windows (.exe), Mac (.dmg), o Linux (.deb). Sí, Brisa es multiplataforma gracias a su integración con Tauri.',
'Soportamos Pre-renderizado Parcial, puedes pre-renderizar solo una parte de tu página, como el pie de página.',
'También soportamos muchas más características como middleware, layouts, WebSockets, rutas API, suspense, etc.',
'Brisa es el futuro de la Web, y el futuro es ahora. Te invitamos a probarlo y a contribuir a la comunidad.',
Expand Down
Loading

0 comments on commit 32336a6

Please sign in to comment.