Home > Web Front-end > JS Tutorial > FiveM x TypeScript

FiveM x TypeScript

王林
Release: 2024-09-08 22:30:40
Original
495 people have browsed it

FiveM x TypeScript

FiveM is a modification for Grand Theft Auto V enabling you to play multiplayer on customized dedicated servers, powered by Cfx.re.

When you develop a FiveM server, you can create resource. Theses resources can be write in multiple langage Lua, C# and JavaScript. In this article we are going to see how to build a resource using TypeScript

Type :

For typing our code, we will use two packages provided by CFX.re the company behind FiveM

  • @citizenfx/client
  • @citizenfx/server

These packages provided types for each native method usable inside client-side or server-side code.

tsconfig.json

{
  "compilerOptions": {
    "target": "ES2020",
    "module": "ESNext",
    "moduleResolution": "Bundler",
    // Location
    "outDir": "./dist",
    // Other
    "types": ["@citizenfx/client", "@types/node"],
    "lib": ["ES2020"],
    "strict": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "skipLibCheck": true
  },
  "include": ["src/**/*.ts"],
  "exclude": ["**/node_modules", "**/.test.ts"]
}

Copy after login

Bundle :

After the compilation of .ts files, you will have to create a bundle which going to be loaded and ran by FiveM server. Indeed, FiveM only allows require of native node.js package like path, fs, …

To do it, we use the tool named rollup, it’s a JavaScript module bundler based on plugin system. I also explored other tools like vite, rspack, but it’s too complicated. An other tool which offers good performance will be turbopack a tool behind next bundling but it still inside next for the moment.

rollup.config.mjs

import typescript from "@rollup/plugin-typescript";
import commonjs from "@rollup/plugin-commonjs";
import resolve from "@rollup/plugin-node-resolve";

export default {
  input: "src/index.ts",
  output: {
    dir: "dist",
    format: "cjs",
    sourcemap: false,
  },
  plugins: [resolve(), typescript(), commonjs()],
};
Copy after login

package.json :

{
  ...
  "devDependencies": {
    "@citizenfx/client": "2.0.9282-1",
    "@rollup/plugin-commonjs": "^26.0.1",
    "@rollup/plugin-node-resolve": "^15.2.3",
    "@rollup/plugin-typescript": "^11.1.6",
    "@types/node": "^20.14.12",
    "rollup": "^4.20.0",
    "tslib": "^2.6.3",
    "typescript": "^5.5.4"
  },
  ...
}
Copy after login

Exemple

init.ts

import { join } from "path"

export const init = () => {
    console.log("inited", join(".", "init.js"));
}
Copy after login

index.ts

import { init } from "./init"

on("onResourceStart", async (resName: string) => {
  if (resName === GetCurrentResourceName()) {
    init();
  }
});
Copy after login

After running rollup -c, you will have only one file :

'use strict';

var path = require('path');

const init = () => {
    console.log("inited", path.join(".", "init.js"));
};

on("onResourceStart", async (resName) => {
    if (resName === GetCurrentResourceName()) {
        init();
    }
});

Copy after login

The above is the detailed content of FiveM x TypeScript. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template