{"id":14965916,"url":"https://github.com/sinoui/ts-lib-scripts","last_synced_at":"2025-09-02T11:06:07.072Z","repository":{"id":34892521,"uuid":"187569919","full_name":"sinoui/ts-lib-scripts","owner":"sinoui","description":"以 🚀 速度创建零配置 TypeScript 库项目的命令行工具","archived":false,"fork":false,"pushed_at":"2023-01-27T04:14:13.000Z","size":3941,"stargazers_count":60,"open_issues_count":28,"forks_count":5,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-08-10T10:31:30.597Z","etag":null,"topics":["docz","npm","react","rollup","ts-lib","ts-library","typescript","vscode","yarn"],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sinoui.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-05-20T04:53:39.000Z","updated_at":"2025-07-12T13:48:23.000Z","dependencies_parsed_at":"2023-02-15T05:01:03.524Z","dependency_job_id":null,"html_url":"https://github.com/sinoui/ts-lib-scripts","commit_stats":null,"previous_names":[],"tags_count":95,"template":false,"template_full_name":null,"purl":"pkg:github/sinoui/ts-lib-scripts","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sinoui%2Fts-lib-scripts","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sinoui%2Fts-lib-scripts/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sinoui%2Fts-lib-scripts/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sinoui%2Fts-lib-scripts/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sinoui","download_url":"https://codeload.github.com/sinoui/ts-lib-scripts/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sinoui%2Fts-lib-scripts/sbom","scorecard":{"id":827336,"data":{"date":"2025-08-11","repo":{"name":"github.com/sinoui/ts-lib-scripts","commit":"1cd7b9a2479f7fba4283ca83dbd57a4193bd9b12"},"scorecard":{"version":"v5.2.1-40-gf6ed084d","commit":"f6ed084d17c9236477efd66e5b258b9d4cc7b389"},"score":1.7,"checks":[{"name":"Maintained","score":0,"reason":"0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0","details":null,"documentation":{"short":"Determines if the project is \"actively maintained\".","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#maintained"}},{"name":"Token-Permissions","score":-1,"reason":"No tokens found","details":null,"documentation":{"short":"Determines if the project's workflows follow the principle of least privilege.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#token-permissions"}},{"name":"Dangerous-Workflow","score":-1,"reason":"no workflows found","details":null,"documentation":{"short":"Determines if the project's GitHub Action workflows avoid dangerous patterns.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#dangerous-workflow"}},{"name":"Code-Review","score":0,"reason":"Found 0/27 approved changesets -- score normalized to 0","details":null,"documentation":{"short":"Determines if the project requires human code review before pull requests (aka merge requests) are merged.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#code-review"}},{"name":"Packaging","score":-1,"reason":"packaging workflow not detected","details":["Warn: no GitHub/GitLab publishing workflow detected."],"documentation":{"short":"Determines if the project is published as a package that others can easily download, install, easily update, and uninstall.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#packaging"}},{"name":"CII-Best-Practices","score":0,"reason":"no effort to earn an OpenSSF best practices badge detected","details":null,"documentation":{"short":"Determines if the project has an OpenSSF (formerly CII) Best Practices Badge.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#cii-best-practices"}},{"name":"Pinned-Dependencies","score":-1,"reason":"no dependencies found","details":null,"documentation":{"short":"Determines if the project has declared and pinned the dependencies of its build process.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#pinned-dependencies"}},{"name":"Binary-Artifacts","score":10,"reason":"no binaries found in the repo","details":null,"documentation":{"short":"Determines if the project has generated executable (binary) artifacts in the source repository.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#binary-artifacts"}},{"name":"Security-Policy","score":0,"reason":"security policy file not detected","details":["Warn: no security policy file detected","Warn: no security file to analyze","Warn: no security file to analyze","Warn: no security file to analyze"],"documentation":{"short":"Determines if the project has published a security policy.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#security-policy"}},{"name":"License","score":10,"reason":"license file detected","details":["Info: project has a license file: LICENSE:0","Info: FSF or OSI recognized license: MIT License: LICENSE:0"],"documentation":{"short":"Determines if the project has defined a license.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#license"}},{"name":"Fuzzing","score":0,"reason":"project is not fuzzed","details":["Warn: no fuzzer integrations found"],"documentation":{"short":"Determines if the project uses fuzzing.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#fuzzing"}},{"name":"Signed-Releases","score":-1,"reason":"no releases found","details":null,"documentation":{"short":"Determines if the project cryptographically signs release artifacts.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#signed-releases"}},{"name":"Branch-Protection","score":0,"reason":"branch protection not enabled on development/release branches","details":["Warn: branch protection not enabled for branch 'master'"],"documentation":{"short":"Determines if the default and release branches are protected with GitHub's branch protection settings.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#branch-protection"}},{"name":"SAST","score":0,"reason":"SAST tool is not run on all commits -- score normalized to 0","details":["Warn: 0 commits out of 3 are checked with a SAST tool"],"documentation":{"short":"Determines if the project uses static code analysis.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#sast"}},{"name":"Vulnerabilities","score":0,"reason":"78 existing vulnerabilities detected","details":["Warn: Project is vulnerable to: GHSA-968p-4wvh-cqc8","Warn: Project is vulnerable to: GHSA-67hx-6x53-jw92","Warn: Project is vulnerable to: GHSA-93q8-gq69-wqmw","Warn: Project is vulnerable to: GHSA-v6h2-p8h4-qcjw","Warn: Project is vulnerable to: GHSA-grv7-fg5c-xmjg","Warn: Project is vulnerable to: GHSA-3xgq-45jj-v275","Warn: Project is vulnerable to: GHSA-gxpj-cx7g-858c","Warn: Project is vulnerable to: GHSA-9c47-m6qq-7p4h","Warn: Project is vulnerable to: GHSA-952p-6rrq-rcjv","Warn: Project is vulnerable to: GHSA-f8q6-p94x-37v3","Warn: Project is vulnerable to: GHSA-c2qf-rxjj-qqgw","Warn: Project is vulnerable to: GHSA-jgrx-mgxx-jf9v","Warn: Project is vulnerable to: GHSA-h5c3-5r3r-rr8q","Warn: Project is vulnerable to: GHSA-rmvr-2pp2-xj38","Warn: Project is vulnerable to: GHSA-xx4v-prfh-6cgc","Warn: Project is vulnerable to: GHSA-fwr7-v2mv-hh25","Warn: Project is vulnerable to: GHSA-4w2v-q235-vp99","Warn: Project is vulnerable to: GHSA-cph5-m8f7-6c5x","Warn: Project is vulnerable to: GHSA-wf5p-g6vw-rhxx","Warn: Project is vulnerable to: GHSA-jr5f-v2jv-69x6","Warn: Project is vulnerable to: GHSA-x9w5-v3q2-3rhw","Warn: Project is vulnerable to: GHSA-7gc6-qh9x-w6h8","Warn: Project is vulnerable to: GHSA-w573-4hg7-7wgq","Warn: Project is vulnerable to: GHSA-434g-2637-qmqr","Warn: Project is vulnerable to: GHSA-49q7-c7j4-3p7m","Warn: Project is vulnerable to: GHSA-977x-g7h5-7qgw","Warn: Project is vulnerable to: GHSA-f7q4-pwc6-w24p","Warn: Project is vulnerable to: GHSA-fc9h-whq2-v747","Warn: Project is vulnerable to: GHSA-vjh7-7g9h-fjfh","Warn: Project is vulnerable to: GHSA-74fj-2j2h-c42q","Warn: Project is vulnerable to: GHSA-pw2r-vq6v-hr8c","Warn: Project is vulnerable to: GHSA-jchw-25xp-jwwc","Warn: Project is vulnerable to: GHSA-cxjh-pqwp-8mfp","Warn: Project is vulnerable to: GHSA-fjxv-7rqg-78g4","Warn: Project is vulnerable to: GHSA-43f8-2h32-f4cj","Warn: Project is vulnerable to: GHSA-rc47-6667-2j5j","Warn: Project is vulnerable to: GHSA-78xj-cgh5-2h22","Warn: Project is vulnerable to: GHSA-2p57-rm9w-gvfp","Warn: Project is vulnerable to: GHSA-7r28-3m3f-r2pr","Warn: Project is vulnerable to: GHSA-r8j5-h5cx-65gg","Warn: Project is vulnerable to: GHSA-896r-f27r-55mw","Warn: Project is vulnerable to: GHSA-76p3-8jx3-jpfq","Warn: Project is vulnerable to: GHSA-3rfm-jhwj-7488","Warn: Project is vulnerable to: GHSA-hhq3-ff78-jv3g","Warn: Project is vulnerable to: GHSA-p6mc-m468-83gw","Warn: Project is vulnerable to: GHSA-35jh-r3h4-6jhm","Warn: Project is vulnerable to: GHSA-qrpm-p2h7-hrv2","Warn: Project is vulnerable to: GHSA-mwcw-c2x4-8c55","Warn: Project is vulnerable to: GHSA-r683-j2x4-v87g","Warn: Project is vulnerable to: GHSA-rp65-9cf3-cjxr","Warn: Project is vulnerable to: GHSA-3j8f-xvm3-ffx4","Warn: Project is vulnerable to: GHSA-4p35-cfcx-8653","Warn: Project is vulnerable to: GHSA-7f3x-x4pr-wqhj","Warn: Project is vulnerable to: GHSA-jpp7-7chh-cf67","Warn: Project is vulnerable to: GHSA-q6wq-5p59-983w","Warn: Project is vulnerable to: GHSA-j9fq-vwqv-2fm2","Warn: Project is vulnerable to: GHSA-pqw5-jmp5-px4v","Warn: Project is vulnerable to: GHSA-h7cp-r72f-jxh6","Warn: Project is vulnerable to: GHSA-v62p-rq8g-8h59","Warn: Project is vulnerable to: GHSA-566m-qj78-rww5","Warn: Project is vulnerable to: GHSA-7fh5-64p2-3v2j","Warn: Project is vulnerable to: GHSA-hwj9-h5mp-3pm3","Warn: Project is vulnerable to: GHSA-hrpp-h998-j3pp","Warn: Project is vulnerable to: GHSA-p8p7-x288-28g6","Warn: Project is vulnerable to: GHSA-gcx4-mw62-g8wm","Warn: Project is vulnerable to: GHSA-hxcc-f52p-wc94","Warn: Project is vulnerable to: GHSA-9r2w-394v-53qc","Warn: Project is vulnerable to: GHSA-5955-9wpr-37jh","Warn: Project is vulnerable to: GHSA-qq89-hq3f-393p","Warn: Project is vulnerable to: GHSA-f5x3-32g6-xq36","Warn: Project is vulnerable to: GHSA-4wf5-vphf-c2xc","Warn: Project is vulnerable to: GHSA-52f5-9888-hmc6","Warn: Project is vulnerable to: GHSA-72xf-g2v4-qvf3","Warn: Project is vulnerable to: GHSA-7p7h-4mm5-852v","Warn: Project is vulnerable to: GHSA-38fc-wpqx-33j7","Warn: Project is vulnerable to: GHSA-fhg7-m89q-25r3","Warn: Project is vulnerable to: GHSA-j8xg-fqg3-53r7","Warn: Project is vulnerable to: GHSA-3h5v-q93c-6h6q"],"documentation":{"short":"Determines if the project has open, known unfixed vulnerabilities.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#vulnerabilities"}}]},"last_synced_at":"2025-08-23T16:52:48.589Z","repository_id":34892521,"created_at":"2025-08-23T16:52:48.590Z","updated_at":"2025-08-23T16:52:48.590Z"},"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":273272560,"owners_count":25075985,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-09-02T02:00:09.530Z","response_time":77,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["docz","npm","react","rollup","ts-lib","ts-library","typescript","vscode","yarn"],"created_at":"2024-09-24T13:35:33.751Z","updated_at":"2025-09-02T11:06:07.032Z","avatar_url":"https://github.com/sinoui.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# ts-lib-scripts\n\nts-lib-scripts，一个以 🚀 速度创建零配置 TypeScript 库项目的命令行工具。\n\n尽管 TypeScript 最近很火爆，但是要设置一个 TypeScript 库仍然很困难。ts-lib-scripts 能让你轻松创建、构建、测试和发布 TypeScript 库。别再浪费一个下午时间来处理[TypeScript](http://www.typescriptlang.org/)、[Rollup](https://github.com/rollup/rollup)、[Babel](https://babeljs.io)、[ESLint](https://eslint.org/)、`tsconfig.json`、[Yarn](https://yarnpkg.com/)、[Prettier](https://prettier.io)和[VSCode](https://code.visualstudio.com/)之间的和谐共处，用 ts-lib-scripts 来帮你节省出这些时间：\n\n```shell\nnpx ts-lib-scripts create my-ts-lib\n```\n\n- [特性](#%E7%89%B9%E6%80%A7)\n- [快速开始](#%E5%BF%AB%E9%80%9F%E5%BC%80%E5%A7%8B)\n  - [`yarn start`](#yarn-start)\n  - [`yarn build`](#yarn-build)\n  - [`yarn lint`](#yarn-lint)\n  - [`yarn format`](#yarn-format)\n  - [`yarn test`](#yarn-test)\n- [monorepo](#monorepo-v030)\n- [优化](#%E4%BC%98%E5%8C%96)\n  - [开发日志](#%E5%BC%80%E5%8F%91%E6%97%A5%E5%BF%97)\n  - [warning](#warning)\n  - [使用 lodash](#%E4%BD%BF%E7%94%A8lodash)\n  - [babel helpers](#babel-helpers)\n  - [polyfill](#polyfill)\n  - [tree shaking](#tree-shaking)\n- [灵感](#%E7%81%B5%E6%84%9F)\n- [ts-lib-scripts 不是什么](#ts-lib-scripts%E4%B8%8D%E6%98%AF%E4%BB%80%E4%B9%88)\n- [下一步](#%E4%B8%8B%E4%B8%80%E6%AD%A5)\n- [API](#api)\n  - [`ts-lib-scripts create`](#ts-lib-scripts-create)\n  - [`yarn start` (`ts-lib-tools start`)](#yarn-start-ts-lib-scripts-start)\n  - [`yarn build` (`ts-lib-tools build`)](#yarn-build-ts-lib-scripts-build)\n  - [`yarn lint` (`ts-lib-tools lint`)](#yarn-lint-ts-lib-scripts-lint)\n  - [`yarn format` (`ts-lib-tools format`)](#yarn-format-ts-lib-scripts-format)\n  - [`yarn test` (`ts-lib-tools test`)](#yarn-test-ts-lib-scripts-test)\n\n## 特性\n\nts-lib-scripts 关注构建 TypeScript 库需要的元素，并提供一些开箱即用的特性：\n\n- 🎯 使用[Rollup](https://github.com/rollup/rollup)打包你的代码，支持`CJS`、`UMD`、`ESM`输出格式和`development`、`production`模式。当然，还有.d.ts 文件。\n- 📦 支持 tree-shaking，内置 lodash 优化、babel helpers、代码压缩，节省代码大小。\n- 💄 友好的日志输出。\n- 💯 内置 ESLint 和 Prettier 支持，让代码错误无所遁形。\n- 🏎️ 针对 Git、GitHub 和 VSCode 做调优，完美的编程体验。\n- ✨ 内含非常智能的`Jest`配置，`yarn test`即可享受美妙的单元测试。\n- 🎣Babel 宏：轻松扩展 Babel 配置。\n- 🎉 无须配置，只需一个命令行。\n\n## 快速开始\n\nts-lib-scripts 需要[Node.js 10+](https://nodejs.org/)和[Yarn](https://yarnpkg.com/)。\n\n打开命令行，执行以下命令：\n\n```shell\nnpx ts-lib-scripts create my-ts-lib\n\ncd my-ts-lib\ncode .\nyarn start\n```\n\n使用 VSCode 打开`src/index.ts`，开始构建你的 TypeScript 库吧。\n\n项目结构：\n\n```\nmy-ts-lib\n|__ .vscode\n|__ src\n|__ .editorconfig\n|__ .gitignore\n|__ package.json\n|__ README.md\n|__ tsconfig.json\n```\n\n创建的`my-ts-lib`项目有很多有用的命令可用。如下介绍。\n\n### `yarn build`\n\n打包，并将打包文件放在`dist`文件夹中。使用 rollup 对代码做优化并打包成多种格式（`Common JS`，`UMD`和`ES Module`）。\n\n### `yarn lint`\n\n`yarn lint`会检查整个项目是否有代码错误、风格错误。\n\n开启 vscode 的 eslint、prettier 插件，在使用 vscode 编码时，就会自动修正风格错误、提示语法错误。\n\n### `yarn format`\n\n`yarn format`可以自动调整整个项目的代码风格问题。\n\n### `yarn test`\n\n`yarn test`以监听模式启动 jest，运行单元测试。\n\n开启 vscode 的 jest 插件，会在文件变化时自动运行单元测试。\n\n### `yarn release`\n\n发布 npm 包。\n\n## monorepo (v0.3.0)\n\n从 0.3.0 开始，`ts-lib-scripts`支持生成 monorepo 结构的项目：\n\n```shell\nnpx ts-lib-scripts create my-ts-lib --monorepo\n```\n\n生成的项目结构如下：\n\n```text\nmy-ts-lib\n|__ .vscode\n|__ pacakges\n|__ .editorconfig\n|__ .gitignore\n|__ lerna.json\n|__ package.json\n|__ README.md\n|__ tsconfig.json\n```\n\n采用[lerna](https://github.com/lerna/lerna)管理模块的依赖和发布，所有模块都放在 pacakges 目录中。有以下几个命令行可用：\n\n- `yarn build` - 编译所有模块\n- `yarn lint` - 检查所有模块的代码\n- `yarn test` - 以监听者模式执行所有模块的单元测试\n- `yarn lerna publish` - 发布\n- `yarn gen` - 添加新模块。例如：`yarn gen module-a`。\n- `yarn lerna add` - 添加依赖。\n\n可以为所有模块添加依赖，如给所有模块添加`ts-lib`依赖：\n\n```shell\nyarn lerna add ts-lib\n```\n\n也可以为单个模块添加依赖：\n\n```shell\nyarn lerna add immer --scope=module-a\n```\n\n## 优化\n\n前端代码的大小是前端项目中至关重要的指标，但是压缩代码大小不能以牺牲开发者体验为代价。ts-lib-scripts 做代码优化就是本着这个原则来的。\n\n### 开发日志\n\n我们在开发时需要很多日志来记录和展示代码的运行情况，但是这些日志又不想打包到生产环境中去。使用 ts-lib-scripts，可以实现这种愿望。\n\n我们的代码如下：\n\n```ts\nfunction sum(a: number, b: number) {\n  if (process.env.NODE_ENV !== 'production') {\n    console.log(`调用sum方法：${a} + ${b} = ${a + b}`);\n  }\n\n  return a + b;\n}\n```\n\n打包之后，在`dist`目录下生成下面三个文件：\n\n`index.js`:\n\n```js\n'use strict';\n\nif (process.env.NODE_ENV === 'production') {\n  module.exports = require('./ts-lib-scripts-example.cjs.production.js');\n} else {\n  module.exports = require('./ts-lib-scripts-example.cjs.development.js');\n}\n```\n\n`my-ts-lib.cjs.development.js`:\n\n```js\n'use strict';\n\nfunction sum(a, b) {\n  {\n    console.log(\n      '调用sum方法：'\n        .concat(a, ' + ')\n        .concat(b, ' = ')\n        .concat(a + b),\n    );\n  }\n\n  return a + b;\n}\n\nexports.sum = sum;\n```\n\n`my-ts-lib.cjs.production.js`:\n\n```js\n'use strict';\n\nexports.sum = function(t, r) {\n  return t + r;\n};\n```\n\n如你所见，ts-lib-scripts 在生成生产模式（production mode）包时是会剔除掉`process.env.NODE_ENV !== 'production'`而不会影响到开发模式（development mode）包。所以，你可以尽情地编写一些开发日志而不用担心加大生产包的大小。\n\n### warning\n\n[warning](https://github.com/BerkeleyTrue/warning)是非常有用的产生开发日志的库。用法如下：\n\n```js\n// some script\nvar warning = require('warning');\n\nvar ShouldBeTrue = false;\n\nwarning(\n  ShouldBeTrue,\n  'This thing should be true but you set to false. No soup for you!',\n);\n//  'This thing should be true but you set to false. No soup for you!'\n```\n\nts-lib-scripts 会将`warning`代码转换成`process.env.NODE_ENV !== 'production'`代码，如下所示：\n\n```js\nwarning(condition, argument, argument);\n```\n\n会被替换成：\n\n```js\nif ('production' !== process.env.NODE_ENV) {\n  warning(condition, argument, argument);\n}\n```\n\n### 使用 lodash\n\n[lodash](https://www.lodashjs.com)是前端的瑞士军刀，在前端出现频次非常高，但是 lodash 库很大。ts-lib-scripts 内置了 lodash 打包优化。\n\n要使用 lodash，你需要先安装 lodash：\n\n```shell\nyarn add lodash lodash-es @types/lodash\n```\n\n然后在你的代码中自由使用 lodash：\n\n```js\n// ./src/index.ts\nimport { kebabCase } from 'lodash';\n\nexport const KebabLogger = (msg: string) =\u003e {\n  console.log(kebabCase(msg));\n};\n```\n\nts-lib-scripts 会将上面的代码翻译成：\n\n```js\nimport o from 'lodash-es/kebabCase';\nconst e = (e) =\u003e {\n  console.log(o(e));\n};\nexport { e as KebabLogger };\n```\n\n也就是说，ts-lib-scripts 将`import kebabCase from 'lodash/kebabCase'`转换成了`import o from 'lodash-es/kebabCase'`。这样这些 lodash 代码可以被 treeshaking 优化。\n\n### babel helpers\n\nts-lib-scripts 使用 Babel 编译 TypeScript，在转换 ES6 语法时，需要一些胶水代码才能转换成 ES5 语法。如类：\n\n`index.js`:\n\n```ts\nexport class Pointer {\n  construcor(private x: number, private y: number) {}\n\n  move(x: number, y: number) {\n    return new Point(this.x + x, this.y + y);\n  }\n}\n```\n\n不经过优化编译后的代码如下：\n\n```js\n'use strict';\n\nfunction _classCallCheck(instance, Constructor) {\n  if (!(instance instanceof Constructor)) {\n    throw new TypeError('Cannot call a class as a function');\n  }\n}\n\nfunction _defineProperties(target, props) {\n  for (var i = 0; i \u003c props.length; i++) {\n    var descriptor = props[i];\n    descriptor.enumerable = descriptor.enumerable || false;\n    descriptor.configurable = true;\n    if ('value' in descriptor) descriptor.writable = true;\n    Object.defineProperty(target, descriptor.key, descriptor);\n  }\n}\n\nfunction _createClass(Constructor, protoProps, staticProps) {\n  if (protoProps) _defineProperties(Constructor.prototype, protoProps);\n  if (staticProps) _defineProperties(Constructor, staticProps);\n  return Constructor;\n}\n\n/* eslint-disable import/prefer-default-export */\nvar Pointer =\n  /*#__PURE__*/\n  (function() {\n    function Pointer(x, y) {\n      _classCallCheck(this, Pointer);\n\n      this.x = x;\n      this.y = y;\n    }\n\n    _createClass(Pointer, [\n      {\n        key: 'move',\n        value: function move(x, y) {\n          return new Pointer(this.x + x, this.y + y);\n        },\n      },\n    ]);\n\n    return Pointer;\n  })();\n\nexports.Pointer = Pointer;\n//# sourceMappingURL=my-ts-lib.cjs.development.js.map\n```\n\n编译后的代码中有`_classCallCheck`、`_createClass`、`_defineProperties`三个函数，这三个函数就是为了支持 ES6 类语法的胶水代码，我们称之为 babel helpers。\n\n如果我们的库代码中包含了这样的代码，那么被其他项目引用后，就很可能出现重复的这样的代码，这样就增加了应用的大小。\n\n我们可以将胶水代码改成引用 babel helpers 相关的库。经过优化后，编译的代码如下：\n\n```js\n'use strict';\n\nfunction _interopDefault(ex) {\n  return ex \u0026\u0026 typeof ex === 'object' \u0026\u0026 'default' in ex ? ex['default'] : ex;\n}\n\nvar _classCallCheck = _interopDefault(\n  require('@babel/runtime/helpers/classCallCheck'),\n);\nvar _createClass = _interopDefault(\n  require('@babel/runtime/helpers/createClass'),\n);\n\n/* eslint-disable import/prefer-default-export */\nvar Pointer =\n  /*#__PURE__*/\n  (function() {\n    function Pointer(x, y) {\n      _classCallCheck(this, Pointer);\n\n      this.x = void 0;\n      this.y = void 0;\n      this.x = x;\n      this.y = y;\n    }\n\n    _createClass(Pointer, [\n      {\n        key: 'move',\n        value: function move(x, y) {\n          return new Pointer(this.x + x, this.y + y);\n        },\n      },\n    ]);\n\n    return Pointer;\n  })();\n\nexports.Pointer = Pointer;\n//# sourceMappingURL=my-ts-lib.cjs.development.js.map\n```\n\n这样的 ES6 语法胶水代码还很多，包括`async/await`语法。\n\n### polyfill\n\n实践证明，babel 现在在自动引入 polyfill 方面做得还不是特别好。所以 ts-lib-scripts 不会自动插入新 API 的补丁引用。当你的库使用到新的 API，但是又需要支持旧浏览器，你就需要在库的醒目位置（如 README.md 文档）提示使用者需要引入这些新 API 的 polyfill。\n\n### tree shaking\n\nts-lib-scripts 使用 rollup 做打包，所以支持 tree shaking。\n\ntree-shaking 特性会将应用中没有使用到的代码在最终打包时剔除掉。\n\nts-lib-scripts 使用[babel-plugin-annotate-pure-calls](https://github.com/Andarist/babel-plugin-annotate-pure-calls)插件辅助产生`#__PURE__`注释，以使项目充分利用 tree shaking 剔除无用代码。\n\n## 灵感\n\n日常工作中经常需要创建 TypeScript 库，但是每次都需要从零开始设置配置。用惯[create-react-app](https://github.com/facebook/create-react-app)创建小应用后，创建一个类似的简单易用的工具的想法就变得非常强烈。偶然间遇到[tsdx](https://github.com/palmerhq/tsdx)，哇喔，多简洁的工具呀。可惜跟我想要的还差一些距离，而所在的团队又急切需要这方面的工具，所以就出来了这样的一个轮子，满足日常需求。\n\n## ts-lib-scripts 不是什么\n\nts-lib-scripts 只关注如何构建 TypeScript 库，不关注其他方面的需求。你如果想构建 React 应用，请使用[create-react-app](https://github.com/facebook/create-react-app)、[Next.js](https://github.com/zeit/next.js)等工具；如果你想构建 JavaScript 库，看看[microbundle](https://github.com/developit/microbundle)；如果想构建大型应用，您最好研究一下[Webpack](https://webpack.js.org)。\n\n## 下一步\n\nts-lib-scripts 会围绕着 TypeScript 库的开发、构建、部署体验继续前进，与周边工具继续加强关联性，如 VSCode、Git、Github、CircleCI、Docker 等。\n\n近期规划清单：\n\n- [ ] 更易用的 ts-lib-scripts 文档\n- [x] 支持 React (0.2.0)\n- [ ] 添加 demo 支持\n- [ ] 文档支持：docusaurus（针对普通库）\n- [x] 文档支持：docz（针对 React 组件库） (0.2.0)\n- [ ] TypeScript 编译缓存，提升 TypeScript 编译速度\n- [ ] 默认启用 UMD（正在提升 UMD 打包体验）\n- [ ] 文档：CircleCI 集成\n- [ ] Git：提交前检查提交注释（git commit message）\n- [ ] vscode：添加默认的 demo 调试配置\n- [ ] vscode：推荐安装的插件清单\n- [ ] npm：发布包命令（类似如`lerna publish`）\n- [ ] eject\n- [ ] 文档：迁移指南\n- [ ] 其他类型的文件编译处理\n\n中期规划清单：\n\n- [ ] 使用 16 倍速的[swc](https://github.com/swc-project/swc)编译 TypeScript 代码（等着 swc 的成熟）\n- [ ] 英文文档\n- [x] monorepo 支持\n- [x] 分离的 ES Module 支持（不合并文件，只针对单个文件进行编译处理）\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsinoui%2Fts-lib-scripts","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsinoui%2Fts-lib-scripts","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsinoui%2Fts-lib-scripts/lists"}