• Home
  • Categories
  • Recent
  • Popular
  • Top
  • Tags
  • Users
  • Groups
  • Documentation
    • Home
    • Read API
    • Write API
    • Plugin Development
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
v3.5.2 Latest
Buy Hosting

Eventual migration to TypeScript

Scheduled Pinned Locked Moved Technical Support
4 Posts 3 Posters 380 Views
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • stevefan1999-personalS Offline
    stevefan1999-personalS Offline
    stevefan1999-personal
    wrote on last edited by
    #1

    We all know NodeBB plugin is such a pain in the ass, because we do not have type information and we aren't sure about the hooked data structure and made unintended modification and can even unexpectedly corrupt the database. We should move on to use TypeScript, with the added benefit of being able to use ES modules that will make it able to start much faster (since ES modules are static and CommonJS are dynamic, this decreases pressure on module resolution)

    julianJ 1 Reply Last reply
    2
  • julianJ Online
    julianJ Online
    julian GNU/Linux
    replied to stevefan1999-personal on last edited by
    #2

    @stevefan1999-personal This is probably somewhat of an eventuality, I think.

    I don't have much experience with TypeScript, but I know that there are some developers (@PitaJ in particular) who are already writing plugins in TypeScript.

    There are a couple of larger tech debt projects to take care of first, but this is definitely already on our medium-term roadmap.

    1 Reply Last reply
    1
  • julianJ Online
    julianJ Online
    julian GNU/Linux
    wrote on last edited by julian
    #3

    I'm not entirely certain about ESM support; if only because migrating from commonjs to ESM is often an all-or-nothing approach.

    IIRC once you are in ESM-land, you can easily import other ES modules, and you can import commonJS modules, but your own exports cannot be imported by commonJS files.

    That causes a significant issue in that many plugins (still written in CommonJS) will try to require ESM-ified libraries in NodeBB and promptly crash.

    While I am reasonably certain there is a way forward (via some sort of shim, etc.), that might require the talents of people smarter than me (like @baris, @pitaj, or @oplik0 😄 )

    oplik0O 1 Reply Last reply
    0
  • oplik0O Offline
    oplik0O Offline
    oplik0 Plugin & Theme Dev Community Rep
    replied to julian on last edited by oplik0
    #4

    @julian I think it might be possible with some build tooling magic...

    Importing plugins using CJS is as you mentioned the easy part - await import() works both ways with CJS, and in a pinch you can use createRequire to get require back in ESM.

    This even works with ESM plugins by just changing src/plugins/index.js to use (await import(libraryPath)).default (and ideally adding some way to resolve main from package.json since currently that change needs library in plugin.json to be defined 🙂 )

    The other way though is the problem - calling require on ESM will fail. So the big issue are the require.main.require calls.

    However, I imagine it should be possible to fix that in the build step - though it would require building the server-side code too (which TS will require anyway). I'm not sure how one would go about it with Webpack, but Rollup already has a plugin for converting CommonJS to ESM (since it outputs ESM). I'm not sure how it handles require.main.require, but it probably could be transformed to work correctly as just a relative require before that plugin runs (or the plugin could just be improved to handle that).

    But ultimately if there are other breaking plugin changes on the roadmap maybe just pairing migration to ESM with them would be simpler. Since importing CJS from ESM mostly works fine, I imagine it would be similar in difficulty to the switch to require.main.require for most plugins.

    1 Reply Last reply
    1
  • julianJ julian moved this topic from NodeBB Development on

Copyright © 2023 NodeBB | Contributors
  • Login

  • Don't have an account? Register

  • Login or register to search.
Powered by NodeBB Contributors
  • First post
    Last post
0
  • Home
  • Categories
  • Recent
  • Popular
  • Top
  • Tags
  • Users
  • Groups
  • Documentation
    • Home
    • Read API
    • Write API
    • Plugin Development