{"_id":"@boombox/npm-ts-template","name":"@boombox/npm-ts-template","dist-tags":{"latest":"1.0.0"},"versions":{"1.0.0":{"name":"@boombox/npm-ts-template","version":"1.0.0","description":"A template with typescript for a npm package","main":"index.js","scripts":{"test":"jest --config jestconfig.json","build":"tsc","format":"prettier --write \"src/**/*.ts\" \"src/**/*.js\"","lint":"tslint -p tsconfig.json","prepare":"npm run build","prepublishOnly":"npm test && npm run lint","preversion":"npm run lint","version":"npm run format && git add -A src","postversion":"git push && git push --tags"},"repository":{"type":"git","url":"git+https://github.com/Penggeor/boombox.git"},"keywords":["localStorage","sessionStorage"],"author":{"name":"penggeor"},"license":"ISC","bugs":{"url":"https://github.com/Penggeor/boombox/issues"},"homepage":"https://github.com/Penggeor/boombox#readme","devDependencies":{"@types/jest":"^29.5.12","jest":"^29.7.0","prettier":"^3.2.5","ts-jest":"^29.1.2","tslint":"^6.1.3","tslint-config-prettier":"^1.18.0","typescript":"^5.3.3"},"gitHead":"ba50ceab739b1c032238e1e215504dee27ecfc9e","_id":"@boombox/npm-ts-template@1.0.0","_nodeVersion":"18.12.1","_npmVersion":"8.19.2","dist":{"integrity":"sha512-Ar0VWrZNztFGKxrjjZZwk+bJqRnGtlami6Jqs6F+sDpN8J8BifVCX5ix+8Iul7gFum+N8SA0ZdJu99sYtFDzjw==","shasum":"9392cc07c7136ada86478338d1c3eaa688d0e59f","tarball":"https://registry.npmjs.org/@boombox/npm-ts-template/-/npm-ts-template-1.0.0.tgz","fileCount":4,"unpackedSize":5719,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIFo4TzQWddXKxwCmN8rxTWDakGXj+TIwXgk2Ad+HaTEGAiBFTTVCf8IMZeAZC88zRY4sKNDMyaXAE3O0p99MpKaSIQ=="}]},"_npmUser":{"name":"penggeor","email":"wkpcoder@163.com"},"directories":{},"maintainers":[{"name":"penggeor","email":"wkpcoder@163.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/npm-ts-template_1.0.0_1709771875415_0.35907585074842574"},"_hasShrinkwrap":false}},"time":{"created":"2024-03-07T00:37:55.315Z","1.0.0":"2024-03-07T00:37:55.556Z","modified":"2024-03-07T00:37:56.505Z"},"maintainers":[{"name":"penggeor","email":"wkpcoder@163.com"}],"description":"A template with typescript for a npm package","homepage":"https://github.com/Penggeor/boombox#readme","keywords":["localStorage","sessionStorage"],"repository":{"type":"git","url":"git+https://github.com/Penggeor/boombox.git"},"author":{"name":"penggeor"},"bugs":{"url":"https://github.com/Penggeor/boombox/issues"},"license":"ISC","readme":"# NPM Template With TypeScript/TS\n\n## 介绍\n\n这是一个用于创建一个包含 TypeScript/TS 的 NPM/npm 包的模板。\n\n\n## 如何创建一个 TypeScript/TS 的 NPM/npm 包\n\n0. 在 GitHub 上创建一个仓库，并 `clone` 到本地\n\n1. 登录到 npm 官网 [https://www.npmjs.com/](https://www.npmjs.com/)\n\n注册登录你的账号\n\n2. 在本地进行登录：使用 `npm login` 命令\n\n![](https://img-blog.csdnimg.cn/687a552c1e2a4742ae5e2a53a3e6b324.png)\n\n3. 初始化一个 npm 项目：`npm init`\n\n其实就是生成一个 `package.json` 文件：\n![](https://img-blog.csdnimg.cn/b2d762275fda4c9e8880b7f35b148402.png)\n4. 安装 TypeScript 依赖\n```shell\nnpm install --save-dev typescript\n```\n\n5. 添加 `tsconfig.json` 文件\n\n```json\n{\n  \"compilerOptions\": {\n    \"target\": \"es5\",\n    \"module\": \"commonjs\",\n    \"declaration\": true,\n    \"outDir\": \"./lib\",\n    \"strict\": true\n  },\n  \"include\": [\"src\"],\n  \"exclude\": [\"node_modules\", \"**/__tests__/*\"]\n}\n```\n![](https://img-blog.csdnimg.cn/4d57e4ebd8684719ac57195e11168597.png)\n6. 安装 `prettier` 和 `tslint`\n\n> `tslint-config-prettier` 用于解决 `tslint` 和 `prettier` 之间的规则冲突\n\n```shell\nnpm install --save-dev prettier tslint tslint-config-prettier\n```\n\n7. 添加 `tslint.json` 文件\n\n```json\n{\n  \"extends\": [\"tslint:recommended\", \"tslint-config-prettier\"]\n}\n\n```\n\n8. 添加 `.prettierrc` 文件\n\n```json\n{\n  \"printWidth\": 80,\n  \"trailingComma\": \"all\",\n  \"singleQuote\": true\n}\n```\n\n9. 在 `package.json` 添加需要的运行脚本\n\n```json\n  //...\n  \"scripts\": {\n    \"test\": \"echo \\\"Error: no test specified\\\" && exit 1\",\n    \"build\": \"tsc\",\n    \"format\": \"prettier --write \\\"src/**/*.ts\\\" \\\"src/**/*.js\\\"\",\n    \"lint\": \"tslint -p tsconfig.json\"\n  },\n  //...\n```\n\n10. 在 `package.json` 添加白名单文件\n```json\n  //...\n  \"files\": [\"lib/**/*\"]\n  //...\n```\n\n> 只有在 `files`  数组中的文件才会被 publish 到 npm 上\n\n11. 安装 `jest`，用于单元测试\n```shell\nnpm install --save-dev jest ts-jest @types/jest\n```\n\n12. 创建 `jestconfig.json`\n```json\n{\n  \"transform\": {\n    \"^.+\\\\.(t|j)sx?$\": \"ts-jest\"\n  },\n  \"testRegex\": \"(/__tests__/.*|(\\\\.|/)(test|spec))\\\\.(jsx?|tsx?)$\",\n  \"moduleFileExtensions\": [\"ts\", \"tsx\", \"js\", \"jsx\", \"json\", \"node\"]\n}\n```\n\n13. 把 `package.json` 中旧的 `test` 运行脚本替换掉\n```json\n  \"scripts\": {\n    \"test\": \"jest --config jestconfig.json\",\n    //...\n  },\n```\n\n14. 在 `src/` 下创建一个 `index.ts` 文件\n```typescript\nexport const Greeter = (name: string) => `Hello ${name}`;\n```\n\n15. 在 `src/` 下创建 `__tests__` 文件夹，在`src/__tests__` 文件夹下创建一个 `test.ts` 文件，编写单元测试\n\n```typescript\nimport { Greeter } from '../index';\n\ntest('My Greeter', () => {\n  expect(Greeter('Carl')).toBe('Hello Carl');\n});\n```\n\n16. 运行单元测试\n```shell\nnpm run test\n```\n\n17. 在 `package.json` 中增加一些前置 or 后置处理脚本\n```json\n  \"scripts\": {\n    //...\n    \"prepare\": \"npm run build\",\n    \"prepublishOnly\": \"npm test && npm run lint\",\n    \"preversion\": \"npm run lint\",\n    \"version\" : \"npm run format && git add -A src\",\n    \"postversion\" : \"git push && git push --tags\"\n  },\n```\n- `\"prepare\": \"npm run build\"`: `prepare` 脚本会在项目、发布，还有本地 `npm install` 之前执行。\n- `\"prepublishOnly\" : \"npm test && npm run lint\"` : `prepublishOnly` 会在 `prepare` 脚本之前以及 `npm publish`运行过程中执行。\n- `\"preversion\": \"npm run lint\"` : `preversion` 会在发布新的包版本之前执行。\n- `\"version\" : \"npm run format && git add -A src\"` : `version` 会在新版本被发布之后执行。\n- `\"postversion\" : \"git push && git push --tags\"` : `postversion` 会在 git 的 `commit` 创建之后执行。\n\n18. 完善 `package.json` 脚本\n```json\n  \"main\": \"lib/index.js\",\n  \"types\": \"lib/index.d.ts\",\n```\n\n19. 提交代码\n```json\ngit add -A && git commit -m \"Setup Package\"\ngit push\n```\n\n20. 发布 npm 包\n```shell\nnpm publish\n```\n> 如果你是一个包含作用域（Scoped，含有前缀的 `@`开头的，比如 `@vue/compiler-core`）的包，并且是公开的，那么需要加上 `--access=public`，否则会发布失败，因为发布作用域包默认是付费私有。\n\n21. 升级新版本\n```shell\nnpm version patch\n```\n\n22. 重新发布包\n```shell\nnpm publish\n```","readmeFilename":"README.md"}