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

Doesn't work with custom server #21

Closed
thebuilder opened this issue Apr 21, 2021 · 8 comments · Fixed by vitejs/vite#3080
Closed

Doesn't work with custom server #21

thebuilder opened this issue Apr 21, 2021 · 8 comments · Fixed by vitejs/vite#3080
Labels
enhancement New feature or request

Comments

@thebuilder
Copy link
Contributor

thebuilder commented Apr 21, 2021

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
Copy link
Owner

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!

@aleclarson aleclarson added the enhancement New feature or request label Apr 21, 2021
@thebuilder
Copy link
Contributor Author

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
Copy link
Owner

Thanks!

I've submitted a PR to vite that fixes this.
vitejs/vite#3080

@chrisvariety
Copy link

chrisvariety commented May 3, 2021

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
Copy link
Owner

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

@thebuilder
Copy link
Contributor Author

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
Copy link

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

@thebuilder
Copy link
Contributor Author

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

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

Successfully merging a pull request may close this issue.

3 participants