matthiaaas / express-file-routing

Flexible file-based routing for Express (like Next.js + additional features)
https://www.npmjs.com/package/express-file-routing
MIT License
85 stars 13 forks source link

Usage with a bundler #37

Closed tomas-correia closed 2 months ago

tomas-correia commented 2 months ago

I'm using Rollup to bundle my TypeScript app. Because express-file-routing needs to load route modules from a routes directory, this directory is never mixed in the bundle, nor the actual dist folder.

I'm kind of confused as to how to proceed with this, since there's either an easy way to solve this, or this is an awkward situation that wasn't well thought out during the development of this library.

matthiaaas commented 2 months ago

hey @tomas-correia, thank you for filing an issue. May I ask about the intentions of bundling your source code?

It's right, the library relies on dynamically requiring/importing JS modules by traversing a source code directory.

tomas-correia commented 2 months ago

As for using Rollup, it's mainly in order to resolve my absolute import paths when building the app, since TSC alone can't do it.

Even if I wasn't bundling it, just the fact that I'm using TS, there'd be a build process where everything is merged in a single file.

Since the route modules are never imported anywhere, neither TSC or Rollup are able to include them during build.

matthiaaas commented 2 months ago

For the TypeScript setup and the path aliases you may take a look at matthiaaas/clean-express-prisma. Even though the repo is a bit dated, you might be inspired by the config for running a TS app with this library in dev and prod mode.

Let me know if that helps @tomas-correia.

tomas-correia commented 2 months ago

For the TypeScript setup and the path aliases you may take a look at matthiaaas/clean-express-prisma. Even though the repo is a bit dated, you might be inspired by the config for running a TS app with this library in dev and prod mode.

Let me know if that helps @tomas-correia.

Thanks. I'm trying to follow this solution, but TypeScript is not resolving the import paths without the .js at the end. Not even in VS Code it can resolve them.

I noticed in the "Route Example" section of the README there is an import path without the .js extension. How is this supposed to be working?

matthiaaas commented 2 months ago

I am a bit confused about the issue you are facing. Including the module's file extension in the import path is usually unnecessary.

The boilerplate should work out of the box without any additional configuration. Just run yarn install and then yarn dev.

Can you share if that works for you? After that you should upgrade all dependencies to the latest version since all of them are outdated, including a major version bump of this library. This will require some code changes in the main entry file.

tomas-correia commented 2 months ago

I was trying to understand what your tsconfig has/hasn't that mine does/doesn't for those kind of imports to work.

I found out it has to do with the module and moduleResolution options, which I have set and you don't. I have tried all combinations of module and moduleResolution in order to have everything in my codebase working as before, but errors always pop up no matter what, either because of top-level awaits, imports without an extension, among other problems.

The problem here is I need module and moduleResolution options to be set in my tsconfig. moduleResolution used to be set to "Bundler", since I was using Rollup, and that setting worked for everything I had.

matthiaaas commented 2 months ago

It seems like you're encountering a range of unrelated issues. If you can share your project repository, I might be able to guide you through setting up a TypeScript project with ts-node or a similar package. However, it doesn't seem that the library itself is the issue.

tomas-correia commented 1 month ago

The question of using this library together with a bundler is still open though, as that hasn't been clarified.

matthiaaas commented 1 month ago

Usage of this library with a bundler is indeed tricky and currently not under further consideration, since a bundler is probably unnecessary in almost all appliances of JavaScript backend software.

This evaluation can be reassessed for a sufficiently strong argument for bundling code executed on the server side.

Regarding the inital issue, bundling with Rollup is not necessary in order to make path aliases work in TypeScript.

matthiaaas commented 1 month ago

Feeling the need to point out that this library supports TypeScript and related features, including path aliases. For configuration, see this package.json, which uses ts-node-dev as a TS dev server and tsc + tsc-alias to transpile the TS code for production.