vscode 工程配置

简介: vscode 工程配置

安装必要的开发包

cross-env

$ pnpm add cross-env -D点击复制复制失败已复制


修改 package.json 文件

{
  "scripts": {
    "lint": "cross-env NODE_ENV=production eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
  }
}点击复制复制失败已复制


配置文件部分

.vscode/settings.json 文件

示例:

{
  "editor.detectIndentation": false, // vscode默认启用了根据文件类型自动设置tabsize的选项
  "editor.tabSize": 2, // 重新设定tabsize
  "editor.formatOnSave": true, // #每次保存的时候自动格式化
  // 在使用搜索功能时,将这些文件夹/文件排除在外
  "search.exclude": {
    "**/node_modules": true,
    "**/bower_components": true,
    "**/target": true,
    "**/logs": true,
    "**/dist": true
  },
  // 这些文件将不会显示在工作空间中
  "files.exclude": {
    "**/.git": true,
    "**/.svn": true,
    "**/.hg": true,
    "**/CVS": true,
    "**/.DS_Store": true,
    "**/*.js": {
      "when": "$(basename).ts" //ts编译后生成的js文件将不会显示在工作空中
    },
    "**/node_modules": true
  },
  "[typescript]": {
    "editor.defaultFormatter": "esbenp.prettier-vscode"
  },
  "[json]": {
    "editor.defaultFormatter": "esbenp.prettier-vscode"
  },
  "[markdown]": {
    // 显示空格
    "editor.renderWhitespace": "all",
    // 快速补全
    "editor.quickSuggestions": {
      "other": true,
      "comments": true,
      "strings": true
    },
    // snippet 提示优先
    "editor.snippetSuggestions": "top",
    "editor.tabCompletion": "on",
    // 使用enter 接受提示
    "editor.acceptSuggestionOnEnter": "on",
    "editor.defaultFormatter": "IricBing.iricbing-markdown-formatter"
  },
  "typescript.preferences.importModuleSpecifier": "relative", // 使用相对路径
  "javascript.preferences.importModuleSpecifier": "relative", // 使用相对路径
  "clang-format.executable": "/usr/bin/clang-format", //环境中需要安装 clang-format 软件
  "clang-format.style": "google",
}点击复制复制失败已复制


.vscode/launch.json 文件

{
  // Use IntelliSense to learn about possible attributes.
  // Hover to view descriptions of existing attributes.
  // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
  "version": "0.2.0",
  "configurations": [
    {
      "type": "node",
      "request": "launch",
      "name": "Debug locate master server",
      "runtimeVersion": "14.17.0", // 这里要按照项目的 .nvmrc文件进行修改!
      "args": [
        "${workspaceFolder}/src/main.ts"
      ],
      "runtimeArgs": [
        "--nolazy",
        "-r",
        "ts-node/register"
      ],
      "sourceMaps": true,
      "cwd": "${workspaceRoot}"
    },
    {
      "type": "node",
      "request": "launch",
      "name": "Jest All",
      "runtimeVersion": "14.17.0", // 这里要按照项目的 .nvmrc文件进行修改!
      "program": "${workspaceFolder}/node_modules/jest/bin/jest",
      "args": [
        "--runInBand"
      ],
      "console": "integratedTerminal",
      "internalConsoleOptions": "neverOpen"
    },
    {
      "type": "node",
      "request": "launch",
      "name": "Jest Current File",
      "runtimeVersion": "14.17.0", // 这里要按照项目的 .nvmrc文件进行修改!
      "program": "${workspaceFolder}/node_modules/jest/bin/jest",
      "args": [
        "/${fileBasenameNoExtension}"
      ],
      "console": "integratedTerminal",
      "internalConsoleOptions": "neverOpen"
    },
    {
      "type": "node",
      "request": "launch",
      "name": "Jest e2e test",
      "runtimeVersion": "14.17.0", // 这里要按照项目的 .nvmrc文件进行修改!
      "program": "${workspaceFolder}/node_modules/jest/bin/jest",
      "args": [
        "--config",
        "${workspaceFolder}/test/jest-e2e.json",
        "--runInBand"
      ],
      "console": "integratedTerminal",
      "internalConsoleOptions": "neverOpen"
    },
    {
      "type": "node",
      "request": "launch",
      "name": "Jest current e2e file",
      "runtimeVersion": "14.17.0", // 这里要按照项目的 .nvmrc文件进行修改!
      "program": "${workspaceFolder}/node_modules/jest/bin/jest",
      "args": [
        "/${fileBasenameNoExtension}",
        "--config",
        "${workspaceFolder}/test/jest-e2e.json",
        "--runInBand"
      ],
      "env": {
        "DATABASE_DATABASE": "nine_star_e2e",
        "COMMON_PRINT_USER_ACTIVITY_LOG": "false",
        "COMMON_PRINT_SYSTEM_LOG": "false",
        "SMS_AUTH_IP_THROTTLE": "10000"
      },
      "console": "integratedTerminal",
      "internalConsoleOptions": "neverOpen"
    }
  ]
}点击复制复制失败已复制


