提示
我们是 pnpm
生态,因此以下的配置均基于 pnpm
,使用 npm
和 yarn
的请按需自己修改。
Step1. 新建项目
$ nest new nestjs-mp-cluster # 使用pnpm点击复制复制失败已复制
Step2. 修改 package.json 文件
在 package.json
文件中增加如下配置:
{ "files": [ "dist/**/*", "pnpm-lock.yaml" ], "main": "./dist/index.js", "types": "./dist/index.d.ts", "keywords": [ "nestjs", "微信小程序", "多个微信小程序", "typescript" ], }点击复制复制失败已复制
说明:
其中 files
, main
, types
字段必填, keywords
可选。
修改 script
属性内容
{ "scripts": { "format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"", "build:npm": "rimraf dist && nest build -p tsconfig.npm.build.json", "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" }, "peerDependencies": { "@nestjs/common": "8.x", "@nestjs/core": "8.x" }, }点击复制复制失败已复制
将 dependencies
中的依赖全部移到 devDependencies
中去。
删除 private
字段。因为我们发布的都是公共npm
包, private
包需要花钱购买服务,不删除此字段无法发布。
Step3. 修改tsconfig.json文件
{ "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, "esModuleInterop": true } }点击复制复制失败已复制
提示
修改地方主要是 target
和 esModuleInterop
,因为我们目前都是 node16
环境,因此这里 target
写成了 es2021
,如果需要兼容更低版本的 node
环境,这里需要相应调整。
Step4. 修改 tsconfig.build.json 文件
将 tsconfig.build.json
文件改为以下内容:
{ "extends": "./tsconfig.json", "exclude": [ "node_modules", "test", "dist", "**/*.d.ts", "**/*spec.ts" ] }点击复制复制失败已复制
Step5. 新建 tsconfig.npm.build.json 文件
在项目根目录下新建文件 tsconfig.npm.build.json
,写入以下内容:
{ "extends": "./tsconfig.json", "compilerOptions": { "removeComments": false, "sourceMap": false, "incremental": false }, "exclude": [ "node_modules", "test", "dist", "**/*.d.ts", "**/*spec.ts" ] }点击复制复制失败已复制
Step6. 开发
开始开发,调试等,具体见 开发详解
Step7. 编译
$ pnpm build:npm点击复制复制失败已复制
Step8. 发布
警告
发布之前一定要看看 package.json
文件中的 version
字段,npm会根据这个字段给包打上版本。
$ npm login # 注意,需要将npm源切换回npm $ npm publish --access=public 点击复制复制失败已复制
扩展
可选操作
.eslintrc.js 文件配置
module.exports = { parser: '@typescript-eslint/parser', parserOptions: { project: 'tsconfig.json', sourceType: 'module', }, plugins: ['@typescript-eslint/eslint-plugin'], extends: [ 'plugin:@typescript-eslint/recommended', 'plugin:prettier/recommended', 'prettier', ], root: true, env: { node: true, jest: true, }, ignorePatterns: ['.eslintrc.js'], rules: { '@typescript-eslint/interface-name-prefix': 'off', '@typescript-eslint/explicit-function-return-type': 'off', '@typescript-eslint/explicit-module-boundary-types': 'off', '@typescript-eslint/no-explicit-any': 'off', "@typescript-eslint/no-empty-interface": 'off', "@typescript-eslint/ban-types": 'off', "@typescript-eslint/no-unused-vars": ['warn', { "argsIgnorePattern": "^_" }], 'no-console': process.env.NODE_ENV === 'production' ? ['error', { allow: ['warn', 'error'] }] : 'off', 'no-debugger': process.env.NODE_ENV === 'production' ? 'warn' : 'off', 'no-var': 0, "no-unused-vars": ['warn', { "argsIgnorePattern": "^_" }], 'prefer-rest-params': 0 }, };点击复制复制失败已复制
.prettierrc 文件配置
{ "singleQuote": true, "trailingComma": "none", "semi": true, "printWidth": 160, "bracketSpacing": true, "arrowParens": "avoid", "endOfLine": "auto" }点击复制复制失败已复制
删除 test 文件夹
test
文件夹里面主要是 e2e
测试(汉语名称为: 端对端测试
)
一般插件用不到端对端测试,这部分可以删除。
提交代码前自动格式化代码
安装 husky
和 lint-staged
包
$ pnpm add -D husky lint-staged cross-env 点击复制复制失败已复制
在项目根目录增加 .lintstagedrc.json
文件,写入如下内容:
{ "src/**/*.ts": [ "prettier --write \"src/**/*.ts\"", "cross-env NODE_ENV=production eslint \"{src,apps,libs,test}/**/*.ts\" --fix", "git add ." ], "test/**/*.ts": [ "prettier --write \"test/**/*.ts\"", "cross-env NODE_ENV=production eslint \"{src,apps,libs,test}/**/*.ts\" --fix", "git add ." ] }点击复制复制失败已复制
接下来配置 husky
,输入如下命令:
$ pnpm husky install $ pnpm husky add .husky/pre-commit "npx lint-staged"点击复制复制失败已复制
警告
不要在 package.json
文件中配置 postinstall
脚本进行 husky install
,此脚本会影响插件的安装!
提示
husky
版本进化很快,还破坏性升级,这就有点难受了,如果发现不好使,可能就是升级了,目前版本: 6.0.0