GithubHelp home page GithubHelp logo

xxshady / altv-esbuild Goto Github PK

View Code? Open in Web Editor NEW
34.0 1.0 3.0 574 KB

Hot reload (without reconnect and restart of the server) ✨

Home Page: https://xxshady.github.io/altv-esbuild

JavaScript 3.72% TypeScript 96.28%
altv esbuild fivem hot-reload hot-reload-plugin ragemp gta5 gtav

altv-esbuild's People

Contributors

github-actions[bot] avatar xxshady avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

altv-esbuild's Issues

alt:V resource import confusing error log in dev mode

Example repro

server root

server.toml

modules = [ "js-module" ]
resources = [
  "test",
  "main"
]

resources/test

resource.toml

type = "js"
main = "server.js"

server.js

export const test = () => 10

main

import { test } from "test" // runtime nodejs error when altv-esbuild dev mode is on

Top-level exception with external module when dev: true (altv 14.0)

Only when "dev: true" I get this error when setting external module :

[Error] [altv-esbuild] Top-level exception:
   ReferenceError: ___altvEsbuild_externalOnTop_node_x__alt_x_altv_x_csharp_x_entity_x_sync_x_to_x_js_x_wrapper___ is not defined
    at altv-esbuild:externals-on-top:alt:altv-csharp-entity-sync-to-js-wrapper (file:///xxxxxxxxx/resources/xear-3d-audio-api/dist/server/index.mjs:84:25)
    at __require (file:///xxxxxxxxxx/resources/xear-3d-audio-api/dist/server/index.mjs:45:50)
    at file:///E:/Dropbox/Game%20Dev/AltV/altv-server-DEV-W/resources/xear-3d-audio-api/dist/server/index.mjs:93:30

My watch-server :

import { build } from "esbuild"
import { altvEsbuild } from "altv-esbuild"

build({
    watch: true,
    bundle: true,
    platform: "node",
    logLevel: "info",
    target: "esnext",
    format: "esm",
    external: ["alt:altv-csharp-entity-sync-to-js-wrapper"],
    entryPoints: ["./resources/xear-3d-audio-api/src/server/index.mjs"],
    outfile: "./resources/xear-3d-audio-api/dist/server/index.mjs",
    plugins: [
        altvEsbuild({
            mode: "server",
            dev: true, // enables hot reload automatically
            altvEnums: false,
        })
    ],
})

In "/server/index.mjs" have :

import alt from 'alt-server'
import * as EntitySyncWrap from 'alt:altv-csharp-entity-sync-to-js-wrapper'

Reloading resources without altv-server connection - esbuild

I wanted to deploy altv-server for local development in Docker. And I'm having trouble net connecting altv-server-inject to my esbuild server.

The problem would be solved by being able to specify the host of the esbuild server.

But I thought, why is this connection needed at all? Altv server itself can monitor resource updates and reload them on the fly.

I tested fs.watch too

import fs from "fs";

fs.watch('./resources/chat/server/index.js', (eventType, filename) => {
  alt.log(filename + ' changed');
});

And it works.

You can tie a resource reloading to this.

This can even be packaged as a separate resource for reloading resources. And use it in esbuild

alt:V 15.0 adaptation

  • Virtual entities baseobjects should be destroyed on resource stop (maybe use base object create & destroy event for all baseobjects and store them in weakset?)

Get rid of the `altv-enums` module

Improve altvEnums feature by adding ability (it will be a separate option enhancedAltvEnums for backward compatibility) to use enums directly from alt-* modules, as with default typescript compiler (tsc):

- import { RadioStation } from 'altv-enums'
+ import { RadioStation } from 'alt-server'

To allow such code i will add transpilation of enums to js from your own local node_modules/@altv/types-* on initial build of code
(types-server, types-client, types-shared)

How it works

I think it would be good to add "how it works" readme section for alt:V community

Client & server code version mismatch

Problem

  1. Server code of 1st version starts
  2. Client code of 1st version starts
  3. Server restarts resource (changes version/revision of code)
  4. Server code of 2nd version starts
  5. Client code of 1st version still starts
  6. Client code of 1st version emits clientReady event to server and server thinks its on same version

Add altv-inject user API

Add api to the altv code to trigger the connection of already connected players during the start of the resource to connect them by user code decision.

Something like this

import { triggerPlayersConnect } from 'altv-esbuild'

function initDatabase() {
  // ... some database async shit
  triggerPlayersConnect()
}

Add hooking API

Allow users to intercept various function calls in the alt:V JS API for debugging/development purposes

Example:

altvEsbuild({
  mode: "client",
  dev: {
    hooks: {
      // "alt-client" module
      alt: {
        // hook function must be an arrow-function so it can be converted to a string
        // caution: variables from outer scope (any variable of build script) can't be used
        log: (args, originalFunc, alt) => {
          originalFunc("alt.log is called")
          originalFunc(...args)
          
          alt.logWarning("calling another api function")
          alt.log("since alt.log is hooked this call will cause infinite recursion")
        },
      },
      // "natives" module (clientside only ofcourse)
      natives: {

      },
      globalThis: {
        console: { ... },
        setTimeout,
        // ....etc.
      }
    }
  }
})

'external' option not working

With my project in JS that use an external C# to JS wrapper I get this error :

[server] X [ERROR] Could not resolve "altv-csharp-entity-sync-to-js-wrapper"
[server]
[server]     resources/altvfpvdrone-objapi-dev/src/server/index.mjs:2:32:
      2 │ ...rt * as EntitySyncWrap from 'altv-csharp-entity-sync-to-js-wrapper'
[server]         ╵                                ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[server]
[server]   You can mark the path "altv-csharp-entity-sync-to-js-wrapper" as external to exclude it from the bundle, which will remove this error.
[server]
[server] 1 error

I have added :
external: "altv-csharp-entity-sync-to-js-wrapper",

To the build({}) of watch-server.js but the error remained.

watch-server.js :

import { build } from "esbuild"
import { altvEsbuild } from "altv-esbuild"

build({
    watch: true,
    bundle: true,
    platform: "node",
    logLevel: "info",
    external: "altv-csharp-entity-sync-to-js-wrapper",
    entryPoints: ["./resources/altvfpvdrone-objapi-dev/src/server/index.mjs"],
    outfile: "./resources/altvfpvdrone-objapi-dev/dist/server/index.mjs",
    plugins: [
        altvEsbuild({
            mode: "server",
            dev: true, // enables hot reload automatically
            altvEnums: true,
        })
    ],
})

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.