首页 > web前端 > js教程 > 正文

FiveM x TypeScript

WBOY
发布: 2024-09-09 08:16:30
转载
881人浏览过

fivem x typescript

fivem 是 grand theft auto v 的修改版,使您能够在由 cfx.re 提供支持的定制专用服务器上玩多人游戏。

当您开发 fivem 服务器时,您可以创建资源。这些资源可以用多种语言编写:lua、c# 和 javascript。在本文中,我们将了解如何使用 typescript 构建资源

类型 :

为了输入我们的代码,我们将使用 fivem 背后的公司 cfx.re 提供的两个软件包

  • @citizenfx/client
  • @citizenfx/服务器

这些包为客户端或服务器端代码中可用的每个本机方法提供了类型。

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"]
}

登录后复制

捆 :

编译 .ts 文件后,您将必须创建一个将由 fivem 服务器加载和运行的包。事实上,fivem 只允许 require 原生 node.js 包,如路径、fs、…

为此,我们使用名为 rollup 的工具,它是一个基于插件系统的 javascript 模块捆绑器。我也探索过其他工具,如 vite、rspack,但太复杂了。另一种提供良好性能的工具是turbopack,它是下一次捆绑之后的工具,但目前仍然在下一次。

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()],
};
登录后复制

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"
  },
  ...
}
登录后复制

示例

init.ts

import { join } from "path"

export const init = () => {
    console.log("inited", join(".", "init.js"));
}
登录后复制

index.ts

import { init } from "./init"

on("onresourcestart", async (resname: string) => {
  if (resname === getcurrentresourcename()) {
    init();
  }
});
登录后复制

运行 rollup -c 后,您将只有一个文件:

'use strict';

var path = require('path');

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

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

登录后复制

以上就是FiveM x TypeScript的详细内容,更多请关注php中文网其它相关文章!

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:dev.to网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号