nestjs+webpack打包成一个mainjs

nestjs+webpack打包成一个mainjs

tsconfig

需要在paths 中定义需要引入的文件基础位置

js 复制代码
{
  "compilerOptions": {
    "module": "commonjs",
    "declaration": true,
    "removeComments": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "allowSyntheticDefaultImports": true,
    "target": "ES2021",
    "sourceMap": true,
    "outDir": "./dist",
    "baseUrl": "./",
    "incremental": true,
    "skipLibCheck": true,
    "strictNullChecks": false,
    "noImplicitAny": false,
    "strictBindCallApply": false,
    "forceConsistentCasingInFileNames": false,
    "noFallthroughCasesInSwitch": false,
    "paths": {
      "src/*": ["src/*"],
      "src/global/*": ["src/global/*"],
      "src/util/*": ["src/util/*"],
      "src/userModules/*": ["src/userModules/*"]
    }
  }
}

package.json

js 复制代码
 "class-transformer": "0.2.3",
   "buildw": "nest build --webpack --webpackPath=./webpack.config.js"
js 复制代码
{
  "name": "node_nestjs",
  "version": "0.0.1",
  "description": "",
  "author": "",
  "private": true,
  "license": "UNLICENSED",
  "scripts": {
    "build": "nest build",
    "format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
    "start": "nest start",
    "start:dev": "nest start --watch",
    "start:debug": "nest start --debug --watch",
    "start:prod": "node dist/main",
    "lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
    "test": "jest",
    "test:watch": "jest --watch",
    "test:cov": "jest --coverage",
    "test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
    "test:e2e": "jest --config ./test/jest-e2e.json",
    "buildw": "nest build --webpack --webpackPath=./webpack.config.js"
  },
  "main": "dist/src/main.js",
  "bin": "dist/src/main.js",
  "dependencies": {
    "@grpc/grpc-js": "^1.12.4",
    "@grpc/proto-loader": "^0.7.13",
    "@nestjs/common": "^10.0.0",
    "@nestjs/config": "^3.1.1",
    "@nestjs/core": "^10.0.0",
    "@nestjs/jwt": "^10.2.0",
    "@nestjs/mapped-types": "*",
    "@nestjs/microservices": "^10.3.7",
    "@nestjs/passport": "^10.0.2",
    "@nestjs/platform-express": "^10.3.7",
    "@nestjs/platform-socket.io": "^10.2.10",
    "@nestjs/serve-static": "^4.0.0",
    "@nestjs/typeorm": "^10.0.1",
    "@nestjs/websockets": "^10.2.10",
    "@types/mosca": "^2.8.8",
    "ammo.js": "^0.0.10",
    "amqp-connection-manager": "^4.1.14",
    "class-transformer": "0.2.3",
    "exceljs": "^4.4.0",
    "express": "^4.21.1",
    "express-formidable": "^1.2.0",
    "fs": "^0.0.1-security",
    "ioredis": "^5.3.2",
    "kafkajs": "^2.2.4",
    "moment": "^2.29.4",
    "mosca": "^2.8.3",
    "mqtt": "^5.5.2",
    "multer": "^1.4.5-lts.1",
    "mysql": "^2.18.1",
    "nats": "^2.28.2",
    "nest": "^0.1.6",
    "nest-mqtt": "^0.2.0",
    "passport": "^0.6.0",
    "passport-jwt": "^4.0.1",
    "passport-local": "^1.0.0",
    "reflect-metadata": "^0.1.13",
    "rxjs": "^7.8.1",
    "socket.io": "^4.7.2",
    "socket.io-client": "^4.7.2",
    "tsconfig-paths-webpack-plugin": "^4.2.0",
    "typeorm": "^0.3.17",
    "xlsx": "^0.18.5",
    "xlsx-populate": "^1.21.0"
  },
  "devDependencies": {
    "@nestjs/cli": "^10.0.0",
    "@nestjs/schematics": "^10.0.0",
    "@nestjs/testing": "^10.0.0",
    "@types/express": "^4.17.17",
    "@types/jest": "^29.5.2",
    "@types/node": "^20.3.1",
    "@types/passport-jwt": "^3.0.13",
    "@types/passport-local": "^1.0.38",
    "@types/supertest": "^2.0.12",
    "@typescript-eslint/eslint-plugin": "^6.0.0",
    "@typescript-eslint/parser": "^6.0.0",
    "eslint": "^8.42.0",
    "eslint-config-prettier": "^9.0.0",
    "eslint-plugin-prettier": "^5.0.0",
    "jest": "^29.5.0",
    "prettier": "^3.0.0",
    "source-map-support": "^0.5.21",
    "supertest": "^6.3.3",
    "ts-jest": "^29.1.0",
    "ts-loader": "^9.4.3",
    "ts-node": "^10.9.1",
    "tsconfig-paths": "^4.2.0",
    "typescript": "^5.1.3"
  },
  "jest": {
    "moduleFileExtensions": [
      "js",
      "json",
      "ts"
    ],
    "rootDir": "src",
    "testRegex": ".*\\.spec\\.ts$",
    "transform": {
      "^.+\\.(t|j)s$": "ts-jest"
    },
    "collectCoverageFrom": [
      "**/*.(t|j)s"
    ],
    "coverageDirectory": "../coverage",
    "testEnvironment": "node"
  }
}

