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

1 首先需要将nest-cli打包的功能通过webpack接管
 
nest-cli.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的基本模块,因此可以直接使用
 
 文件内容, 就按照下面的内容进行修改即可
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, 不然会出现奇奇怪怪的问题