LuxelEspañol

Luxel v0.2 · Start

Getting started

Luxel is a framework for CWP (Compiled Web Projection: the interface is compiled once, the server fills only values, the browser receives no application code; defined in the README). This page takes you from nothing to a running Luxel app, then through one change of each kind: a view, a state path, and an event validated on the server. At the end you build, test and run the app the way production runs it.

You need Node.js 22.12 or later.

1. Create the app

The current release of Luxel is a beta, 0.1.0-beta.2. Create an app from it with npm:

npm create luxel@beta my-app

The framework's packages are installed from the npm registry in step 3.

create-luxel refuses a target directory that exists and is not empty. --name sets the package name (the default comes from the directory name).

2. Or: create it from a source checkout

Skip this step if you used npm create luxel@beta. When you work from a checkout of the Luxel repository (to try a change of the framework, for example), you pack its packages into tarballs and the new app installs them from there. Nothing of Luxel is downloaded from the registry.

cd ‹path-to-luxel> npm install npm run build # the tarballs carry the built dist/ npm run pack:local # writes .packs/luxel-framework-*.tgz and .packs/create-luxel-‹version>.tgz

npm run build also builds the comparison lab in examples/, which has its own dependencies (npm --prefix examples install). To build only the framework, run the first two steps of that script instead: npx tsc -b && npm run bundle -w @luxel-framework/runtime.

Then create the app with the checkout's create-luxel:

node packages/create-luxel/bin.mjs ../my-app --packages "$PWD/.packs"

Or, from anywhere, with the packed create-luxel:

npx --yes --package=‹path-to-luxel>/.packs/create-luxel-‹version>.tgz create-luxel my-app --packages ‹path-to-luxel>/.packs

3. Install and run

cd my-app # ../my-app if you created it from a checkout npm install npm run dev

Open http://localhost:4321. You land on the sign-in page. Sign in as demo with the password demo-password. This user exists only in development: there is none when LUXEL_ENV=production.

You now have:

  • a static sign-in page: the same page for everyone, served without a session;
  • a contacts page: a form validated on the server, a table paginated on the server, a remove action per row, and a sign-out button.

The project looks like this:

luxel.config.ts language, styles, variants, policies src/screens/login.screen.tsx the sign-in screen, at /sign-in (static, public) src/screens/contacts.screen.tsx the contacts screen, at / (signed in) server/bindings.mjs sessions, event store, scopes (object authorization) server/contacts.mjs the demo data (in memory) server/users.mjs the demo user store src/styles.css your stylesheet tests/app.test.mjs screen tests (no browser) tests/app.spec.mjs end-to-end test (a browser)

Each screen is one file, src/screens/‹view id>.screen.tsx. It declares, once, what the screen shows (its state), its messages, who may load it and send each event, the handlers, the view and its address. The build generates the server contract from it (Screens).

Both screens declare a clean path in their route: the contacts screen is / and the sign-in screen is /sign-in. A screen without a path is served at /‹variant>/‹view id>; the template has one variant, dom (Routes).

4. Edit a view and watch it reload

Open src/screens/contacts.screen.tsx and change the heading in its view:

‹Text id="title" as="h1">My contacts‹/Text>

Save. luxel dev rebuilds and reloads the page. You stay signed in. A screen file also holds the screen's handlers, so saving it starts a new host process (the only way Node loads new server code); in development the session is kept in a file and survives that. The demo contacts are in the process's memory and do not: add them again. A CSS change reloads the host in place and keeps both.

Now make a mistake on purpose. Replace the heading with ‹div>Contacts‹/div> and save. The page shows the compiler error, escaped, with its code:

src/screens/contacts.screen.tsx:… - error LUXEL-E004 (unknown-component): …

There are no HTML elements in Luxel views: Stack, Row, Text and the other components are the whole vocabulary. Undo the change and the page comes back.

5. Show a new value from the server

A view shows server state. Each value is declared in the screen's state, filled by its handlers, and bound in the view with an accessor. Let's add a line that says who is signed in. Everything is in src/screens/contacts.screen.tsx.

The declaration: add a welcome value and its message.

