axe-me / vite-plugin-node

Vite plugin to run your node dev server with HMR!
997 stars 48 forks source link

`vite build` does not bundle the dependencies #90

Closed tresabhi closed 2 weeks ago

tresabhi commented 1 year ago

It's quite clear in the title. No matter what I do, it just won't bundle. I can't even find a place to specify bundle: true.

vite.config.ts:

export default defineConfig({
  server: { port: 3000 },
  plugins: [
    ...VitePluginNode({
      adapter: 'express',
      appPath: './src/main.ts',
      exportName: 'viteNodeApp',
      tsCompiler: 'esbuild',
    }),
  ],
  build: {
    target: 'node18',
    minify: 'esbuild',
    sourcemap: 'inline',
    emptyOutDir: true,
  },
  optimizeDeps: {
    esbuildOptions: { treeShaking: true },
  },
});

package.json (scripts):

"scripts": {
    "dev": "vite-node src/main.ts --script",
    "build": "vite build",
    "lint": "tsc"
  },

And the command output if that's needed:

yarn build
yarn run v1.22.19
$ vite build
vite v4.3.7 building SSR bundle for production...
✓ 44 modules transformed.
dist/main.js  143.33 kB │ map: 90.05 kB
✓ built in 393ms
Done in 0.69s.
pkhadson commented 10 months ago

it is solved? i've the same problem

dantzjs commented 10 months ago

A swc plugin is needed. You can replace esbuild builder for swc:

npm install -D @swc/core

or any adapter for esbuild

ljb2458 commented 6 months ago

需要一个 swc 插件。您可以将 esbuild builder 替换为 swc:

npm install -D @swc/核心

或任何用于 esbuild 的适配器

After this modification, there is still no solution

package.json

{
  "name": "fastify-test",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "dev": "vite",
    "tsc": "tsc && node dist/tsc/app.js",
    "build": "vite build"
  },
  "type": "module",
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "@fastify/type-provider-typebox": "^4.0.0",
    "@sinclair/typebox": "^0.32.20",
    "fastify": "^4.26.2"
  },
  "devDependencies": {
    "@swc/core": "^1.4.14",
    "ts-node": "^10.9.2",
    "ts-node-dev": "^2.0.0",
    "typescript": "^5.4.5",
    "vite": "^4.5.3",
    "vite-plugin-node": "^3.1.0"
  }
}
export default defineConfig((config) => {
  return {
    server: {
      port: 3000,
      host: "0.0.0.0",
    },
    base: "./",
    build: {
      target: "node18",
      minify: "esbuild",
      sourcemap: "inline",
      emptyOutDir: true,
    },
    optimizeDeps: {
      esbuildOptions: { treeShaking: true },
    },
    plugins: [
      ...VitePluginNode({
        adapter: "fastify",
        appPath: "src/main.ts",
        exportName: "app",
        tsCompiler: "swc",
      }),
    ],
  };
});

I need help

JokerLHF commented 2 months ago

it is solved? i've the same problem +1

rxliuli commented 2 months ago

it is solved? i've the same problem +1

I ended up using a separate plugin when building

import { defineConfig, mergeConfig, Plugin, UserConfig } from 'vite'
import { VitePluginNode } from 'vite-plugin-node'
import { node } from '@liuli-util/vite-plugin-node'

export default defineConfig((env) => {
  const r: UserConfig = {
    // ...vite configures
    server: {
      // vite server configs, for details see [vite doc](https://vitejs.dev/config/#server-host)
      port: 3000,
    },
    plugins: [],
  }

  if (env.command === 'serve') {
    r.plugins!.push(
      VitePluginNode({
        // Nodejs native Request adapter
        // currently this plugin support 'express', 'nest', 'koa' and 'fastify' out of box,
        // you can also pass a function if you are using other frameworks, see Custom Adapter section
        adapter: 'express',

        // tell the plugin where is your project entry
        appPath: './src/main.ts',

        // Optional, default: 'viteNodeApp'
        // the name of named export of you app from the appPath file
        exportName: 'viteNodeApp',

        // Optional, default: false
        // if you want to init your app on boot, set this to true
        initAppOnBoot: false,

        // Optional, default: 'esbuild'
        // The TypeScript compiler you want to use
        // by default this plugin is using vite default ts compiler which is esbuild
        // 'swc' compiler is supported to use as well for frameworks
        // like Nestjs (esbuild dont support 'emitDecoratorMetadata' yet)
        // you need to INSTALL `@swc/core` as dev dependency if you want to use swc
        tsCompiler: 'esbuild',

        // Optional, default: {
        // jsc: {
        //   target: 'es2019',
        //   parser: {
        //     syntax: 'typescript',
        //     decorators: true
        //   },
        //  transform: {
        //     legacyDecorator: true,
        //     decoratorMetadata: true
        //   }
        // }
        // }
        // swc configs, see [swc doc](https://swc.rs/docs/configuration/swcrc)
        swcOptions: {},
      }),
    )
  } else {
    r.plugins!.push(node({ entry: 'src/main.ts' }))
  }
  return r
})
axe-me commented 2 weeks ago

hi, to bundle dependencies, you can add them into ssr.noExternal option, example:

/// <reference types="vitest" />
import { defineConfig } from 'vite';
import { VitePluginNode } from 'vite-plugin-node';

export default defineConfig({
  server: {
    port: 3606,
  },
  test: {
    globals: true,
  },
  ssr: {
    external: [
      'express', 
    ],
    noExternal: [ // this list is copied from dist/index.js, any module that not node built-in should be copied here
      'typedi',
      'type-graphql',
      'graphql',
      'class-validator',
      'getopts',
      '@apollo/server/express4',
      '@apollo/gateway',
      '@apollo/server',
      'graphql-tag',
      '@apollo/subgraph',
      '@graphql-tools/utils',
      'winston',
      'winston-daily-rotate-file',
      'morgan',
      'graphql-request',
      'axios',
      'jws',
      '@okta/jwt-verifier',
      'graphql-scalars',
      'cors',
      'jose',
      'nanoid',
      'axios-retry',
      'change-case',
      'helmet',
      'papaparse',
      'node-mailjet',
      'zod',
      'langchain',
      'langchain/output_parsers',
      'weaviate-ts-client',
      '@sentry/node',
      'cheerio',
      'turndown',
      'domino',
      'csv-writer',
      'stream-chain',
      'stream-json',
    ],
  },
  plugins: [
    ...VitePluginNode({
      adapter: 'express',
      appPath: './src/index.ts',
      exportName: 'app',
      tsCompiler: 'swc',
      swcOptions: {
        jsc: {
          keepClassNames: true,
        },
      },
    }),
  ],
});

this is really just a question for vite, not related to this plugin, hope this is helpful for you.