vscode配置typescript和eslint的環境

 

一、typescript配置

tsconfig.build.json

{
  "extends": "./tsconfig.json",
  "compilerOptions": {
    "outDir": "./deploy/dist",
  },
  "exclude": ["node_modules", "dist", "test", "**/*spec.ts"]
}

 

tsconfig.json

{
  "compilerOptions": {
    "lib": ["es5", "es6"],
    "target": "es6",
    "module": "commonjs",
    "baseUrl": ".",
    "paths": {
        "@common/*":["src/common/*"],
        "@configuration/*":["src/modules/configuration"],
        "@creative/*": ["src/modules/creative/*"],
        "@admin/*": ["src/modules/admin/*"],
        "@auth/*":["src/modules/auth/*"],
    },
    // 允許編譯javascript文件
    "allowJs": true,
    // 報告.js文件中的錯誤。與allowJs一起使用
    "checkJs": true,
    // "plugins": [],
    // 若有未使用的局部變量則拋錯
    "noUnusedLocals": false,
    // 若有未使用的參數則拋錯
    "noUnusedParameters": false,
    // 類型爲any時,是否需要發出警告,設置true,則不警告
    "noImplicitAny": false,
    // 提供迭代器全面支持
    "downlevelIteration": true,
    // 去掉註解
    "removeComments": true,
    // 從tslib導入外部的輔助方法
    "importHelpers": true,
    // 遇到@internal註解時,不會觸發代碼定義
    "stripInternal": true,
    // 錯誤信息,跟蹤信息將帶有顏色和樣式
    "pretty": true,
    // 如果不是函數中的所有路徑都有返回值,則提示Error
    "noImplicitReturns": true,
    // 允許從沒有設置默認導出的模塊中默認導入
    "allowSyntheticDefaultImports": true,
    // 使用元數據特性
    "emitDecoratorMetadata": true,
    // 支持ES7的裝飾器特性
    "experimentalDecorators": true,
    // 將嚴格校驗switch-case語法
    "noFallthroughCasesInSwitch": true,
    // 嚴格null檢查模式,null和undefined值不包含在任何類型裏
    "strictNullChecks": true,
    // 保存上一次的編譯信息,下一次進行增量更新
    "incremental": false,
    // 不生成定義文件d.ts
    "declaration": false,
    // 生成.map文件
    "sourceMap": false,
    // 跳過默認庫檢查
    "skipLibCheck": true,
    // 輸出文件的根目錄
    "outDir": "./dist",
    // 模塊的解析策略
    // "moduleResolution": "node",
  },
  "include": ["src/**/*", "test/**/*"],
  "exclude": ["node_modules", "dist", "test", "**/*spec.ts"]
}

 

二、lint配置

.eslintrc

{
  "parser": "@typescript-eslint/parser",
  "plugins": ["@typescript-eslint", "prettier"],
  "extends": [
    "eslint:recommended",
    "plugin:@typescript-eslint/eslint-recommended",
    "plugin:@typescript-eslint/recommended",
    "plugin:prettier/recommended",
    "standard",
    "prettier/@typescript-eslint",
    "prettier"
  ],
  "parserOptions": {
    "project": "./tsconfig.json"
  },
  "rules": {
    "no-useless-constructor": "off",
    "@typescript-eslint/indent": ["error", 4, { "VariableDeclarator": 4, "SwitchCase": 1 }],
    "@typescript-eslint/no-unused-vars": ["error", {
        "vars": "all",
        "args": "none",
        "ignoreRestSiblings": true
      }],
    "@typescript-eslint/explicit-member-accessibility": ["error", {"accessibility": "no-public"}],
    "@typescript-eslint/explicit-function-return-type": ["off",
      {
        "allowExpressions": true,
        "allowTypedFunctionExpressions": true
      }],
    "@typescript-eslint/interface-name-prefix": 0,
    "@typescript-eslint/no-explicit-any": "off",
    "@typescript-eslint/no-use-before-define": "off",
    "@typescript-eslint/no-parameter-properties": 0,
    "@typescript-eslint/camelcase": ["off", {"properties": "always"}],
    "no-console": ["warn", { "allow": ["warn", "error"] }],
    "eqeqeq": ["warn", "always"]
  },
  "env": {
    "node": true,
    "es6": true,
    "mocha": true,
    "jest": true
  }
}

tslint.json

{
  "defaultSeverity": "error",
  "extends": ["tslint:recommended", "tslint-config-prettier"],
  "jsRules": {
    "no-unused-expression": true
  },
  "rules": {
    "eofline": false,
    "quotemark": [true, "single"],
    "indent": false,
    "member-access": [false],
    "ordered-imports": [false],
    "max-line-length": [true, 450],
    "member-ordering": [false],
    "curly": false,
    "interface-name": [false],
    "array-type": [false],
    "no-empty-interface": false,
    "no-empty": false,
    "arrow-parens": false,
    "object-literal-sort-keys": false,
    "no-unused-expression": false,
    "max-classes-per-file": false,
    "variable-name": [false],
    "one-line": [false],
    "one-variable-per-declaration": [false],
    "semicolon": [true, "always", "ignore-bound-class-methods"],
    "no-console": [false],
    "space-before-function-paren": false,
    "no-shadowed-variable": false
  },
  "rulesDirectory": []
}

.prettierrc.js  

module.exports = {
    // 一行最多 100 字符
    printWidth: 400,
    // 使用 4 個空格縮進
    tabWidth: 4,
    // 不使用縮進符,而使用空格
    useTabs: false,
    // 行尾需要有分號
    semi: true,
    // 使用單引號
    singleQuote: true,
    // 末尾不需要逗號
    trailingComma: "es5",
    // 大括號內的首尾需要空格
    bracketSpacing: true,
    // 箭頭函數,只有一個參數的時候,也需要括號
    arrowParens: "always",
    parser: 'typescript'
};

  

 

相關依賴

"scripts": {
    "build": "rimraf dist && tsc -p tsconfig.json",
    "format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
    "start": "cross-env NODE_ENV=development ts-node -r tsconfig-paths/register src/main.ts",
    "start:dev": "rimraf dist && cross-env NODE_ENV=development concurrently --handle-input \"wait-on dist/main.js && nodemon\" \"tsc -w -p tsconfig.build.json\" ",
    "start:debug": "nodemon --config nodemon-debug.json",
    "start:dist": "node dist/main.js",
    "pm2:prd": "pm2 start ./bin/app.config.js --env production",
    "pm2:preprd": "pm2 start ./bin/app.config.js --env preproduction",
    "pm2:deve": "pm2 start ./bin/app.config.js --env development",
    "pm2:stop": "pm2 stop youtu-service",
    "pm2:restart": "pm2 restart youtu-service",
    "pm2:delete": "pm2 delete youtu-service",
    "lint": "eslint 'src/**/*.{ts,js}' --fix",
    "lint:ts": "tslint -p tsconfig.json -c tslint.json",
    "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",
    "docs": "compodoc -p tsconfig.json -s"
  },
"devDependencies": {
    "@nestjs/testing": "^6.3.2",
    "@types/es6-shim": "^0.31.39",
    "@types/express": "^4.17.0",
    "@types/jest": "^24.0.15",
    "@types/lodash": "^4.14.135",
    "@types/node": "^12.0.10",
    "@types/supertest": "^2.0.7",
    "@typescript-eslint/eslint-plugin": "^1.11.0",
    "@typescript-eslint/parser": "^1.11.0",
    "concurrently": "^4.1.1",
    "eslint": "^6.0.1",
    "eslint-config-prettier": "^6.0.0",
    "eslint-config-standard": "^12.0.0",
    "eslint-plugin-import": "^2.18.0",
    "eslint-plugin-node": "^9.1.0",
    "eslint-plugin-prettier": "^3.1.3",
    "eslint-plugin-promise": "^4.2.1",
    "eslint-plugin-standard": "^4.0.0",
    "jest": "^24.8.0",
    "nodemon": "^1.19.1",
    "prettier": "^1.18.2",
    "rimraf": "^2.6.3",
    "supertest": "^3.4.2",
    "ts-jest": "^23.10.5",
    "ts-loader": "^4.5.0",
    "ts-node": "^7.0.1",
    "tsconfig-paths": "^3.8.0",
    "tslint": "^5.11.0",
    "tslint-config-prettier": "^1.18.0",
    "typescript": "^3.7.3",
    "wait-on": "^3.2.0"
  },
  "engines": {
    "node": ">=8.9.0"
  },
  "jest": {
    "moduleFileExtensions": [
      "js",
      "json",
      "ts"
    ],
    "rootDir": "src",
    "testRegex": ".spec.ts$",
    "transform": {
      "^.+\\.(t|j)s$": "ts-jest"
    },
    "collectCoverageFrom": [
      "**/*.(t|j)s"
    ],
    "coverageDirectory": "../coverage",
    "testEnvironment": "node"
  }

  

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章