NestJs系列之使用Vite搭建项目

介绍

在使用nest创建项目时,默认使用webpack进行打包,有时候启动项目需要1-2分钟。所以希望采用vite进行快速启动项目进行开发。

本文主要使用NestJs、Vite和swc进行配置。文章实操较多,概念性的东西可访问对应的官方文档进行了解。tips: 个人认为概念性的东西,在文章中指出。对熟悉的人来说直接就实操,节省时间。感兴趣的小伙伴探索性去了解,提升学习乐趣

概念

  1. 什么是NestJS?

官方地址: NestJS - A progressive Node.js framework

中文地址: NestJS 简介 | NestJS 中文文档 | NestJS 中文网 (bootcss.com)

个人理解: NodeJS的Spring Boot. 结合了面向对象,函数式编程和依赖注入的理念,使用NodeJS 搭建后端应用程序。

联想: express、egg、Spring Boot

  1. 什么是Vite?
    官方地址: Vite | Next Generation Frontend Tooling (vitejs.dev)
    中文地址: Vite | 下一代的前端工具链 (vitejs.dev)
  2. 什么是swc?
    官方地址:Rust-based platform for the Web -- SWC

实操

创建项目

参考NestJS 官网

执行命令:

复制代码
$ npm i -g @nestjs/cli
$ nest new project-name

安装完成之后目录结构如下:

在项目的根目录下运行项目

在浏览器上输入localhost:3000可以看到项目的输出:Hello World

安装Vite

复制代码
pnpm add vite vite-plugin-node -D

配置Vite

参考VitePluginNode配置

复制代码
export default defineConfig({
  server: {
    port: 3000,
  },
  plugins: [
    ...VitePluginNode({
      // NodeJs 原生请求适配器
      // 支持'express', 'nest', 'koa' 和 'fastify',
      adapter: 'nest',

      // 项目入口文件
      appPath: './src/main',

      // 在项目入口文件中导出的名字
      exportName: 'appServer',

      // 编译方式: esbuild 和 swc,
      // 默认 esbuild. 但esbuild 不支持 'emitDecoratorMetadata'
      // 使用swc需要安装 `@swc/core`
      tsCompiler: 'swc',
    }),
  ],
  optimizeDeps: {
    exclude: [
        '@nestjs/microservices',
        '@nestjs/websockets',
        'cache-manager', 
        'class-transformer',
        'class-validator', 
        'fastify-swagger'
    ],
  },
});

修改入口文件

复制代码
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';

if (import.meta.env.PROD) {
  async function bootstrap() {
    const app = await NestFactory.create(AppModule);
    await app.listen(3000);
  }
  bootstrap();
}
export const appServer = NestFactory.create(AppModule);

问题总结

  1. 无法识别import.meta

    解决方案:修改tsconfig.json

  2. 无法识别env

    解决方案:可参考vite官网添加env.d.ts

    复制代码
    /// <reference types="vite/client" />
    interface ImportMetaEnv {
      readonly VITE_APP_TITLE: string;
      // more env variables...
    }
    interface ImportMeta {
      readonly env: ImportMetaEnv;
    }
相关推荐
Patrick_Wilson1 天前
Vite+ vs nvm:一次「全局 CLI 失踪」事故引出的 Node 工具链选型
node.js·vite·前端工程化
真夜1 天前
开发正常但生产异常的 Bug:Vite manualChunks 循环依赖导致 ReferenceError
前端·前端框架·vite
豹哥学前端2 天前
前端工程化实战:从包管理到 Vite 配置,一套下来全明白
前端·javascript·vite
晓杰'4 天前
Balatro后端进阶(1):自定义NestJS WebSocket Adapter实现消息拦截
后端·websocket·typescript·node.js·游戏开发·nestjs·wsadapter
天渺工作室4 天前
构建完自动打包压缩,一个插件搞定 Vite / Webpack / Rollup / ESBuild
webpack·vite·rollup.js
Hello--_--World5 天前
Vite:什么是bundleless?哪些要打包,哪些不要打包?依赖预构建是什么?依赖预构建如何减少网络请求的?esbuild 又是什么?
前端·javascript·webpack·vite
晓杰'6 天前
从0到1实现 Balatro 游戏后端(2):NestJS框架搭建与项目结构设计
后端·websocket·typescript·node.js·游戏开发·项目实战·nestjs
VillenK6 天前
版本依赖问题:vite-plugin-dts@3.1.0 与 jiti 的兼容性
前端·typescript·vite
晓杰'8 天前
从0到1实现 Balatro 游戏后端(1):项目规划与牌型判断实现
后端·websocket·typescript·node.js·游戏开发·项目实战·nestjs
用户5757303346248 天前
路由守卫 守卫住网站的安全 也守住我们的幸福
nestjs