创建最新的技术库

哈Ha 本文介绍如何使用最新技术编写Hello world。


最后,我们得到一个hello世界库:


  • 使用打字稿
  • 照顾代码风格
  • 产生停靠
  • 进行测试

开始开始


在新文件夹中,启动git和npm


git init npm init 

设置npm时


  package name: (bestlibever) version: (1.0.0) 0.1.0 description: Best lib forever entry point: (index.js) test command: jest git repository: keywords: author: >MAX_ (maximmasterr) license: (ISC) MIT 

项目结构


该项目将具有以下目录:


  • src-这将是库本身的文件(打字稿)
  • lib-这里的tsc将把src中的编译文件
  • 测试-这是测试
  • 示例-使用示例

您还需要添加到.gitignore


 lib/ node_modules/ 

打字稿


现在安装并配置打字稿:


 npm i typescript -D 

并创建一个名为tsconifg.json的文件


  { "compilerOptions": { "target": "es5", "module": "commonjs", "declaration": true, "outDir": "./lib", "strict": true, "sourceMap": true }, "include": ["src"], "exclude": ["node_modules", "**/__tests__/*"] } 

现在让我们将主文件放在src/index.ts


 export function helloWorld(){ return 'Hello world!' } 

还要将脚本添加到package.json


  "build": "tsc", "dev": "tsc --watch" 

现在,我们可以使用以下命令运行一次性构建:


 npm run build 

可重用:


 npm run dev 

代码风格


为了测试代码样式,我们将使用tslint和prettier,并在提交之前运行它。
安装tslint,更漂亮,沙哑:


 npm i tslint tslint-config-prettier prettier husky -D 

通过创建包含内容的.prettierrc文件来配置更漂亮


 { "printWidth": 120, "trailingComma": "none", "singleQuote": true } 

添加脚本以更漂亮地运行


  "prettier": "npx prettier --write src/* test/* example/*" 

通过创建包含内容的tslint.json文件来配置tslint


 { "extends": ["tslint:recommended", "tslint-config-prettier"], "rules": { "curly": false, "ordered-imports": false, "interface-name": [true, "never-prefix"] } } 

添加tslint脚本和代码样式:


  "tslint": "tslint -p tslint -p tsconfig.json", "codestyle": "npm run prettier && npm run tslint" 

现在,在提交并添加到package.json之前,让代码样式运行:


 "husky": { "hooks": { "pre-commit": "npm run codestyle" } } 

该文件


为了从md生成网页,我们使用docsify并从api tsdoc生成文档


安装它们


 npm i docsify-cli typedoc -D 

在docs文件夹中,创建README.md


 # Best lib ever Best lib ever `helloWorld` return `'hello world'` ## Example ``js const a = require('') console.log(a.helloWorld()) // prints 'Hello world!' `` 

同时添加moreExample.md
来源在这里


接下来做


 npx docsify init ./docs 

通过创建sidebar.md文件在docsify中设置边栏


 # Best lib ever * [Best lib ever](/) * [More examples](/moreExamples) 

现在,要了解所有这些美,请添加脚本


 "docsify": "docsify serve ./docs" 

然后跑


 npm run docsify 

现在让我们来看看静态方法文档
首先,在代码中添加对该函数的描述:


 /** * Returns `Hello world!` */ 

.gitinore添加docs/api


为typedoc添加脚本


 "typedoc": "typedoc --out ./docs/api ./src --mode file --readme docs/README.md" 

最后,添加最终的停靠脚本


 "docs": "npm run typedoc && npm run docsify" 

现在一切都足够查看码头


 npm run docs 

测验


安装笑话


 npm install --save-dev @types/jest @types/node jest ts-jest typescript 

让我们创建jest.config.js配置


 module.exports = { roots: ['/src'], transform: { '^.+\\.tsx?$': 'ts-jest', }, testRegex: '(/__test__/.*|(\\.|/)(test|spec))\\.tsx?$', moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'], } 

创建一个测试( test/index.ts ):


 import { helloWorld } from "../src"; test('Should return helloWorld', () => { expect(helloWorld()).toBe('Hello world!') }) 

现在您可以在


 npm test 

总结一下


现在我们可以


 npm run build #   npm run dev #    npm run codestyle #    npm run docs #   npm test #   

最终的github仓库

Source: https://habr.com/ru/post/zh-CN467187/


All Articles