Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support Node.js as output #441

Merged
merged 47 commits into from
Aug 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
80bfb01
feat: change 'server' to 'bun' and 'node'
aralroca Aug 28, 2024
54ac4dc
docs: improve documentation node
aralroca Aug 28, 2024
f7f1004
docs: fix some docs
aralroca Aug 28, 2024
b4abc04
feat: build with target node when is output=node
aralroca Aug 28, 2024
3b2fb0b
feat: accept process.env.PORT
aralroca Aug 28, 2024
8236acf
feat: add brisa/server/node handler and serve
aralroca Aug 28, 2024
76ce34f
feat: add info about custom server with Node.js
aralroca Aug 28, 2024
dec3b8b
refactor: move serve and handler inside cli/serve
aralroca Aug 29, 2024
a64861d
feat: improve structure
aralroca Aug 29, 2024
4dbb66d
docs: improve custom server docs
aralroca Aug 29, 2024
64f7855
docs: improve custom server docs
aralroca Aug 29, 2024
790607e
fix things for node.js
aralroca Aug 29, 2024
62765e6
test: add cli tests + add log
aralroca Aug 29, 2024
a518b6a
chore: update package.json exports
aralroca Aug 29, 2024
a5bc712
feat: compile serve with hardcoded paths to run everywhere
aralroca Aug 29, 2024
7db3123
test: improve test
aralroca Aug 29, 2024
3aef4c8
docs: improve docs
aralroca Aug 29, 2024
4839529
feat: improve ambient types
aralroca Aug 29, 2024
275484a
docs: add docs about added APIs
aralroca Aug 29, 2024
733650f
test: init node.js tests
aralroca Aug 30, 2024
8904b8d
chore: change bun to npm in node pipeline
aralroca Aug 30, 2024
45ed331
chore: fix package.json script
aralroca Aug 30, 2024
0431cf9
chore: update script
aralroca Aug 30, 2024
305fbb1
test: fix test
aralroca Aug 31, 2024
c1f75d3
chore: fix pipeline
aralroca Aug 31, 2024
ea5ddfa
chore: use build inside each job
aralroca Aug 31, 2024
55e2c94
chore: remove setup job
aralroca Aug 31, 2024
9c37093
chore: fix node gh action
aralroca Aug 31, 2024
0286e1b
chore: try to create setup again
aralroca Aug 31, 2024
b4fbf14
chore: try again
aralroca Aug 31, 2024
16825aa
chore: fix GH action
aralroca Aug 31, 2024
dc2e26b
chore: use bun on node GH action to :workspace schema
aralroca Aug 31, 2024
580c7ef
fix: fix absolute import specifiers in Node.js
aralroca Aug 31, 2024
a398b66
fix: fix more absolute import specifiers in Node.js
aralroca Aug 31, 2024
b2faa84
fix(build): solve absolute import specifiers in Node.js
aralroca Aug 31, 2024
d669c46
revert: revert resolve
aralroca Aug 31, 2024
6c417b0
feat: add tls in Node.js via config
aralroca Aug 31, 2024
43c2eab
feat: use brisa.config.js in prod
aralroca Aug 31, 2024
ebbbd0a
refactor: rename module
aralroca Aug 31, 2024
214c645
test: add test case
aralroca Aug 31, 2024
4cda434
test: add test case
aralroca Aug 31, 2024
f579906
test: add more test cases
aralroca Aug 31, 2024
fafdf83
test: add api route test
aralroca Aug 31, 2024
7328773
fix(serve): fix redirect with parameters
aralroca Aug 31, 2024
01b1893
feat: add split-cookies-string
aralroca Aug 31, 2024
c8ef043
feat: resolve cookies with Node.js
aralroca Aug 31, 2024
46fc3d2
docs: add docs about websockets in Node.js
aralroca Aug 31, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 28 additions & 11 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Test
name: Tests

on:
push:
Expand All @@ -7,29 +7,46 @@ on:
branches: [main]

jobs:
build:
bun-tests:
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]

steps:
# Useful to test Node.js runtime is working fine on some tests
- uses: actions/checkout@v4
- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: '22'
node-version: '22'
- name: Setup Bun.js
uses: oven-sh/setup-bun@v1
with:
bun-version: 1.1.26
- name: Install dependencies
run: bun install
- name: Build project
run: bun run build
- name: Run Bun tests
run: bun test

# Bun setup, used for almost everything inside the project
node-tests:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]
steps:
- uses: actions/checkout@v4
- name: Bunjs
- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: '22'
- name: Setup Bun.js
uses: oven-sh/setup-bun@v1
with:
bun-version: 1.1.26

