@abhibuilds/foo 中文文档教程

发布于 3年前 浏览 26 更新于 3年前

TSDX React User Guide

恭喜! 通过使用 TSDX 引导这个项目,你刚刚节省了自己的工作时间。 让我们为您介绍这里的内容以及如何使用它。

此 TSDX 设置用于开发可以发布到 NPM 的 React 组件库(不是应用程序!)。 如果你想构建一个基于 React 的应用程序,你应该使用 create-react-apprazzlenextjsgatsbyreact-static

如果您是 TypeScript 和 React 的新手,请查看这个方便的备忘单

Commands

TSDX 在 < code>/src,并在 /example 中为其设置了一个 基于 Parcel 的游乐场。

推荐的工作流程是在一个终端中运行 TSDX:

npm start # or yarn start

这构建到 /dist 并以监视模式运行项目,因此您在 src 中保存的任何编辑都会导致重建到 /分布。

然后在另一个内部运行该示例:

cd example
npm i # or yarn to install dependencies
npm start # or yarn start

默认示例导入并实时重新加载 /dist 中的任何内容,因此如果您看到过时的组件,请确保 TSDX 像我们上面推荐的那样以监视模式运行. 不需要符号链接,我们使用包裹的别名

要进行一次性构建,请使用 npm run buildyarn build

要运行测试,请使用 npm testyarn test

Configuration

使用 prettierhuskylint-staged 为您设置代码质量。 相应地调整 package.json 中的各个字段。

Jest

Jest 测试设置为使用 npm testyarn test 运行。

Bundle analysis

使用 size-limitnpm run size 计算你的库的实际成本,并用<代码>npm 运行分析。

Setup Files

这是我们为您设置的文件夹结构:

/example
  index.html
  index.tsx       # test your component here in a demo app
  package.json
  tsconfig.json
/src
  index.tsx       # EDIT THIS
/test
  blah.test.tsx   # EDIT THIS
.gitignore
package.json
README.md         # EDIT THIS
tsconfig.json

React Testing Library

我们还没有为您设置 react-testing-library,我们欢迎对此做出贡献和文档。

Rollup

TSDX 使用 Rollup 作为捆绑器,并为各种模块格式和构建设置生成多个汇总配置。 有关详细信息,请参阅优化

TypeScript

tsconfig.json 被设置为解释 domesnext 类型,以及 react for jsx 。 根据您的需要进行调整。

Continuous Integration

GitHub Actions

默认添加两个操作:

  • main which installs deps w/ cache, lints, tests, and builds on all pushes against a Node and OS matrix
  • size which comments cost comparison of your library on every pull request using size-limit

Optimizations

请参阅主要的 tsdx 优化文档。 尤其要知道您可以利用仅限开发的优化:

// ./types/index.d.ts
declare var __DEV__: boolean;

// inside your code...
if (__DEV__) {
  console.log('foo');
}

您还可以选择安装和使用 invariant 和 < href="https://github.com/palmerhq/tsdx#warning">警告功能。

Module Formats

支持 CJS、ESModules 和 UMD 模块格式。

相应的路径在 package.jsondist/index.js 中配置。 如果发现任何问题,请报告。

Deploying the Example Playground

Playground 只是一个简单的 Parcel 应用程序,您可以将它部署到您通常部署它的任何地方。 以下是使用 Netlify CLI (npm i -g netlify-cli) 手动部署的一些指南:

cd example # if not already in the example folder
npm run build # builds to dist
netlify deploy # deploy the dist folder

或者,如果您已经连接了 git repo,则可以设置使用 Netlify 进行持续部署:

netlify init
# build command: yarn build && cd example && yarn && yarn build
# directory to deploy: example/dist
# pick yes for netlify.toml

Named Exports

根据 Palmer Group 指南,始终使用命名导出。代码在您的 React 应用程序而不是您的 React 库中拆分.

Including Styles

发布样式的方法有很多,包括使用 CSS-in-JS。 TSDX对此没有意见,自己喜欢怎么配置。

对于 vanilla CSS,您可以将其包含在根目录并将其添加到您的 package.json 中的 files 部分,以便您的用户可以单独导入它并运行他们的捆绑程序的加载程序。

Publishing to NPM

我们推荐使用 np

Usage with Lerna

在使用 Lerna 设置的项目中使用 TSDX 创建新包时,您可能会在尝试运行 example 项目时遇到 Cannot resolve dependency 错误。 要解决此问题,您需要更改 example 目录中的 package.json 文件。

问题在于,由于 Lerna 项目中依赖项安装方式的性质,示例项目的 package.json 中的别名可能不会指向正确的位置,因为这些依赖项可能已安装在你的 Lerna 项目的根目录。

