mocks-server / main

Node.js mock server running live, interactive mocks in place of real APIs
https://www.mocks-server.org
Apache License 2.0
290 stars 17 forks source link

EsBuild suport #463

Open ffind opened 1 year ago

ffind commented 1 year ago

Is your feature request related to a problem? Please describe. I'm using esbuild stack, ie vite, vitests and wanted to setup mocks-server without babel, but as far as I understood it is not really possible

Describe the solution you'd like Would be nice to avoid strict binding to certain transpiler, may be to have set of plugins that allow typescript support? I'm glad to support

javierbrea commented 1 year ago

Hi @ffind, For the moment the only possibility for doing such thing is developing a plugin and loading files from a folder different to the "mocks" one.

The server is made with CJS, and it requires a transpiler for converting files in any other format to it before loading them. Converting the files in the "mocks" folder is a responsibility of an element in the core of the server, so, this behavior can't be modified by using a plugin. The server API includes a files.createLoader method that allows to receive file contents from defined folders in the "mocks" path, and implement your own customized behavior when files are loaded or they change, but file contents are received already transpiled in this case, so this method is not valid for your requirement.

So, as mentioned, you could implement a plugin being at charge of loading files from a different folder at your convenience, and then use the mock.createloaders method for adding routes and collections based on their contents. But you would have to implement the whole logic about reading the files and transpile them with your preferred library, and you would be also at charge of watching the files in order to refresh the routes and collections when there is any change, convert YAML files, etc.

The good new is that currently I'm migrating the project to TypeScript, and I'll try to export the project both in CJS and ESM, so, probably in next releases ESM would be supported out of the box, without the need to use any transpiler. But it would be still needed to transpile TypeScript files, so I will take this requirement into account in order to provide also an easier mechanism to use custom transpilers.