GithubHelp home page GithubHelp logo

xxshady / altv-esbuild Goto Github PK

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

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

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

JavaScript 3.68% TypeScript 96.32%
altv esbuild fivem hot-reload hot-reload-plugin ragemp gta5 gtav

altv-esbuild's Introduction

altv-esbuild

ingame demo

You can click for better quality

Special thanks ❤️ to innxz and uncle_ara for financially supporting me and this library.

A plugin that greatly simplifies server/client JS and TS development (as well as production) on the alt:V platform.
(extended and improved version of the previous esbuild dev plugin).

Features

  • Write your scripts in JS/TS with enabled 5-million-polygon cars and mlo without client crashes
  • Hot reload without server restart or client reconnect (using alt:V resource restart)
  • Full client and server support
  • Restart console command for client and server ("res" by default)
  • Improved top-level exception output during development
  • Direct support for alt:V enums, even in JS code (documentation)

Docs

Docs web page: https://xxshady.github.io/altv-esbuild.

How to use?

Example resource can be found here.

Install from npm

npm i altv-esbuild

Add to your build code of the server and client

Example of the build server code:

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

// your own variable
const DEV_MODE = true

esbuild.build({
  entryPoints: ["src/main.js"],
  outfile: "dist/bundle.js",
  bundle: true,
  watch: DEV_MODE, // this build option is outdated, see example directory in the repo
  target: "esnext",
  format: "esm",
  
  plugins: [
    altvEsbuild({
      mode: "server", // use "server" for server code, and "client" for client code

      // see docs for more info about these options:
      dev: {
        enabled: DEV_MODE,

        // if `DEV_MODE` is false it will also be automatically set to false too
        enhancedRestartCommand: true, 
      },
      altvEnums: true,
    }),
  ],
  
  external: [
    // none of the following is required, the plugin handles all alt:V modules automatically
    // "alt-server",
    // "alt-client",
    // "alt-shared",
    // "natives"
  ]
})

Limitations

Imports of types, interfaces

esbuild doesn't support importing types as values, you should use type-only imports import type or import { type YourType } syntax.
typescript-eslint with consistent-type-imports rule helps a lot with this.

types.ts

export type SomeType = number

script.ts

import { type SomeType } from './types.ts'

alt:V

Connection queue is not supported between hot reloads (or just resource restarts).
Player disconnect is also not supported between hot reloads. Only player connect is emulated for now in dev environment.

esbuild

Not supported build options

How to find exact source location of any exception?

You can use esbuild source-maps like this: sourcemap: "inline".

serverside

Enable source-maps in server.toml and here you go.

clientside

Here its a bit complicated. If you use vscode Source maps navigator extension can help you jump to your source code.

Some libraries don't work with bundling

If you see such errors it may mean this is your case:

  • Error: Dynamic require of "crypto" is not supported
  • SyntaxError: Unexpected identifier
  • ReferenceError: __dirname is not defined

Libraries that must be external: discord.js, Prisma, TypeORM

Solution: add this library to external build option of esbuild

Contributions

All contributions are greatly appreciated. If there are any questions or you would like to discuss a feature, you can always open issue.

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.