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
, 不然会出现奇奇怪怪的问题