webpack.config.js

js 复制代码
/* eslint-disable @typescript-eslint/no-var-requires */
const path = require("path");
const webpack = require("webpack");
// fork-ts-checker-webpack-plugin需要单独安装
const ForkTsCheckerWebpackPlugin = require("fork-ts-checker-webpack-plugin");
const TsconfigPathsPlugin = require('tsconfig-paths-webpack-plugin');
module.exports = {
    entry: "./src/main",
    target: "node",
    // 置为空即可忽略webpack-node-externals插件
    //   externals: {},
    externals: [
        // 添加需要忽略的模块
        'vertx',
        // 其他模块
    ],
    // ts文件的处理
    module: {
        rules: [
            {
                test: /\.ts?$/,
                use: {
                    loader: "ts-loader",
                    options: { transpileOnly: true }
                },
                exclude: /node_modules/
            }
        ]
    },
    // 打包后的文件名称以及位置
    output: {
        filename: "main.js",
        path: path.resolve(__dirname, "dist")
    },
    optimization: {
        usedExports: false,
    },
    resolve: {
        extensions: ['.js', '.ts', '.json'],
        plugins: [new TsconfigPathsPlugin({ configFile: './tsconfig.json' })],
    },
    plugins: [
        // 需要进行忽略的插件
        new webpack.IgnorePlugin({
            checkResource(resource) {
                const lazyImports = [
                    "@nestjs/microservices",
                    "@nestjs/microservices/microservices-module",
                    "@nestjs/websockets/socket-module",
                    "cache-manager",
                    "class-validator",
                    "class-transformer"
                ];
                if (!lazyImports.includes(resource)) {
                    return false;
                }
                try {
                    require.resolve(resource, {
                        paths: [process.cwd()]
                    });
                } catch (err) {
                    return true;
                }
                return false;
            }
        }),
        new ForkTsCheckerWebpackPlugin()
    ]
};
相关推荐
文心快码BaiduComate几秒前
Comate Spec Mode能力升级:让复杂任务开发更可控、更稳定
前端·后端
前端付豪4 分钟前
实现 AI 回复支持 Markdown 渲染
前端·人工智能·markdown
阳火锅15 分钟前
鳌虾 AoCode:重新定义 AI 编程助手的下一代可视化工具
前端·人工智能·架构
拾贰_C18 分钟前
【node】node彻底卸载删除
前端
TON_G-T19 分钟前
uniapp-解放主包,组件下沉分包插件
webpack·uni-app
SuperEugene19 分钟前
Vue3 组合式函数(Hooks)封装规范实战:命名 / 输入输出 / 复用边界 + 避坑|Vue 组件与模板规范篇
开发语言·前端·javascript·vue.js·前端框架
芝士麻雀22 分钟前
掌握 .claude/ 目录:让 Claude Code 真正懂你的项目
前端·后端
cmd23 分钟前
JS深浅拷贝全解析|常用方法+手写实现+避坑指南(附完整代码)
前端·javascript
进击的尘埃23 分钟前
AbortController 实战:竞态取消、超时兜底与请求生命周期管理
前端·javascript
张一凡9324 分钟前
我用 Zustand 三年了,直到遇见 easy-model...
前端·javascript·react.js