tom-sherman / rescript-remix

MIT License
30 stars 5 forks source link

Mimic Remix's routing conventions for ReScript modules #15

Closed drewschrauf closed 2 years ago

drewschrauf commented 2 years ago

Closes #4

Implements the proposed file naming convention discussed in #4

There may be some goofs here and there (the Remix routing convention doesn't have a tonne of tests to copy). It supports all of the features listed in the Remix documentation.

It can be enabled by renaming the routes folder to res-routes and updating remix.config.js to call the registration function.

const { registerRoutes } = require('rescript-remix/registerRoutes');

module.exports = {
  ...
  routes(defineRoutes) {
    return defineRoutes(route => {
      registerRoutes(route);
    });
  }
};

One thing to note: We require the registerRoutes.js file to be in a known location and to be using the commonjs module system so it works with Node. We can't guarantee those when it's being used as a dependency by another project as the JS output location and module system is defined by the consumer's bsconfig.json. As a result, we need to generate this file at build time and publish it to npm. There's a postbuild step to copy the file to the correct location.

Also, should I be updating the README for this stuff as part of this PR? Or should we do a documentation pass when this project is a bit more mature (and just show how to use it in the template project for now).

tom-sherman commented 2 years ago

should I be updating the README for this stuff as part of this PR?

Happy to do this in some followups 👍