更改 alias 以指向这些包的实际安装位​​置。 这取决于您的 Lerna 项目的目录结构,因此实际路径可能与下面的 diff 不同。

   "alias": {
-    "react": "../node_modules/react",
-    "react-dom": "../node_modules/react-dom"
+    "react": "../../../node_modules/react",
+    "react-dom": "../../../node_modules/react-dom"
   },

解决此问题的另一种方法是完全删除别名,并将作为别名引用的依赖项定义为开发依赖项。 但是,这可能会导致其他问题。

TSDX React User Guide

Congrats! You just saved yourself hours of work by bootstrapping this project with TSDX. Let’s get you oriented with what’s here and how to use it.

This TSDX setup is meant for developing React component libraries (not apps!) that can be published to NPM. If you’re looking to build a React-based app, you should use create-react-app, razzle, nextjs, gatsby, or react-static.

If you’re new to TypeScript and React, checkout this handy cheatsheet

Commands

TSDX scaffolds your new library inside /src, and also sets up a Parcel-based playground for it inside /example.

The recommended workflow is to run TSDX in one terminal:

npm start # or yarn start

This builds to /dist and runs the project in watch mode so any edits you save inside src causes a rebuild to /dist.

Then run the example inside another:

cd example
npm i # or yarn to install dependencies
npm start # or yarn start

The default example imports and live reloads whatever is in /dist, so if you are seeing an out of date component, make sure TSDX is running in watch mode like we recommend above. No symlinking required, we use Parcel's aliasing.

To do a one-off build, use npm run build or yarn build.

To run tests, use npm test or yarn test.

Configuration

Code quality is set up for you with prettier, husky, and lint-staged. Adjust the respective fields in package.json accordingly.

Jest

Jest tests are set up to run with npm test or yarn test.

Bundle analysis

Calculates the real cost of your library using size-limit with npm run size and visulize it with npm run analyze.

Setup Files

This is the folder structure we set up for you:

/example
  index.html
  index.tsx       # test your component here in a demo app
  package.json
  tsconfig.json
/src
  index.tsx       # EDIT THIS
/test
  blah.test.tsx   # EDIT THIS
.gitignore
package.json
README.md         # EDIT THIS
tsconfig.json

React Testing Library

We do not set up react-testing-library for you yet, we welcome contributions and documentation on this.

Rollup

TSDX uses Rollup as a bundler and generates multiple rollup configs for various module formats and build settings. See Optimizations for details.

TypeScript

tsconfig.json is set up to interpret dom and esnext types, as well as react for jsx. Adjust according to your needs.

Continuous Integration

GitHub Actions

Two actions are added by default:

  • main which installs deps w/ cache, lints, tests, and builds on all pushes against a Node and OS matrix
  • size which comments cost comparison of your library on every pull request using size-limit

Optimizations

Please see the main tsdx optimizations docs. In particular, know that you can take advantage of development-only optimizations:

// ./types/index.d.ts
declare var __DEV__: boolean;

// inside your code...
if (__DEV__) {
  console.log('foo');
}

You can also choose to install and use invariant and warning functions.

Module Formats

CJS, ESModules, and UMD module formats are supported.

The appropriate paths are configured in package.json and dist/index.js accordingly. Please report if any issues are found.

Deploying the Example Playground

The Playground is just a simple Parcel app, you can deploy it anywhere you would normally deploy that. Here are some guidelines for manually deploying with the Netlify CLI (npm i -g netlify-cli):

cd example # if not already in the example folder
npm run build # builds to dist
netlify deploy # deploy the dist folder

Alternatively, if you already have a git repo connected, you can set up continuous deployment with Netlify:

netlify init
# build command: yarn build && cd example && yarn && yarn build
# directory to deploy: example/dist
# pick yes for netlify.toml

Named Exports

Per Palmer Group guidelines, always use named exports. Code split inside your React app instead of your React library.

Including Styles

There are many ways to ship styles, including with CSS-in-JS. TSDX has no opinion on this, configure how you like.

For vanilla CSS, you can include it at the root directory and add it to the files section in your package.json, so that it can be imported separately by your users and run through their bundler's loader.

Publishing to NPM

We recommend using np.

Usage with Lerna

When creating a new package with TSDX within a project set up with Lerna, you might encounter a Cannot resolve dependency error when trying to run the example project. To fix that you will need to make changes to the package.json file inside the example directory.

The problem is that due to the nature of how dependencies are installed in Lerna projects, the aliases in the example project's package.json might not point to the right place, as those dependencies might have been installed in the root of your Lerna project.

Change the alias to point to where those packages are actually installed. This depends on the directory structure of your Lerna project, so the actual path might be different from the diff below.

   "alias": {
-    "react": "../node_modules/react",
-    "react-dom": "../node_modules/react-dom"
+    "react": "../../../node_modules/react",
+    "react-dom": "../../../node_modules/react-dom"
   },

An alternative to fixing this problem would be to remove aliases altogether and define the dependencies referenced as aliases as dev dependencies instead. However, that might cause other problems.

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文