Skip to content

Commit

Permalink
refactor(build): run tsc before building
Browse files Browse the repository at this point in the history
  • Loading branch information
thelindat committed Sep 6, 2024
1 parent 088707e commit 12cce1a
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 69 deletions.
156 changes: 89 additions & 67 deletions build.js
Original file line number Diff line number Diff line change
@@ -1,48 +1,79 @@
//@ts-check

import esbuild from 'esbuild';
import { readFile, readFileSync, writeFileSync } from 'fs';
import { readFile, writeFile } from 'fs/promises';
import { spawn } from 'child_process';

const production = process.argv.includes('--mode=production');
const pkg = JSON.parse(await readFile('package.json', 'utf8'));
const copyright = await readFile('README.md', 'utf8').then((value) => {
return value
?.replace(/[\s\S]*?## Copyright/, '')
?.match(/.{1,65}\s+|\S+/g)
?.join('\n * ')
.replace(/\n{2,}/g, '\n')
.split('\n')[0];
});

console.log(copyright);

/**
* @param {string} name
* @param {import('esbuild').BuildOptions} options
*/
function getContext(name, options) {
return esbuild
.context({
bundle: true,
entryPoints: [`${name}/index.ts`],
outfile: `dist/${name}.js`,
keepNames: true,
dropLabels: production ? ['DEV'] : undefined,
legalComments: 'inline',
banner: {
js: `/**\n * ${copyright} */`,
},
plugins: [
{
name: 'build',
setup(build) {
build.onEnd((result) => {
if (!result || result.errors.length === 0) console.log(`Successfully built ${name}`);
});
},
},
],
...options,
})
.catch(() => process.exit(1));
}

/** @type {import('esbuild').BuildOptions} */
const server = {
const server = await getContext('server', {
platform: 'node',
target: ['node16'],
format: 'cjs',
};
});

/** @type {import('esbuild').BuildOptions} */
const client = {
const client = await getContext('client', {
platform: 'browser',
target: ['es2021'],
format: 'iife',
};
});

const production = process.argv.includes('--mode=production');
const buildCmd = production ? esbuild.build : esbuild.context;
const wordWrap = new RegExp(`.{1,65}\\s+|\\S+`, 'g');
const packageJson = JSON.parse(readFileSync('package.json', { encoding: 'utf8' }));
const copyright = readFileSync('README.md', { encoding: 'utf8' })
.replace(/[\s\S]*?## Copyright/, '')
.match(wordWrap)
.join('\n * ')
.replace(/\n{2,}/g, '\n');

console.log(copyright.split('\n')[0]);

writeFileSync(
'.yarn.installed',
new Date().toLocaleString('en-AU', { timeZone: 'UTC', timeStyle: 'long', dateStyle: 'full' })
);

writeFileSync(
'fxmanifest.lua',
`fx_version 'cerulean'
async function build() {
return Promise.all([server.rebuild(), client.rebuild()]).then(() => {
writeFile('.yarn.installed', new Date().toISOString());
writeFile(
'fxmanifest.lua',
`fx_version 'cerulean'
game 'gta5'
name '${packageJson.name}'
author '${packageJson.author}'
version '${packageJson.version}'
license '${packageJson.license}'
repository '${packageJson.repository.url}'
description '${packageJson.description}'
name '${pkg.name}'
author '${pkg.author}'
version '${pkg.version}'
license '${pkg.license}'
repository '${pkg.repository.url}'
description '${pkg.description}'
dependencies {
'/server:7290',
Expand All @@ -52,7 +83,6 @@ dependencies {
files {
'lib/init.lua',
'lib/client/**.lua',
'imports/client.lua',
'locales/*.json',
'common/data/*.json',
}
Expand All @@ -61,38 +91,30 @@ client_script 'dist/client.js'
server_script 'dist/server.js'
`
);

for (const context of ['client', 'server']) {
buildCmd({
bundle: true,
entryPoints: [`${context}/index.ts`],
outfile: `dist/${context}.js`,
keepNames: true,
dropLabels: production ? ['DEV'] : undefined,
legalComments: 'inline',
banner: {
js: `/**\n * ${copyright} */`,
},
plugins: production
? undefined
: [
{
name: 'rebuild',
setup(build) {
const cb = (result) => {
if (!result || result.errors.length === 0) console.log(`Successfully built ${context}`);
};
build.onEnd(cb);
},
},
],
...(context === 'client' ? client : server),
})
.then((build) => {
if (production) return console.log(`Successfully built ${context}`);
);
});
}

build.watch();
})
.catch(() => process.exit(1));
const tsc = spawn(`tsc --build ${production ? '' : '--watch --preserveWatchOutput'} && tsc-alias`, {
stdio: ['inherit', 'pipe', 'inherit'],
shell: true,
});

if (production) {
tsc.on('close', async (code) => {
if (code !== 0) return process.exit(code);

await build();

process.exit(0);
});
}

tsc.stdout.on('data', async (data) => {
const output = data.toString();
process.stdout.write(output);

if (output.includes('Found 0 errors.')) {
await build();
}
});
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
"./server": "./package/lib/server/index.js"
},
"scripts": {
"prepare": "tsc --build && tsc-alias",
"build": "node build.js --mode=production",
"watch": "node build.js"
},
Expand Down
2 changes: 1 addition & 1 deletion server/config.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export * from '../common/config';

export const CREATE_DEFAULT_ACCOUNT = GetConvarInt('ox:createDefaultAccount', 1) === 1;
export const CREATE_DEFAULT_ACCOUNT = GetConvarInt('ox:createDefaultAccount', 1) === 1;

0 comments on commit 12cce1a

Please sign in to comment.