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: use deno.json and move it inside build #658

Merged
merged 1 commit into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ _brisa
**/out/*
**/out-*/*
**/dist/*
**/build-utils/create-deno-json/deno.json
**/.temp-test-files/*
packages/brisa/index.js
packages/brisa/out
Expand Down
5 changes: 5 additions & 0 deletions packages/brisa/src/build-utils/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Brisa build utils

This folder is made to move all the `utils` we currently have that are related to the building of the brisa here so that it is easier to maintain and understand.

We are going to do this migration gradually, so as not to break anything, if you want to contribute to this task, it will be very well received.
103 changes: 103 additions & 0 deletions packages/brisa/src/build-utils/create-deno-json/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import { describe, it, expect, beforeEach, afterEach } from 'bun:test';
import fs from 'node:fs';
import path from 'node:path';
import { createDenoJSON } from '.';

describe('createDenoJSON', () => {
beforeEach(() => {
globalThis.mockConstants = {
BUILD_DIR: import.meta.dirname,
ROOT_DIR: import.meta.dirname,
SRC_DIR: import.meta.dirname,
};
});

afterEach(() => {
delete globalThis.mockConstants;
fs.rmSync(path.join(import.meta.dirname, 'deno.json'));
});

it('should create the default deno.json file when no existing one', () => {
createDenoJSON();
expect(
JSON.parse(
fs.readFileSync(path.join(import.meta.dirname, 'deno.json'), 'utf-8'),
),
).toEqual({
imports: {
'fs/promises': 'node:fs/promises',
path: 'node:path',
},
permissions: {
read: true,
write: true,
run: 'inherit',
},
});
});

it('should merge with the existing deno.json file in the root directory', () => {
// Ensure is not taking src dir
globalThis.mockConstants!.SRC_DIR = path.join(import.meta.dirname, 'src');
fs.writeFileSync(
path.join(import.meta.dirname, 'deno.json'),
JSON.stringify({
permissions: {
read: false,
},
}),
);
createDenoJSON();
expect(
JSON.parse(
fs.readFileSync(path.join(import.meta.dirname, 'deno.json'), 'utf-8'),
),
).toEqual({
imports: {
'fs/promises': 'node:fs/promises',
path: 'node:path',
},
permissions: {
read: false,
write: true,
run: 'inherit',
},
});
});

it('should merge with the existing deno.json file in the src directory', () => {
// Ensure is taking src dir
globalThis.mockConstants!.ROOT_DIR = path.join(
import.meta.dirname,
'not-exist',
);

fs.writeFileSync(
path.join(import.meta.dirname, 'deno.json'),
JSON.stringify({
imports: {
cluster: 'node:cluster',
},
foo: 'bar',
}),
);
createDenoJSON();
expect(
JSON.parse(
fs.readFileSync(path.join(import.meta.dirname, 'deno.json'), 'utf-8'),
),
).toEqual({
foo: 'bar',
imports: {
'fs/promises': 'node:fs/promises',
path: 'node:path',
cluster: 'node:cluster',
},
permissions: {
read: true,
write: true,
run: 'inherit',
},
});
});
});
63 changes: 63 additions & 0 deletions packages/brisa/src/build-utils/create-deno-json/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import fs from 'node:fs';
import path from 'node:path';
import { getConstants } from '@/constants';

export function createDenoJSON() {
const { BUILD_DIR } = getConstants();
const denoJSON = getDenoJSON();
fs.writeFileSync(
path.join(BUILD_DIR, 'deno.json'),
JSON.stringify(denoJSON, null, 2),
);
}

function getDenoJSON() {
const { ROOT_DIR, SRC_DIR } = getConstants();
const defaultDenoJSON = {
imports: {
'fs/promises': 'node:fs/promises',
path: 'node:path',
},
permissions: {
read: true,
write: true,
run: 'inherit',
},
};

if (fs.existsSync(path.join(ROOT_DIR, 'deno.json'))) {
const denoJSON = JSON.parse(
fs.readFileSync(path.join(ROOT_DIR, 'deno.json'), 'utf-8'),
);
return {
...denoJSON,
imports: {
...defaultDenoJSON.imports,
...denoJSON.imports,
},
permissions: {
...defaultDenoJSON.permissions,
...denoJSON.permissions,
},
};
}

if (fs.existsSync(path.join(SRC_DIR, 'deno.json'))) {
const denoJSON = JSON.parse(
fs.readFileSync(path.join(SRC_DIR, 'deno.json'), 'utf-8'),
);
return {
...denoJSON,
imports: {
...defaultDenoJSON.imports,
...denoJSON.imports,
},
permissions: {
...defaultDenoJSON.permissions,
...denoJSON.permissions,
},
};
}

return defaultDenoJSON;
}
120 changes: 120 additions & 0 deletions packages/brisa/src/cli/build.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@
// It's important the order of logs, prebuild should be necessary
// before the build because it needs to find the correct path
// during the build
expect(logs).toContain(

Check failure on line 110 in packages/brisa/src/cli/build.test.ts

View workflow job for this annotation

GitHub Actions / bun-tests (ubuntu-latest)

error: expect(received).toContain(expected)

Compiled successfully!" Received: "" at /home/runner/work/brisa/brisa/packages/brisa/src/cli/build.test.ts:110:20
`Copied prebuild folder inside build${LOG_PREFIX.INFO}${LOG_PREFIX.TICK}Compiled successfully!`,
);
expect(fs.existsSync).toHaveBeenCalledTimes(2);
Expand Down Expand Up @@ -172,7 +172,7 @@
Route: '○ /pages/index',
},
]);
expect(logs).toContain('Generated static pages successfully!');

Check failure on line 175 in packages/brisa/src/cli/build.test.ts

View workflow job for this annotation

GitHub Actions / bun-tests (ubuntu-latest)

error: expect(received).toContain(expected)

Expected to contain: "Generated static pages successfully!" Received: "[ info ] " at /home/runner/work/brisa/brisa/packages/brisa/src/cli/build.test.ts:175:20
expect(logs).not.toContain('Ω (i18n) prerendered for each locale');
});

Expand Down Expand Up @@ -200,7 +200,7 @@
Route: '○ /pages/index',
},
]);
expect(logs).toContain('Generated static pages successfully!');

Check failure on line 203 in packages/brisa/src/cli/build.test.ts

View workflow job for this annotation

GitHub Actions / bun-tests (ubuntu-latest)

error: expect(received).toContain(expected)

Expected to contain: "Generated static pages successfully!" Received: "[ info ] " at /home/runner/work/brisa/brisa/packages/brisa/src/cli/build.test.ts:203:20
expect(logs).toContain('Ω (i18n) prerendered for each locale');
});

Expand Down Expand Up @@ -248,7 +248,7 @@
Route: '| ○ /user/jane',
},
]);
expect(logs).toContain('Generated static pages successfully!');

Check failure on line 251 in packages/brisa/src/cli/build.test.ts

View workflow job for this annotation

GitHub Actions / bun-tests (ubuntu-latest)

error: expect(received).toContain(expected)

Expected to contain: "Generated static pages successfully!" Received: "[ info ] " at /home/runner/work/brisa/brisa/packages/brisa/src/cli/build.test.ts:251:20
});

it('should call outputAdapter if defined in the configuration (PROD)', async () => {
Expand All @@ -268,7 +268,7 @@

await build();
const logs = mockLog.mock.calls.flat().toString();
expect(logs).toContain('Adapting output to my-adapter...');

Check failure on line 271 in packages/brisa/src/cli/build.test.ts

View workflow job for this annotation

GitHub Actions / bun-tests (ubuntu-latest)

error: expect(received).toContain(expected)

Expected to contain: "Adapting output to my-adapter..." Received: "" at /home/runner/work/brisa/brisa/packages/brisa/src/cli/build.test.ts:271:20
expect(mockAdapter).toHaveBeenCalledTimes(1);
expect(mockAdapter.mock.calls[0][0]).toEqual(globalThis.mockConstants);
});
Expand Down Expand Up @@ -325,5 +325,125 @@
await build();
expect(mockLog).not.toHaveBeenCalled();
});

it('should NOT create deno.json when output is "bun" in production', async () => {
globalThis.mockConstants = {
...(getConstants() ?? {}),
BUILD_DIR,
BRISA_DIR,
CONFIG: {
output: 'bun',
},
IS_PRODUCTION: false,
};

await build();
expect(fs.existsSync(path.join(BUILD_DIR, 'deno.json'))).toBeFalse();
});

it('should NOT create deno.json when output is "node" in production', async () => {
globalThis.mockConstants = {
...(getConstants() ?? {}),
BUILD_DIR,
BRISA_DIR,
CONFIG: {
output: 'bun',
},
IS_PRODUCTION: false,
};

await build();
expect(fs.existsSync(path.join(BUILD_DIR, 'deno.json'))).toBeFalse();
});

it('should NOT create deno.json when output is "static" in production', async () => {
globalThis.mockConstants = {
...(getConstants() ?? {}),
BUILD_DIR,
BRISA_DIR,
CONFIG: {
output: 'static',
},
IS_PRODUCTION: false,
};

await build();
expect(fs.existsSync(path.join(BUILD_DIR, 'deno.json'))).toBeFalse();
});

it('should NOT create deno.json when output is "ios" in production', async () => {
globalThis.mockConstants = {
...(getConstants() ?? {}),
BUILD_DIR,
BRISA_DIR,
CONFIG: {
output: 'ios',
},
IS_PRODUCTION: false,
};

await build();
expect(fs.existsSync(path.join(BUILD_DIR, 'deno.json'))).toBeFalse();
});

it('should NOT create deno.json when output is "android" in production', async () => {
globalThis.mockConstants = {
...(getConstants() ?? {}),
BUILD_DIR,
BRISA_DIR,
CONFIG: {
output: 'android',
},
IS_PRODUCTION: false,
};

await build();
expect(fs.existsSync(path.join(BUILD_DIR, 'deno.json'))).toBeFalse();
});

it('should NOT create deno.json when output is "desktop" in production', async () => {
globalThis.mockConstants = {
...(getConstants() ?? {}),
BUILD_DIR,
BRISA_DIR,
CONFIG: {
output: 'desktop',
},
IS_PRODUCTION: false,
};

await build();
expect(fs.existsSync(path.join(BUILD_DIR, 'deno.json'))).toBeFalse();
});

it('should NOT create deno.json when output is "deno" in development', async () => {
globalThis.mockConstants = {
...(getConstants() ?? {}),
BUILD_DIR,
BRISA_DIR,
CONFIG: {
output: 'deno',
},
IS_PRODUCTION: false,
};

await build();
expect(fs.existsSync(path.join(BUILD_DIR, 'deno.json'))).toBeFalse();
});

it('should create deno.json when output is "deno" in production', async () => {
globalThis.mockConstants = {
...(getConstants() ?? {}),
BUILD_DIR,
BRISA_DIR,
CONFIG: {
output: 'deno',
},
IS_PRODUCTION: true,
};

await build();
expect(fs.existsSync(path.join(BUILD_DIR, 'deno.json'))).toBeTrue();
});
});
});
5 changes: 5 additions & 0 deletions packages/brisa/src/cli/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { logTable, generateStaticExport } from './build-utils';
import compileBrisaInternalsToDoBuildPortable from '@/utils/compile-serve-internals-into-build';
import { log } from '@/utils/log/log-build';
import runtimeVersion from '@/utils/runtime-version';
import { createDenoJSON } from '@/build-utils/create-deno-json';

const outputText = {
bun: 'Bun.js Web Service App',
Expand Down Expand Up @@ -128,6 +129,10 @@ export default async function build() {

await compileBrisaInternalsToDoBuildPortable();

if (IS_PRODUCTION && CONFIG.output === 'deno') {
createDenoJSON();
}

if (IS_PRODUCTION && CONFIG.outputAdapter) {
log(LOG_PREFIX.WAIT, `Adapting output to ${CONFIG.outputAdapter.name}...`);
await CONFIG.outputAdapter.adapt(constants, generated);
Expand Down
Loading