.vscode/extensions.json 文件

{
  "recommendations": [
    "streetsidesoftware.code-spell-checker",
    "IricBing.iricbing-markdown-formatter",
    "aaron-bond.better-comments",
    "mikestead.dotenv",
    "dbaeumer.vscode-eslint",
    "plex.vscode-protolint",
    "wayou.vscode-todo-highlight",
    "gruntfuggly.todo-tree",
    "esbenp.prettier-vscode",
    "xaver.clang-format"
  ]
}点击复制复制失败已复制


nest-cli.json 文件

示例:

{
  "$schema": "https://json.schemastore.org/nest-cli",
  "collection": "@nestjs/schematics",
  "sourceRoot": "src",
  "compilerOptions": {
    "plugins": [
      {
        "name": "@iot9x.com/nestjs-swagger",
        "options": {
          "dtoFileNameSuffix": [".dto.ts"],
          "controllerFileNameSuffix": ".controller.ts",
          "dtoKeyOfComment": "description",
          "controllerKeyOfComment": "summary",
          "classValidatorShim": true,
          "introspectComments": true
        }
      }
    ],
    "assets": ["**/*.proto", "**/*.graphql", "**/*.prisma"],
    "watchAssets": true
  }
}点击复制复制失败已复制


提示

这里使用了我们自己魔改版本的 @iot9x.com/nestjs-swagger 代替了原生的 @nestjs/swagger


tsconfig.json 文件

示例:

{
  "compilerOptions": {
    "module": "commonjs",
    "declaration": true,
    "removeComments": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "allowSyntheticDefaultImports": true,
    //node 12.x
    // "lib": [        //当需要高级语法时,扩充此字段
    //   "ES2020"
    // ],
    // "target": "es2019", //这里需要按照当前使用node的版本来选择打包版本
    //node 14.x
    // "target": "es2020",
    //node 16.x
    "target": "es2021",
    "sourceMap": true,
    "outDir": "./dist",
    "baseUrl": "./",
    "incremental": true,
    "skipLibCheck": true,
    "strictNullChecks": false,
    "noImplicitAny": false,
    "strictBindCallApply": false,
    "forceConsistentCasingInFileNames": false,
    "noFallthroughCasesInSwitch": false,
    "esModuleInterop": true
  }
}点击复制复制失败已复制


tsconfig.build.json 文件

示例:

{
  "extends": "./tsconfig.json",
  "include": [  //include 属性是用来避免在开发时更改静态资源(通常为public文件夹下的内容)而导致服务重启的情况,仅监听src文件下所有文件
    "src/**/*"
  ],
  "exclude": [
    "node_modules",
    "test",
    "dist",
    "**/*spec.ts"
  ]
}点击复制复制失败已复制


.prettierrc 文件

示例:

{
  "singleQuote": true,
  "trailingComma": "none",
  "semi": true,
  "printWidth": 160,
  "bracketSpacing": true,
  "arrowParens": "avoid",
  "endOfLine":"auto"
}点击复制复制失败已复制


.eslintrc.js 文件

示例:

module.exports = {
  parser: '@typescript-eslint/parser',
  parserOptions: {
    project: 'tsconfig.json',
    tsconfigRootDir: __dirname,
    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-empty-interface': '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
  },
};点击复制复制失败已复制


.gitignore 文件

/target
/documentation
# environment file
.env
# ignore npm lock file and yarn.lock, use pnpm
package-lock.json
yarn.lock点击复制复制失败已复制


可选配置

提交代码前自动格式化代码

注意:monoRepo 模式不可用,需要使用 lerna 来配置。


安装 huskylint-staged 包(因为需要增加 ENV 参数,所以还是把 cross-env 包装上吧。)

$ pnpm add husky lint-staged cross-env -D 点击复制复制失败已复制


在项目根目录增加 .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 文件,在 scripts 脚本中增加一行命令:

{
  "scripts": {
    "postinstall": "husky install",
  }
}点击复制复制失败已复制


提示

husky 版本进化很快,还破坏性升级,这就有点难受了,如果发现不好使,可能就是升级了,目前版本: 6.0.0


