aleclarson / vite-tsconfig-paths

Support for TypeScript's path mapping in Vite
MIT License
1.28k stars 45 forks source link

Doesn't work with custom server #21

Closed thebuilder closed 3 years ago

thebuilder commented 3 years ago

I'm running Vite dev with a custom Express server, and after upgrading vite-tsconfig-paths from version 2.5.1 it breaks. Seems to work if just running vite dev.

./node_modules/vite-tsconfig-paths/dist/index.js:124
                                    done(viteResolve(path, importer));
                                         ^

TypeError: viteResolve is not a function
    at ./node_modules/vite-tsconfig-paths/dist/index.js:124:42
    at ./node_modules/tsconfig-paths/lib/match-path-async.js:72:24
    at ./node_modules/tsconfig-paths/lib/filesystem.js:46:9
    at FSReqCallback.oncomplete (fs.js:184:5)

viteResolve is set in buildStart, but this function is never called. https://github.com/aleclarson/vite-tsconfig-paths/blob/master/src/index.ts#L21

It breaks the moment I try to load a module using vite.ssrLoadModule. I've tried trim the server.mjs file down to the essentials:

server.mjs

import { createServer as createViteServer } from "vite";
import express from "express";

export async function createServer(root = process.cwd()) {
  const app = express();

  const vite = await createViteServer({
    root,
    logLevel: "info",
    server: {
      middlewareMode: true
    }
  });

  app.use(vite.middlewares);

  app.use("*", async (req, res, next) => {
    await vite.ssrLoadModule("/src/entry-server.ts");

    res.status(200).end("output");
  });

  return { app, vite };
}
async function init() {
  const { app } = await createServer();

  app.listen(3000, () => {});
}

init();
aleclarson commented 3 years ago

I haven't tested this plugin in Vite's middleware mode.

Can you fork the master branch and setup the demo folder with your bare bones example? That would help a ton!

thebuilder commented 3 years ago

I've added a server example to your demo folder: https://github.com/thebuilder/vite-tsconfig-paths/tree/bug/middleware-demo

Run yarn serve (or node server.mjs) to start it.

I'm guessing it should possible to write a Jest test for it, by just creating the Vite server and loading a module:

import { createServer } from "vite";

const vite = await createServer({
    server: {
      middlewareMode: true
    }
 });
await vite.ssrLoadModule("/src/entry-server.ts");
aleclarson commented 3 years ago

Thanks!

I've submitted a PR to vite that fixes this. https://github.com/vitejs/vite/pull/3080

chrisvariety commented 3 years ago

Looks like this is resolved w/ vite@2.2.4 !

Edit: actually seems like it is still happening on production build only?

#9 41.98 vite v2.2.4 building SSR bundle for production...
#9 42.35 /app/node_modules/vite-tsconfig-paths/dist/index.js:58
#9 42.35                             done(viteResolve(path, importer));
#9 42.35                                  ^
#9 42.35
#9 42.35 TypeError: viteResolve is not a function
#9 42.35     at /app/node_modules/vite-tsconfig-paths/dist/index.js:58:34
#9 42.35     at /app/node_modules/tsconfig-paths/lib/match-path-async.js:72:24
#9 42.35     at /app/node_modules/tsconfig-paths/lib/filesystem.js:46:9
#9 42.35     at FSReqCallback.oncomplete (fs.js:184:5)
aleclarson commented 3 years ago

@chrisvariety That looks unrelated, and I can't reproduce it. Please open a new issue with a repository link for reproducing it. 👍

thebuilder commented 3 years ago

Looks like this is resolved w/ vite@2.2.4 !

Edit: actually seems like it is still happening on production build only?

#9 41.98 vite v2.2.4 building SSR bundle for production...
#9 42.35 /app/node_modules/vite-tsconfig-paths/dist/index.js:58
#9 42.35                             done(viteResolve(path, importer));
#9 42.35                                  ^
#9 42.35
#9 42.35 TypeError: viteResolve is not a function
#9 42.35     at /app/node_modules/vite-tsconfig-paths/dist/index.js:58:34
#9 42.35     at /app/node_modules/tsconfig-paths/lib/match-path-async.js:72:24
#9 42.35     at /app/node_modules/tsconfig-paths/lib/filesystem.js:46:9
#9 42.35     at FSReqCallback.oncomplete (fs.js:184:5)

@chrisvariety Are you running Windows? Got a developer running Windows that gets this issue, while the same code builds fine on my machine. Something to investigate

chrisvariety commented 3 years ago

@thebuilder nope, Mac over here. I'll try to put together a minimal reproduction.

thebuilder commented 3 years ago

Interesting - Could it be an async execution order issue?🤔