vite-plugin-node icon indicating copy to clipboard operation
vite-plugin-node copied to clipboard

`vite build` does not bundle the dependencies

Open tresabhi opened this issue 2 years ago • 5 comments

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.

tresabhi avatar May 17 '23 01:05 tresabhi

it is solved? i've the same problem

pkhadson avatar Dec 10 '23 18:12 pkhadson

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

npm install -D @swc/core

or any adapter for esbuild

dantzjs avatar Dec 18 '23 20:12 dantzjs

需要一个 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

ljb2458 avatar Apr 15 '24 15:04 ljb2458

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

JokerLHF avatar Aug 09 '24 03:08 JokerLHF

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
})

rxliuli avatar Aug 18 '24 17:08 rxliuli