nksaraf / vinxi

The Full Stack JavaScript SDK
https://vinxi.vercel.app
MIT License
2.09k stars 80 forks source link

Building with esnext target causes server to not be runnable #179

Open lithdew opened 9 months ago

lithdew commented 9 months ago

I was looking to use top-level await in my application which appears to only be available on targets ES2022/ESNext. The server built by default targets ES2019.

app.config.ts:

import { TanStackRouterVite } from "@tanstack/router-vite-plugin";
import reactRefresh from "@vitejs/plugin-react";
import { createApp } from "vinxi";

export default createApp({
    routers: [
        {
            name: "public",
            type: "static",
            dir: "./public",
            base: "/",
        },
        {
            name: "trpc",
            type: "http",
            handler: "./src/entry-trpc.tsx",
            target: "server",
            base: "/trpc",
        },
        {
            name: "ssr",
            type: "http",
            handler: "./src/entry-server.tsx",
            target: "server",
            plugins: () => [reactRefresh(), TanStackRouterVite()],
        },

        {
            name: "client",
            type: "client",
            handler: "./src/entry-client.tsx",
            target: "browser",
            plugins: () => [reactRefresh(), TanStackRouterVite()],
            base: "/_build",
        },
    ],
    server: {
        esbuild: {
            options: {
                target: "es2022",
            }
        },
        experimental: {
            asyncContext: true,
        },
    },
});

Running node .output/server/index.mjs on the built output causes the server to exit immediately on startup. I am using Node v18.16. In dev mode, the application runs fine.

nksaraf commented 9 months ago

Any error messages or logs ? Need more info

lithdew commented 9 months ago

Any error messages or logs ? Need more info

There appears to be no error messages or logs.

Here's a reproduction: https://stackblitz.com/edit/vinxi-test?file=src%2Frouter.tsx

The top-level await happens in src/router.tsx in export const trpc = ....

Run npm run build && node .output/server/index.mjs and the server will immediately close on startup.

Running npm run dev however works.

shiro commented 8 months ago

I'm also able to run my solid start project in dev, but building and running production just exits without any message.

AlexErrant commented 2 months ago

Is this an esbuild limitation? https://github.com/evanw/esbuild/issues/253

fgsreally commented 1 week ago

in my case.when set target to esnext or support top-level-await,it can build but can't start error is:

File URL path must be absolute    

look at compiled code

globalThis._importMeta_=globalThis._importMeta_||{url:"file:///_entry.js",env:process.env};
//...
var __filename = fileURLToPath(globalThis._importMeta_.url);

it seems that esbuild doesn't handle import.meta correctly

even if I set import-meta to true ,it doesn't work esbuild_supported

const serverConfig = {
  esbuild: {
    options: {
      supported: {
        'top-level-await': true,
        'import-meta': true,
      },
    },
  },
}

I remove all import.meta in source code and now it work