Skip to content

Commit

Permalink
feat: use deno.json and move it inside build (#658)
Browse files Browse the repository at this point in the history
  • Loading branch information
aralroca authored Dec 2, 2024
1 parent 4bece45 commit 62322e3
Show file tree
Hide file tree
Showing 6 changed files with 297 additions and 0 deletions.
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 @@ -325,5 +325,125 @@ describe('cli', () => {
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

0 comments on commit 62322e3

Please sign in to comment.