Skip to content

Commit 92fa535

Browse files
committed
feat(esbuild): utilize esbuild as bundler
1 parent 029acf6 commit 92fa535

File tree

7 files changed

+1088
-1346
lines changed

7 files changed

+1088
-1346
lines changed

.github/workflows/build-test.yml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
name: Build Test
2+
on: [push, pull_request]
3+
4+
jobs:
5+
build-test:
6+
name: Build Test
7+
runs-on: ubuntu-latest
8+
steps:
9+
- name: Checkout
10+
uses: actions/checkout@v2
11+
- name: Use Node.js
12+
uses: actions/setup-node@v2
13+
with:
14+
node-version: '16.x'
15+
cache: 'npm'
16+
- name: Install dependencies NUI
17+
run: npm ci
18+
- name: Build Bundles
19+
run: npm run build
20+

README.md

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,15 @@ if you wish to use TypeScript in NUI, take a look at our other [boilerplate](htt
2323

2424
## Foreword
2525

26-
Although there already is a well established TypeScript boilerplate,
27-
made by [d0p3t](https://github.com/d0p3t/fivem-ts-boilerplate) for the FiveM ecosystem,
28-
he has unfortunately passed away, leaving it unmaintained. This boilerplate is simply
29-
a more up to date alternative.
26+
This boilerplate was originally based off a previous popular TypeScript boilerplate,
27+
made by [d0p3t](https://github.com/d0p3t/fivem-ts-boilerplate). He heartbeakingly passed
28+
in 2021, leaving the original unmaintained. This boilerplate was an up-to-date alternative.
29+
30+
Since then, this boilerplate has been updated to take advantage of tooling progress made
31+
in the greater NPM ecosystem.
3032

3133
## Requirements
32-
* Node > v14
34+
* Node > v16
3335

3436
## Getting Started
3537

@@ -50,7 +52,7 @@ npm i
5052
### Hot Building
5153

5254
While developing your resource, this boilerplate offers
53-
a `watch`script that will automatically hot rebuild on any
55+
a `watch` script that will automatically hot rebuild on any
5456
change within the `client` or `server` directories.
5557

5658
```sh
@@ -72,7 +74,16 @@ the `build` script.
7274
```sh
7375
npm run build
7476
```
77+
78+
## Version < 2.0.0
79+
80+
Version 2.0.0 introduced ESBuild as the primary bundler, removing
81+
the option for automatic builds through the embedded FXServer webpack builder.
82+
83+
This documentation is preserved for legacy purposes.
84+
7585
### Automatic Builds (Optional)
86+
7687
*This is not recommended as the embedded version of yarn is
7788
ocassionally prone to performance and environment problems. We
7889
highly recomend, you manually run the build script*

build/build-bundle.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
const esbuild = require("esbuild");
2+
3+
const IS_WATCH_MODE = process.env.IS_WATCH_MODE;
4+
5+
const TARGET_ENTRIES = [
6+
{
7+
target: "node16",
8+
entryPoints: ["server/server.ts"],
9+
platform: "node",
10+
outfile: "./dist/server/server.js",
11+
},
12+
{
13+
target: "es2020",
14+
entryPoints: ["client/client.ts"],
15+
outfile: "./dist/client/client.js",
16+
},
17+
];
18+
19+
const buildBundle = async () => {
20+
try {
21+
const baseOptions = {
22+
logLevel: "info",
23+
bundle: true,
24+
charset: "utf8",
25+
minifyWhitespace: true,
26+
absWorkingDir: process.cwd(),
27+
};
28+
29+
for (const targetOpts of TARGET_ENTRIES) {
30+
const mergedOpts = { ...baseOptions, ...targetOpts };
31+
32+
if (IS_WATCH_MODE) {
33+
mergedOpts.watch = {
34+
onRebuild(error) {
35+
if (error)
36+
console.error(
37+
`[ESBuild Watch] (${targetOpts.entryPoints[0]}) Failed to rebuild bundle`
38+
);
39+
else
40+
console.log(
41+
`[ESBuild Watch] (${targetOpts.entryPoints[0]}) Sucessfully rebuilt bundle`
42+
);
43+
},
44+
};
45+
}
46+
47+
const { errors } = await esbuild.build(mergedOpts);
48+
49+
if (errors.length) {
50+
console.error(`[ESBuild] Bundle failed with ${errors.length} errors`);
51+
process.exit(1);
52+
}
53+
}
54+
} catch (e) {
55+
console.log("[ESBuild] Build failed with error");
56+
console.error(e);
57+
process.exit(1);
58+
}
59+
};
60+
61+
buildBundle().catch(() => process.exit(1));

0 commit comments

Comments
 (0)