生产环境打包优化

新建 tsconfig.build.prod.json 文件

这是一个新建的文件,主要是为了生产环境构建的时候移除无用信息配置的,这里面还想加入混淆压缩等功能,使用ts官网上的 gulp+uglify 搞了半天没成功。。。

{
  "extends": "./tsconfig.json",
  "compilerOptions": {
    "declaration": false,
    "sourceMap": false,
    "incremental": false
  },
  "exclude": [
    "node_modules",
    "test",
    "dist",
    "**/*spec.ts"
  ]
}点击复制复制失败已复制


修改 package.json 文件,添加如下命令:

{
  "scripts": {
    "build:prod": "rimraf dist && nest build -p tsconfig.build.prod.json",
  }
}点击复制复制失败已复制


以后生产环境构建使用如下命令:

$ pnpm build:prod点击复制复制失败已复制


Docker 支持

新建 .dockerignore 文件

  • 单阶段dist文件夹构建
**/*
!.npmrc
!package.json
!pnpm-lock.yaml
!./dist/点击复制复制失败已复制


  • 多阶段构建
**/*
!.npmrc
!package.json
!pnpm-lock.yaml
!nest-cli.json
!tsconfig.json
!tsconfig.build.json
!tsconfig.build.prod.json
!./src/点击复制复制失败已复制


提示

基于 Drone镜像层处理方案无需 .dockerignore 文件。


新建 Dockerfile 文件

配置信息依环境而定,本笔记不做示例。


Compodoc支持

Compodoc 是一个文档工具,用来生成 NestJS 应用的程序文档,官方说明


  • 安装
$ pnpm add -D @compodoc/compodoc点击复制复制失败已复制


  • 生成文档
$ npx @compodoc/compodoc -p tsconfig.json -s
目录
相关文章
|
5月前
|
Python
新手向 Python:VsCode环境下Manim配置
该文介绍了如何准备和配置开发环境以使用Manim,主要包括两个步骤:一是准备工作,需要下载并安装VsCode和Anaconda,其中Anaconda需添加到系统PATH环境变量,并通过清华镜像源配置;二是配置环境,VsCode中安装中文插件和Python扩展,激活并配置虚拟环境。最后,安装ffmpeg和manim,通过VsCode运行测试代码验证配置成功。
196 1
|
2月前
|
JavaScript 前端开发 编译器
TypeScript教程(一)在vscode中的配置TypeScript环境
本文是一篇TypeScript入门教程,介绍了在VS Code中配置TypeScript环境的步骤,包括安装Node.js、使用npm安装TypeScript、配置npm镜像源、安装VS Code的TypeScript扩展,以及创建和运行一个简单的TypeScript "Hello World"程序。
TypeScript教程(一)在vscode中的配置TypeScript环境
|
20天前
|
JSON 数据格式
vscode配置latex
本文档介绍如何在VSCode中配置LaTeX环境。首先需安装texlive和VSCode,并提前安装SumatraPDF以查看PDF文件。配置包括设置自动编译、PDF预览程序及正反向搜索等。提供了详细的`settings.json`配置示例,涵盖不同操作系统下的编译选项与方案。此外,还介绍了如何配置SumatraPDF实现与VSCode的跳转功能,以及相关快捷键的使用方法。
|
2月前
|
NoSQL 编译器 C语言
VSCode配置配置C++环境
VSCode配置配置C++环境
58 1
|
2月前
|
Dart
Flutter笔记:手动配置VSCode中Dart代码自动格式化
Flutter笔记:手动配置VSCode中Dart代码自动格式化
161 5
|
2月前
|
Rust Shell 开发者
7步开始rust(自定义环境安装与vscode开发环境配置)
本文档详细记录了当改变Rust安装路径至特定目录(如 `/home/local_rust`)时,如何在VSCode环境中正确配置Rust开发环境的过程。主要包括:设置环境变量、使用清华大学镜像安装Rust及更新镜像源、手动部署 `rust-analyzer`、安装标准库源码、安装与配置VSCode插件等七个步骤,确保开发者能够顺利搭建并使用定制化的Rust开发环境。
150 0
|
3月前
|
缓存
如何彻底卸载VSCode及其原来的插件配置缓存
如何彻底卸载VSCode及其原来的插件配置缓存
276 0
|
4月前
cocos creator+vscode安装及配置介绍
cocos creator+vscode安装及配置介绍
167 0
|
5月前
|
C++
VS code debug c代码 配置
VS code debug c代码 配置
53 0
|
5月前
|
编译器 测试技术 C语言
VScode配置C环境概要
VScode配置C环境概要
48 0