1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. GTA Network forum is now in archive mode.

JS: require() other resources, including classes.

Discussion in 'Libraries/Wrappers/Web' started by Katalina, May 24, 2017.

  1. Katalina

    Katalina Member Developer

    Messages:
    23
    Likes Received:
    26
    Joined:
    May 14, 2017
    https://github.com/kayteh/require

    The README goes into more details, but this function does two things:
    1. Allows you to load classes and variables (given some setup) from other JS files. Not possible otherwise...
    2. Allows you to easily address other resources's JS files.
    This semi-emulates node.js's require() function; however, due to loads of limitations in the API, it cannot do relative paths, but does employ caching for speed.

    Example using a personal library.
    Code:
    let Rect
    let helloWorld
    
    API.onResourceStart.connect(() => {
        const { require } = exported.require.require
    
        Rect = require('@/liberty/uikit').Rect
    
        helloWorld = new Rect({ x: 0, y: 0, w: 100, h: 100 })
    })
    
    API.onUpdate.connect(() => {
        helloWorld.draw()
    })
    
    There's still some rough edges, however this has already proven useful to me (i really like "new Rect(...)" vs "createRect", this is 2017 and that stuff was fine 3 years ago and no sooner.)

    I know the exported.require.require.require incantation is f-ing ridiculous. This is another reason to use this.

    See an issue? Did I miss X feature? Drop an issue at https://github.com/kayteh/require/issues/new
     
    Last edited: May 24, 2017
    SaraT and Renato like this.

Share This Page