码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 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
      无法识别import.meta错误

    解决方案:修改tsconfig.json

    {
     "compilerOptions": {
       "module": "ESNext",
       "declaration": true,
       "moduleResolution": "Node",
       "removeComments": true,
       "emitDecoratorMetadata": true,
       "experimentalDecorators": true,
       "allowSyntheticDefaultImports": true,
       "target": "ES2021",
       "sourceMap": true,
       "outDir": "./dist",
       "baseUrl": "./",
       "incremental": true,
       "skipLibCheck": true,
       "strictNullChecks": true,
       "noImplicitAny": true,
       "strictBindCallApply": true,
       "forceConsistentCasingInFileNames": true,
       "noFallthroughCasesInSwitch": true,
       "types": [
         "vite/client"
       ]
     },
    }
    
    
  • 相关阅读:
    【苍穹外卖 | 项目日记】第一天
    Python中直接赋值、浅拷贝和深拷贝的区别
    【泛函分析】第一纲集和第二纲集
    数字签字那些事+ca数字证书
    Mysql数据库清空表中数据、删除表
    YOLOV8的tensorrt部署详解(目标检测模型-CUDA)
    S曲线步进电机控制算法 ChatGPT
    leetcode链表系列(环形链表篇)
    【mcuclub】时钟模块DS1302
    “高级前端开发技术探索路由的使用及Node安装使用“
  • 原文地址:https://www.cnblogs.com/kingkangstudy/p/17896423.html
  • 最新文章
  • 沪漂五周年了:我越来越迷茫了
    Agentic Skill Routing 实战:别再把所有 Skill 塞进 AI Agent 上下文
    MySQL-Seconds_behind_master的精度误差
    [MAF预定义ChatClient中间件-03]CachingChatClient——利用缓存省钱省时间
    AI的至暗历史:从万众期待到被政府撤资,AI的两次死亡徘徊
    Agent OS :五种驯服不确定性的范式
    PortSwigger SQL注入LAB11
    数据库即时编译JIT
    [Begin]AI Learn Data Day 0
    深度学习进阶(二十七)现代 LLM 的核心架构设计其二:SwiGLU
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
小工具 小游戏
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1

京公网安备 11010502049817号