NestJs和Vite使用monorepo管理项目中,需要使用共享的文件夹步骤

NestJsVite使用monorepo管理项目中,需要使用共享的文件夹步骤

1 首先需要将nest-cli打包的功能通过webpack接管

nest-cli.json文件内容

json 复制代码
{
  "$schema": "https://json.schemastore.org/nest-cli",
  "collection": "@nestjs/schematics",
  "sourceRoot": "src",
  "compilerOptions": {
    "webpack": true,
    "deleteOutDir": true,
    "tsConfigPath": "./tsconfig.build.json"
  }
}

根目录创建webpack.config.js文件. 使用nest-cli创建项目时, 已经安装了webpack的基本模块,因此可以直接使用

文件内容, 就按照下面的内容进行修改即可

js 复制代码
const path = require("path")
const webpack = require("webpack")
const CopyPlugin = require("copy-webpack-plugin")

const sharedDirPath = path.resolve(__dirname, "../shared")
module.exports = {
  entry: "./src/main.ts",
  watch: true,
  target: "node",
  module: {
    rules: [
      {
        test: /\.ts?$/,
        use: "ts-loader",
        exclude: /node_modules/
      }
    ]
  },
  mode: "development",
  resolve: {
    extensions: [".tsx", ".ts", ".js"],
    alias: {
      "@shared": sharedDirPath
    }
  },
  plugins: [
    new webpack.HotModuleReplacementPlugin(),
    new CopyPlugin({
      patterns: [
        {
          from: sharedDirPath,
          to: "shared"
        }
      ]
    })
  ],
  output: {
    path: path.join(__dirname, "dist"),
    filename: "main.js"
  }
}

注意copy-webpack-plugin的版本需要指定, 这里webpack的版本是5.73.0安装copy-webpack-plugin@9.1.0即可
pnpm add copy-webpack-plugin@9.1.0 -D, 不然会出现奇奇怪怪的问题

相关推荐
浩~~6 分钟前
反射型XSS注入
前端·xss
AwesomeDevin12 分钟前
AI时代,我们的任务不应沉溺于与 AI 聊天,🤔 从“对话式编程”迈向“数字软件工厂”
前端·后端·架构
harrain18 分钟前
antvG2折线图和区间range标记同时绘制
前端·javascript·vue.js·antv·g2
德育处主任Pro24 分钟前
从重复搭建到高效生产,RollCode的H5开发新范式
前端
蜡台1 小时前
SPA(Single Page Application) Web 应用(即单页应用)架构模式 更新
前端·架构·vue·react·spa·spa更新
网络点点滴2 小时前
组件通信-作用域插槽
前端·javascript·vue.js
kyriewen113 小时前
异步编程:从“回调地狱”到“async/await”的救赎之路
开发语言·前端·javascript·chrome·typescript·ecmascript·html5
Old Uncle Tom3 小时前
Markdown Viewer 再升级
前端
Luna-player3 小时前
Vue3中使用vue-awesome-swiper
前端·vue.js·arcgis
SuperEugene3 小时前
Vue3 Pinia 状态管理规范:状态拆分、Actions 写法、持久化实战,避坑状态污染|状态管理与路由规范篇
前端·javascript·vue.js·前端框架·pinia