- name: Install dependencies
run: bun install
- run: bun run build
- run: bun run test
- name: Build project
run: bun run build
- name: Run Node.js tests
run: bun run test:node
Binary file modified bun.lockb
Binary file not shown.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"release-adapter-vercel:alpha": "bun run --cwd packages/adapter-vercel build && npm publish --workspace=packages/adapter-vercel --tag next --access public",
"release:alpha": "bun run release-brisa:alpha && bun run release-create-brisa:alpha && release-adapter-vercel:alpha",
"test": "bun run --filter 'brisa' test",
"test:node": "node --test \"**/*.node-test.js\"",
"test:coverage": "bun run --cwd packages/brisa --coverage",
"tsc:check": "bun run --cwd packages/brisa tsc:check",
"update-version": "bun run scripts/update-brisa-version.ts",
Expand Down
6 changes: 3 additions & 3 deletions packages/adapter-vercel/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,15 @@ describe('adapter-vercel', () => {
});
});

describe('output=server', () => {
describe('output=bun', () => {
// This is temporal, in the near future we are going to support this
it('should not create .vercel/output/config.json if output is server', async () => {
it('should not create .vercel/output/config.json if output is "bun"', async () => {
const generatedMap = await createOutFixture(['index.html']);
const { adapt } = vercelAdapter();
await adapt(
{
...brisaConstants,
CONFIG: { ...brisaConstants.CONFIG, output: 'server' },
CONFIG: { ...brisaConstants.CONFIG, output: 'bun' },
},
generatedMap,
);
Expand Down
138 changes: 137 additions & 1 deletion packages/brisa/cli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ describe('Brisa CLI', () => {
originalArgv = process.argv.slice();
mock.module(path.join(FIXTURES, 'brisa.config.ts'), () => ({
default: {
output: 'server',
output: 'bun',
},
}));

Expand Down Expand Up @@ -118,6 +118,7 @@ describe('Brisa CLI', () => {
mockExistsSync.mockRestore();
mockRandomBytes.mockRestore();
process.argv = originalArgv.slice();
process.env.PORT = undefined;
});

it('should display the --help options', async () => {
Expand Down Expand Up @@ -182,6 +183,34 @@ describe('Brisa CLI', () => {
]);
});

it('should use process.env.PORT as default port on bun dev', async () => {
process.env.PORT = '3005';
process.argv = ['bun', 'brisa', 'dev'];

const newDevOptions = {
...devOptions,
env: { ...devOptions.env, PORT: '3005' },
};

await cli.main(options);

expect(mockSpawnSync.mock.calls[0]).toEqual([
'bun',
['--version'],
{ stdio: 'ignore' },
]);
expect(mockSpawnSync.mock.calls[1]).toEqual([
'bun',
[BUILD_PATH, 'DEV'],
newDevOptions,
]);
expect(mockSpawnSync.mock.calls[2]).toEqual([
'bun',
[SERVE_PATH, '3005', 'DEV'],
newDevOptions,
]);
});

it('should execute "brisa dev" command with custom port', async () => {
process.argv = ['bun', 'brisa', 'dev', '--port', '5000'];

Expand Down Expand Up @@ -319,6 +348,67 @@ describe('Brisa CLI', () => {
]);
});

it('should use process.env.PORT as default port on tauri integration', async () => {
process.env.PORT = '5000';
process.argv = ['bun', 'brisa', 'dev'];

const newDevOptions = {
...devOptions,
env: { ...devOptions.env, PORT: '5000' },
};

mock.module(path.join(FIXTURES, 'brisa.config.ts'), () => ({
default: {
output: 'desktop',
},
}));

await cli.main(options);

expect(mockSpawnSync.mock.calls[0]).toEqual([
'bun',
['--version'],
{ stdio: 'ignore' },
]);
expect(mockSpawnSync.mock.calls[1]).toEqual([
'bun',
['i', '@tauri-apps/cli@2.0.0-beta.21'],
newDevOptions,
]);
expect(mockSpawnSync.mock.calls[2]).toEqual([
'bunx',
[
'tauri',
'init',
'-A',
'test',
'-W',
'test',
'-D',
'../out',
'--dev-url',
'http://localhost:5000',
'--before-dev-command',
"echo 'Starting desktop app...'",
'--before-build-command',
"echo 'Building desktop app...'",
],
newDevOptions,
]);

expect(mockSpawnSync.mock.calls[3]).toEqual([
'bun',
[BUILD_PATH, 'DEV'],
newDevOptions,
]);

expect(mockSpawnSync.mock.calls[4]).toEqual([
'bunx',
['tauri', 'dev', '--port', '5000'],
newDevOptions,
]);
});

it('should build a desktop app with "brisa dev" command and output=desktop in another port', async () => {
process.argv = ['bun', 'brisa', 'dev', '--port', '5000'];

Expand Down Expand Up @@ -1091,6 +1181,52 @@ describe('Brisa CLI', () => {
]);
});

it('should use process.env.PORT as default port on bun start', async () => {
process.env.PORT = '3005';
process.argv = ['bun', 'brisa', 'start'];

const newProdOptions = {
...prodOptions,
env: { ...prodOptions.env, PORT: '3005' },
};

await cli.main(options);

expect(mockSpawnSync.mock.calls[0]).toEqual([
'bun',
['--version'],
{ stdio: 'ignore' },
]);

expect(mockLog.mock.calls.flat().join('')).toContain(`Runtime on Bun.js`);

expect(mockSpawnSync.mock.calls[1]).toEqual([
'bun',
[SERVE_PATH, '3005', 'PROD'],
newProdOptions,
]);
});

it('should "bun start" call "node" when output is "node"', async () => {
process.argv = ['bun', 'brisa', 'start'];

mock.module(path.join(FIXTURES, 'brisa.config.ts'), () => ({
default: {
output: 'node',
},
}));

await cli.main(options);

expect(mockLog.mock.calls.flat().join('')).toContain(`Runtime on Node.js`);

expect(mockSpawnSync.mock.calls[1]).toEqual([
'node',
[SERVE_PATH, '3000', 'PROD'],
prodOptions,
]);
});

it('should execute "brisa start --help" command', async () => {
process.argv = ['bun', 'brisa', 'start', '--help'];

Expand Down
29 changes: 18 additions & 11 deletions packages/brisa/cli.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#!/usr/bin/env bun

const { yellowLog, redLog } = require('@/utils/log/log-color');
const { blueLog, yellowLog, redLog } = require('@/utils/log/log-color');
const cp = require('child_process');
const path = require('node:path');
const fs = require('node:fs');
const crypto = require('node:crypto');
const process = require('node:process');
const { packageManager } = require('./package.json');
const { version, packageManager } = require('./package.json');
const outPath = path
.join(import.meta.dir, 'out')
// There are some cases where the CLI is executed from the node_modules/.bin folder
Expand All @@ -23,6 +23,7 @@ const buildStandaloneFilePath = path.join(
const serveFilepath = path.join(outPath, 'cli', 'serve', 'index.js');
const MOBILE_OUTPUTS = new Set(['android', 'ios']);
const TAURI_OUTPUTS = new Set(['android', 'ios', 'desktop']);
const INFO = blueLog('[ info ] ') + ' ';

async function main({
currentBunVersion,
Expand Down Expand Up @@ -79,7 +80,7 @@ async function main({
let BUN_EXEC: string;
let BUNX_EXEC: string;
let IS_TAURI_APP = false; // default value depends on brisa.config.ts
let OUTPUT = 'server';
let OUTPUT = 'bun';

// Check if is desktop app
try {
Expand Down Expand Up @@ -107,7 +108,7 @@ async function main({

// Command: brisa dev
if (process.argv[2] === 'dev') {
let PORT = 3000; // default port
let PORT = process.env.PORT ?? 3000; // default port
let DEBUG_MODE = false; // default debug mode

for (let i = 3; i < process.argv.length; i++) {
Expand Down Expand Up @@ -278,7 +279,7 @@ async function main({

// Command: brisa start
else if (process.argv[2] === 'start') {
let PORT = 3000; // default port
let PORT = process.env.PORT ?? 3000; // default port

for (let i = 3; i < process.argv.length; i++) {
switch (process.argv[i]) {
Expand All @@ -295,12 +296,15 @@ async function main({
return process.exit(0);
}
}

cp.spawnSync(
BUN_EXEC,
[serveFilepath, PORT.toString(), 'PROD'],
prodOptions,
const isNode = OUTPUT === 'node';
const exec = isNode ? 'node' : BUN_EXEC;
console.log(
INFO,
`🚀 Brisa ${version}: Runtime on ` +
(isNode ? `Node.js ${process.version}` : `Bun.js ${Bun.version}`),
);

cp.spawnSync(exec, [serveFilepath, PORT.toString(), 'PROD'], prodOptions);
}

// Add integrations like mdx, tailwindcss, etc
Expand Down Expand Up @@ -343,7 +347,10 @@ async function main({
return process.exit(1);
}

async function initTauri(options = devOptions, port = 3000) {
async function initTauri(
options = devOptions,
port = process.env.PORT ?? 3000,
) {
const tauriConfigPath = path.join(
process.cwd(),
'src-tauri',
Expand Down
Loading
Loading