9.0.29 • Published 4 years ago

quboqin-lib-typescript v9.0.29

Weekly downloads
-
License
ISC
Repository
-
Last release
4 years ago

Building and publishing an npm typescript package

  1. 创建目录
mkdir lib-typescript && cd lib-typescript
  1. 初始化 git 仓库
git init
git checkout -b main
echo "# Building and publishing an npm typescript package" >> README.md
git add . && git commit -m "Initial commit"
  1. 初始化 npm 项目
npm init -y
echo "node_modules" >> .gitignore
npm install --save-dev typescript
  1. 配置 tsconfig.json
{
  "compilerOptions": {
    "target": "es2017",
    "module": "commonjs",
    "outDir": "./lib",
    "sourceMap": true,
    "strict": true,
    "noImplicitAny": true,
    "declaration": true,
    "strictPropertyInitialization": false,
    "experimentalDecorators": true /* Enables experimental support for ES7 decorators. */,
    "emitDecoratorMetadata": true /* Enables experimental support for emitting type metadata for decorators. */
  },
  "include": ["src"],
  "exclude": ["node_modules", "**/__tests__/*"]
}

5.ignore /lib

echo "/lib" >> .gitignore

配置 eslint

  1. 安装 eslint 和 typescript 相关依赖
npm i eslint @typescript-eslint/parser @typescript-eslint/eslint-plugin --save-dev
  1. 创建 .eslintrc.js
module.exports = {
  parser: '@typescript-eslint/parser',
  extends: ['plugin:@typescript-eslint/recommended', 'prettier/@typescript-eslint', 'plugin:prettier/recommended'],
  parserOptions: {
    ecmaVersion: 2018,
    sourceType: 'module',
  },
  rules: {},
}
  1. 安装 prettier
npm i prettier eslint-config-prettier eslint-plugin-prettier -D
  1. 创建 .prettierrc.js
module.exports = {
  semi: false,
  trailingComma: 'all',
  singleQuote: true,
  printWidth: 120,
  tabWidth: 2,
}
  1. 配置 vscode workspace
{
  "editor.formatOnSave": false,
  "editor.codeActionsOnSave": {
    "source.fixAll.eslint": true
  },
}
  1. 添加 .eslintignore
node_modules
/lib

配置 npm 库的输出文件

However, blacklisting files is not a good practice. Every new file/folder added to the root, needs to be added to the .npmignore file as well! Instead, you should whitelist the files /folders you want to publish. This can be done by adding the files property in package.json:

  "files": [
    "lib/**/*"
  ],

Setup Testing with Jest

  1. 安装 Jest
npm install --save-dev jest ts-jest @types/jest
  1. Create a new file in the root and name it jestconfig.json:
{
  "transform": {
    "^.+\\.(t|j)sx?$": "ts-jest"
  },
  "testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$",
  "moduleFileExtensions": ["ts", "tsx", "js", "jsx", "json", "node"]
}
  1. Remove the old test script in package.json and change it to:
"test": "jest --config jestconfig.json",
  1. Write a basic test In the src folder, add a new folder called tests and inside, add a new file with a name you like, but it has to end with test.ts, for example greeter.test.ts
import { Greeter } from '../index';
test('My Greeter', () => {
  expect(Greeter('Carl')).toBe('Hello Carl');
});

then try to run

npm test

Use the magic scripts in NPM

  1. 修改脚本
    "prepare": "npm run build",
    "prepublishOnly": "npm test && npm run lint",
    "preversion": "npm run lint",
    "version": "git add -A src",
    "postversion": "git push && git push --tags",
    "patch": "npm version patch && npm publish"
  1. Updating your published package version number To change the version number in package.json, on the command line, in the package root directory, run the following command, replacing <update_type> with one of the semantic versioning release types (patch, major, or minor):
npm version <update_type>
  1. Run npm publish.
9.0.28

4 years ago

9.0.27

4 years ago

9.0.29

4 years ago

9.0.24

4 years ago

9.0.26

4 years ago

9.0.25

4 years ago

9.0.16

4 years ago

9.0.19

4 years ago

9.0.18

4 years ago

9.0.15

4 years ago

9.0.14

4 years ago

9.0.23

4 years ago

9.0.20

4 years ago

9.0.22

4 years ago

9.0.21

4 years ago

9.0.9

4 years ago

9.0.8

4 years ago

9.0.7

4 years ago

9.0.6

4 years ago

9.0.5

4 years ago

9.0.4

4 years ago

9.0.3

4 years ago

9.0.13

4 years ago

9.0.12

4 years ago

9.0.11

4 years ago

9.0.10

4 years ago

9.0.2

4 years ago

9.0.1

4 years ago

9.0.0

4 years ago

1.2.0

4 years ago

1.2.2

4 years ago

1.2.1

4 years ago

7.0.23

4 years ago

2.0.3

4 years ago

7.0.24

4 years ago

2.0.2

4 years ago

7.0.21

4 years ago

2.0.5

4 years ago

7.0.22

4 years ago

2.0.4

4 years ago

7.0.20

4 years ago

2.0.6

4 years ago

2.0.1

4 years ago

2.0.0

4 years ago

7.0.8

4 years ago

7.0.7

4 years ago

7.0.5

4 years ago

7.0.9

4 years ago

4.0.4

4 years ago

4.0.10

4 years ago

4.0.6

4 years ago

4.0.0

4 years ago

4.0.2

4 years ago

1.1.0

4 years ago

4.0.8

4 years ago

1.3.0

4 years ago

7.0.0

4 years ago

7.0.4

4 years ago

7.0.3

4 years ago

7.0.2

4 years ago

7.0.1

4 years ago

7.0.12

4 years ago

7.0.10

4 years ago

7.0.11

4 years ago

7.0.18

4 years ago

7.0.19

4 years ago

7.0.16

4 years ago

7.0.17

4 years ago

7.0.14

4 years ago

7.0.15

4 years ago

1.0.8

4 years ago

1.0.7

4 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago

0.2.0

4 years ago

0.1.1

4 years ago