nestjs+webpack打包成一个mainjs
nestjs+webpack打包成一个mainjs
tsconfig
需要在paths 中定义需要引入的文件基础位置
{
"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
"class-transformer": "0.2.3",
"buildw": "nest build --webpack --webpackPath=./webpack.config.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
/* 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()
]
};
原文地址:https://blog.csdn.net/hacker12356/article/details/144360823
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!