Skip to content

Commit 12cce1a

Browse files
committed
refactor(build): run tsc before building
1 parent 088707e commit 12cce1a

File tree

3 files changed

+90
-69
lines changed

3 files changed

+90
-69
lines changed

build.js

+89-67
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,79 @@
1+
//@ts-check
2+
13
import esbuild from 'esbuild';
2-
import { readFile, readFileSync, writeFileSync } from 'fs';
4+
import { readFile, writeFile } from 'fs/promises';
5+
import { spawn } from 'child_process';
6+
7+
const production = process.argv.includes('--mode=production');
8+
const pkg = JSON.parse(await readFile('package.json', 'utf8'));
9+
const copyright = await readFile('README.md', 'utf8').then((value) => {
10+
return value
11+
?.replace(/[\s\S]*?## Copyright/, '')
12+
?.match(/.{1,65}\s+|\S+/g)
13+
?.join('\n * ')
14+
.replace(/\n{2,}/g, '\n')
15+
.split('\n')[0];
16+
});
17+
18+
console.log(copyright);
19+
20+
/**
21+
* @param {string} name
22+
* @param {import('esbuild').BuildOptions} options
23+
*/
24+
function getContext(name, options) {
25+
return esbuild
26+
.context({
27+
bundle: true,
28+
entryPoints: [`${name}/index.ts`],
29+
outfile: `dist/${name}.js`,
30+
keepNames: true,
31+
dropLabels: production ? ['DEV'] : undefined,
32+
legalComments: 'inline',
33+
banner: {
34+
js: `/**\n * ${copyright} */`,
35+
},
36+
plugins: [
37+
{
38+
name: 'build',
39+
setup(build) {
40+
build.onEnd((result) => {
41+
if (!result || result.errors.length === 0) console.log(`Successfully built ${name}`);
42+
});
43+
},
44+
},
45+
],
46+
...options,
47+
})
48+
.catch(() => process.exit(1));
49+
}
350

4-
/** @type {import('esbuild').BuildOptions} */
5-
const server = {
51+
const server = await getContext('server', {
652
platform: 'node',
753
target: ['node16'],
854
format: 'cjs',
9-
};
55+
});
1056

11-
/** @type {import('esbuild').BuildOptions} */
12-
const client = {
57+
const client = await getContext('client', {
1358
platform: 'browser',
1459
target: ['es2021'],
1560
format: 'iife',
16-
};
61+
});
1762

18-
const production = process.argv.includes('--mode=production');
19-
const buildCmd = production ? esbuild.build : esbuild.context;
20-
const wordWrap = new RegExp(`.{1,65}\\s+|\\S+`, 'g');
21-
const packageJson = JSON.parse(readFileSync('package.json', { encoding: 'utf8' }));
22-
const copyright = readFileSync('README.md', { encoding: 'utf8' })
23-
.replace(/[\s\S]*?## Copyright/, '')
24-
.match(wordWrap)
25-
.join('\n * ')
26-
.replace(/\n{2,}/g, '\n');
27-
28-
console.log(copyright.split('\n')[0]);
29-
30-
writeFileSync(
31-
'.yarn.installed',
32-
new Date().toLocaleString('en-AU', { timeZone: 'UTC', timeStyle: 'long', dateStyle: 'full' })
33-
);
34-
35-
writeFileSync(
36-
'fxmanifest.lua',
37-
`fx_version 'cerulean'
63+
async function build() {
64+
return Promise.all([server.rebuild(), client.rebuild()]).then(() => {
65+
writeFile('.yarn.installed', new Date().toISOString());
66+
writeFile(
67+
'fxmanifest.lua',
68+
`fx_version 'cerulean'
3869
game 'gta5'
3970
40-
name '${packageJson.name}'
41-
author '${packageJson.author}'
42-
version '${packageJson.version}'
43-
license '${packageJson.license}'
44-
repository '${packageJson.repository.url}'
45-
description '${packageJson.description}'
71+
name '${pkg.name}'
72+
author '${pkg.author}'
73+
version '${pkg.version}'
74+
license '${pkg.license}'
75+
repository '${pkg.repository.url}'
76+
description '${pkg.description}'
4677
4778
dependencies {
4879
'/server:7290',
@@ -52,7 +83,6 @@ dependencies {
5283
files {
5384
'lib/init.lua',
5485
'lib/client/**.lua',
55-
'imports/client.lua',
5686
'locales/*.json',
5787
'common/data/*.json',
5888
}
@@ -61,38 +91,30 @@ client_script 'dist/client.js'
6191
server_script 'dist/server.js'
6292
6393
`
64-
);
65-
66-
for (const context of ['client', 'server']) {
67-
buildCmd({
68-
bundle: true,
69-
entryPoints: [`${context}/index.ts`],
70-
outfile: `dist/${context}.js`,
71-
keepNames: true,
72-
dropLabels: production ? ['DEV'] : undefined,
73-
legalComments: 'inline',
74-
banner: {
75-
js: `/**\n * ${copyright} */`,
76-
},
77-
plugins: production
78-
? undefined
79-
: [
80-
{
81-
name: 'rebuild',
82-
setup(build) {
83-
const cb = (result) => {
84-
if (!result || result.errors.length === 0) console.log(`Successfully built ${context}`);
85-
};
86-
build.onEnd(cb);
87-
},
88-
},
89-
],
90-
...(context === 'client' ? client : server),
91-
})
92-
.then((build) => {
93-
if (production) return console.log(`Successfully built ${context}`);
94+
);
95+
});
96+
}
9497

95-
build.watch();
96-
})
97-
.catch(() => process.exit(1));
98+
const tsc = spawn(`tsc --build ${production ? '' : '--watch --preserveWatchOutput'} && tsc-alias`, {
99+
stdio: ['inherit', 'pipe', 'inherit'],
100+
shell: true,
101+
});
102+
103+
if (production) {
104+
tsc.on('close', async (code) => {
105+
if (code !== 0) return process.exit(code);
106+
107+
await build();
108+
109+
process.exit(0);
110+
});
98111
}
112+
113+
tsc.stdout.on('data', async (data) => {
114+
const output = data.toString();
115+
process.stdout.write(output);
116+
117+
if (output.includes('Found 0 errors.')) {
118+
await build();
119+
}
120+
});

package.json

-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
"./server": "./package/lib/server/index.js"
1717
},
1818
"scripts": {
19-
"prepare": "tsc --build && tsc-alias",
2019
"build": "node build.js --mode=production",
2120
"watch": "node build.js"
2221
},

server/config.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
export * from '../common/config';
22

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

0 commit comments

Comments
 (0)