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()
    ]
};
相关推荐
桂月二二22 分钟前
探索前端开发中的 Web Vitals —— 提升用户体验的关键技术
前端·ux
hunter2062062 小时前
ubuntu向一个pc主机通过web发送数据,pc端通过工具直接查看收到的数据
linux·前端·ubuntu
qzhqbb2 小时前
web服务器 网站部署的架构
服务器·前端·架构
刻刻帝的海角2 小时前
CSS 颜色
前端·css
浪浪山小白兔3 小时前
HTML5 新表单属性详解
前端·html·html5
lee5763 小时前
npm run dev 时直接打开Chrome浏览器
前端·chrome·npm
2401_897579653 小时前
AI赋能Flutter开发:ScriptEcho助你高效构建跨端应用
前端·人工智能·flutter
limit for me4 小时前
react上增加错误边界 当存在错误时 不会显示白屏
前端·react.js·前端框架
浏览器爱好者4 小时前
如何构建一个简单的React应用?
前端·react.js·前端框架
qq_392794484 小时前
前端缓存策略:强缓存与协商缓存深度剖析
前端·缓存