Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue importing local TS files from entry #21

Open
giacomorebonato opened this issue Apr 13, 2023 · 10 comments · May be fixed by #38
Open

Issue importing local TS files from entry #21

giacomorebonato opened this issue Apr 13, 2023 · 10 comments · May be fixed by #38

Comments

@giacomorebonato
Copy link

The following is the code my entry points to:

import type { FastifyCookieOptions } from '@fastify/cookie';
import type { DefineFastifyRoutes } from '@matthewp/astro-fastify';
import { fastifyTRPCPlugin } from '@trpc/server/adapters/fastify';

import { createApiContext } from './api-context.js' // fails;
import { env } from './env.js';                                     // fails
import { apiRouter } from './routes/index.js';           // fails

const defineRoutes: DefineFastifyRoutes = async (fastify) => {
  const cookieOption: FastifyCookieOptions = {
    secret: env.JWT_SECRET,
  }

  fastify
    .register(import('fastify-graceful-shutdown'))
    .register(import('@fastify/websocket'))
    .register(import('@fastify/cookie'), cookieOption)
    .register(import('@fastify/csrf-protection'))
    .register(fastifyTRPCPlugin, {
      prefix: '/trpc',
      useWSS: true,
      trpcOptions: { router: apiRouter, createContext: createApiContext },
    })
};

export default defineRoutes;

All the local imports fail with:

Failed to load url ./api-context.ts (resolved id: ./api-context.ts). Does the file exist?

Do you know what's happening?

@tinchoz49
Copy link

I'm having the same issue. It seems that the vite.ssrLoadModule (https://github.com/matthewp/astro-fastify/blob/main/lib/index.js#LL53C42-L53C55) is not respecting the relative path. If you want to try to load the files from the root it works.

Like if you have the api located in: ./api/index.ts try to load the rest of the paths as import { createApiContext } from './api/api-context.js'

anyway is not a good solution, i know

@smnbbrv
Copy link
Contributor

smnbbrv commented Jun 15, 2023

the kinda solution is to put main.ts (the file where defineRoutes is called) in the project root. This automatically makes your imports "from root".

@npup
Copy link

npup commented Sep 24, 2023

@smnbbrv Hi! I'd like to use your tip, but the problem seems to resurface as soon as a relative import is used, so it is hard to modularize any serious fastify related code used from this entry point.
What would really need to be fixed regarding the mentioned ssrLoadModule thing (if @tinchoz49's assumption in #21 (comment) is correct)? I'd love to help.

@craigharman
Copy link

+1 for a fix for this. Such a great dev experience having HMR!

@matthewp
Copy link
Owner

I don't understand this issue. Are you all passing a URL to entry like in the example? If you're passing ./entry.ts or something then I would expect it not to work.

@craigharman
Copy link

I've attached a minimal recreation for you here. But basically to recreate the issue I:

  1. Install astro (npm create astro@latest)
  2. Install astro-fastify as per readme (everything works fine up to this point)
  3. Add a new folder ./api/lib and create a file todo.ts.
  4. Import file from 3 inside ./api/index.ts
  5. Restart everything and receive the error 14:42:42 [ERROR] [content] Failed to load url ./lib/todos (resolved id: ./lib/todos). Does the file exist?

Archive.zip
To use the zip file, just npm install

@matthewp
Copy link
Owner

Thanks for the reproduction! I'm not sure what would cause this, but I think I have a good idea to fix it either way.

@matthewp
Copy link
Owner

I've looked into this a bit but haven't been able to figure it out yet. Inside Vite it seems to resolving these modules without providing the parent module to resolve relative, and I'm not sure why it's doing that. Will keep you updated.

@tinchoz49
Copy link

thank you @matthewp !

@patheticGeek
Copy link

patheticGeek commented May 10, 2024

I am unable to find any related issue in the vite repo, and while trying to create a minimal reproduction using createViteServer as described in vite docs the imports work fine. stackblitz

Could this be related to astro's vite config or at astro level? or can someone link me to an existing issue? or should i file one in astro and see where that goes?

Trying to move to astro at work but this import issue is the only blocker for a week.

thanks for this adapter & astro tho :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants