$ npm init -y
$ npm install -g typescript
$ tsc --init
修改配置如下:
{
"compilerOptions": {
"outDir": "dist",
"module": "umd",
"target": "es5",
"lib": ["es6"],
"sourceMap": true,
"allowJs": true,
"jsx": "react",
"moduleResolution": "node",
"rootDir": "src",
"declaration": true,
"noImplicitReturns": true,
"noImplicitThis": true,
"noImplicitAny": true,
"strictNullChecks": true
},
"exclude": ["node_modules", "dist", "__test__"]
}
配置.eslintignore
#editorconfig.org
root = true
[*]
indent_style = space
indent_size = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_trailing_newline = true
[*.md]
trim_trailing_whitespace = false
$ npm install --save-dev eslint @typescript-eslint/parser @typescript-eslint/eslint-plugin
配置.eslintrc 文件
{
"root": true,
"parser": "@typescript-eslint/parser",
"plugins": ["@typescript-eslint"],
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/eslint-recommended",
"plugin:@typescript-eslint/recommended"
]
}
$ npm run install --save-dev eslint-config-prettier
修改配置.eslintrc 文件
{
"root": true,
"parser": "@typescript-eslint/parser",
"plugins": ["@typescript-eslint"],
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/eslint-recommended",
"plugin:@typescript-eslint/recommended",
// 新增配置
"prettier/@typescript-eslint"
]
}
配置.prettierrc 文件
{
"printWidth": 80,
"tabWidth": 2,
"semi": true,
"singleQuote": true,
"quoteProps": "as-needed",
"trailingComma": "all",
"bracketSpacing": true,
"arrowParens": "always",
"endOfLine": "lf"
}
$ npm install --save-dev jest @type/jest ts-jest eslint-plugin-jest
修改配置.eslintrc 文件
{
"root": true,
"parser": "@typescript-eslint/parser",
"plugins": ["@typescript-eslint"],
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/eslint-recommended",
"plugin:@typescript-eslint/recommended",
"prettier/@typescript-eslint",
// 新增配置
"plugin:jest/recommended"
]
}
配置 jest.config.ts 文件
module.exports = {
preset: 'ts-jest',
globals: {
'ts-jest': {
tsConfig: 'tsconfig.json',
},
},
moduleFileExtensions: ['ts', 'js', 'tsx', 'jsx'],
transform: {
'^.+\\.(ts|tsx)$': 'ts-jest',
},
testMatch: ['**/test/**/*.test.(ts|js)'],
testEnvironment: 'node',
};
$ npm install --save-dev @commitlint/cli @commitlint/config-conventional commitizen husky conventional-changelog conventional-changelog-cli cz-conventional-changelog standard-version lint-staged
配置 package.json 文件
{
// 新增如下
"commitlint": {
"extends": ["@commitlint/config-conventional"]
},
"config": {
"commitizen": {
"path": "./node_modules/cz-conventional-changelog"
}
},
"husky": {
"hooks": {
"pre-commit": "lint-staged"
}
},
"lint-staged": {
"*.{js,md,json}": ["prettier --write", "npm run lint", "git add"],
"*.ts": ["prettier --parser=typescript --write", "npm run lint", "git add"]
}
}