const s = screen({ state: { rows: collection({ key: 'id', fields: { name: text(), email: text(), added: text() } }), pages: collection({ key: 'id', fields: { label: text() } }), summary: text(), welcome: text(), }, messages: { // ...the existing messages welcome: 'Signed in as {0}.', }, });

The handlers: every handler of this screen answers with the values that contactPage returns. Add the new one there.

values: { // ...rows and pages, unchanged welcome: message.welcome(userId), summary: total === 0 ? message.empty() : message.summary(total, String(total), String(page), String(pages)), } satisfies typeof s.types.complete,

The view: bind it, under the heading.

‹Text bind={state.welcome} as="p" className="summary" />

Save. The page now shows "Signed in as u-demo.".

Three things were checked for you:

  • state.welcome is an accessor, not a string: a typo (state.welcom) is a TypeScript error in your editor, and a string there (bind="welcome") does not build (LUXEL-E061).
  • message.welcome(…) takes one argument per placeholder. Anything else is a TypeScript error, and so is a values object without welcome: satisfies typeof s.types.complete asks for every value of the state.
  • The name welcome never reaches the browser. Only the slot index does.

6. Add an input validated on the server

Let's add an optional phone number to each contact. The browser only sends what was typed; the server decides whether it is valid.

The declaration. Add the item field and the refusal message:

rows: collection({ key: 'id', fields: { name: text(), email: text(), phone: text(), added: text() } }),

phoneInvalid: 'Enter a phone number: 6 to 20 characters in total, digits or spaces, optionally starting with +.',

The event. An event declares its inputs, with their bounds. Add phone to the inputs of add, validate it and store it:

// 6 to 20 characters in total (the + counts): the same 20 as the input's maxLength below. const PHONE = /^(?=.{6,20}$)\+?[0-9 ]+$/;

add: s.event( { policy: allow(USER), inputs: { name: input({ maxLength: 60, required: true }), email: input({ maxLength: 254, required: true }), phone: input({ maxLength: 20 }), }, }, async ({ name, email, phone }, ctx) => { const cleanName = name.trim(); const cleanEmail = email.trim().toLowerCase(); const cleanPhone = phone.trim(); if (cleanName.length < 1 || cleanName.length > 60) return ctx.reject('VALIDATION', ctx.message.nameInvalid()); if (!EMAIL.test(cleanEmail)) return ctx.reject('VALIDATION', ctx.message.emailInvalid()); if (cleanPhone !== '' && !PHONE.test(cleanPhone)) return ctx.reject('VALIDATION', ctx.message.phoneInvalid()); contactOps.add(ctx.session.userId, { name: cleanName, email: cleanEmail, phone: cleanPhone, addedAt: Date.now() }); const p = contactPage(ctx.message, ctx.session.userId, 1); return ctx.update(p.values).success(ctx.message.added(cleanName)).version(p.version); }, ),

In contactPage, add the phone to each row. Give it a value even when it is empty: when a handler sets a row, every text field of the row is required.

rows: rows.map((c: { id: string; name: string; email: string; phone?: string; addedAt: number }) => ({ id: c.id, name: c.name, email: c.email, phone: c.phone ?? '', added: DAY.format(c.addedAt), })),

The view. Add a field to the form (it names the event's input, and its value goes with the form's action), clear it on success, and add a column:

‹Row as="form" className="form form--inline"> ‹Field for={events.add.inputs.name} label="Name" autoComplete="off" /> ‹Field for={events.add.inputs.email} label="Email" autoComplete="off" /> ‹Field for={events.add.inputs.phone} label="Phone" autoComplete="off" /> ‹Action id="add" event={events.add} status="result" busyLabel="Adding…" onSuccess={{ clear: [events.add.inputs.name, events.add.inputs.email, events.add.inputs.phone], focus: events.add.inputs.name }} className="button"> Add ‹/Action> ‹/Row>

‹Text id="h-phone" as="th">Phone‹/Text>

‹Text as="td">{c.phone}‹/Text>

Put the header after "Email" and the cell after {c.email}.

The data (server/contacts.mjs): keep the phone.

add(userId, { name, email, phone, addedAt }) { const book = bookOf(userId); const contact = { id: `c${randomBytes(6).toString('hex')}`, name, email, phone, addedAt }; // ...unchanged },

Save, and type abc as the phone. The status zone shows the server's message. A valid phone adds the contact and clears the three fields.

Notice what you did not write: no fetch, no loading state, no error state, no client validation, and no name twice: the input, the field and the handler's argument are one declaration. The busy label, the message in the status zone and the cleared fields are declared in the view and applied by the shared runtime.

7. Build and check

npm run build

This runs luxel build (compile every screen; any error stops the build) and luxel check (the policies of luxel.config.ts: JavaScript budget, collection limits, payload size, allowed events and components, exposure of secrets and server names, data class of public views, and ASEF = 0: no app code in the published files). Both print a code, a location and a suggestion for each problem.

8. Run the tests

npm test

This builds and runs luxel test: the screen tests in tests/app.test.mjs, without a browser. They start the real host on the build, open its pages, fill fields, press actions and read what the page shows: the sign-in, the server's validation, adding, paging and removing contacts, and sign-out (Testing screens).

The end-to-end test does the same story in a browser:

npx playwright install chromium npm run test:e2e

The test starts luxel-host on the build (run npm run build first) with a fresh key, and goes through the whole app: the static sign-in page has no cookie and is cacheable as public, a wrong password and an invalid email are refused by the server, contacts are added and paged, and sign-out returns to the sign-in page.

9. Run it as production does

npm run build LUXEL_ENV=production LUXEL_VIEW_KEYS=k1:$(head -c 32 /dev/urandom | base64) npm start

  • npm start runs luxel-host luxel.config.json. It listens on PORT (default 8080).
  • LUXEL_ENV=production makes the host refuse to start with an unsafe setup, for example without LUXEL_VIEW_KEYS. It never generates a key itself.
  • In production the key comes from your secret store, and it is the same on every node.
  • Put TLS in front: the session cookie is __Host-…; Secure.
  • There is no demo user in production. Replace server/users.mjs and server/contacts.mjs with your database.

Before you deploy more than one node, read Production and Sessions.

This beta has no deployment command: you deploy by hand, following Production. luxel deploy init (a Dockerfile, the list of settings) and luxel doctor (a production environment checked before start) arrive in a later beta.

Next

  • Concepts explains what happened at each step.
  • Views, Forms and Collections cover the components.
  • Screens covers the screen file: state, messages, policies, events and routes.
  • Server covers ctx, and the older form with defineLoad and defineEvent.
Edit this page on GitHub

This page: 7.9 KB of JavaScript (Luxel's shared runtime), 0 KB from your app.

Measured when the site is built: the runtime files this page loads, compressed as they are sent.

Made by IntiOps. The Luxel 0.1.0 betas are on npm (0.1.0-beta.1, 0.1.0-beta.2), with an alpha of 0.2 (0.2.0-alpha.1): no external security review yet.