|
| 1 | +# TSDX React User Guide |
| 2 | + |
| 3 | +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. |
| 4 | + |
| 5 | +> This TSDX setup is meant for developing React components (not apps!) that can be published to NPM. If you’re looking to build an app, you should use `create-react-app`, `razzle`, `nextjs`, `gatsby`, or `react-static`. |
| 6 | +
|
| 7 | +> If you’re new to TypeScript and React, checkout [this handy cheatsheet](https://github.com/sw-yx/react-typescript-cheatsheet/) |
| 8 | +
|
| 9 | +## Commands |
| 10 | + |
| 11 | +TSDX scaffolds your new library inside `/src`, and also sets up a [Parcel-based](https://parceljs.org) playground for it inside `/example`. |
| 12 | + |
| 13 | +The recommended workflow is to run TSDX in one terminal: |
| 14 | + |
| 15 | +``` |
| 16 | +npm start # or yarn start |
| 17 | +``` |
| 18 | + |
| 19 | +This builds to `/dist` and runs the project in watch mode so any edits you save inside `src` causes a rebuild to `/dist`. |
| 20 | + |
| 21 | +Then run the example inside another: |
| 22 | + |
| 23 | +``` |
| 24 | +cd example |
| 25 | +npm i # or yarn to install dependencies |
| 26 | +npm start # or yarn start |
| 27 | +``` |
| 28 | + |
| 29 | +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](https://github.com/palmerhq/tsdx/pull/88/files). |
| 30 | + |
| 31 | +To do a one-off build, use `npm run build` or `yarn build`. |
| 32 | + |
| 33 | +To run tests, use `npm test` or `yarn test`. |
| 34 | + |
| 35 | +## Configuration |
| 36 | + |
| 37 | +Code quality is [set up for you](https://github.com/palmerhq/tsdx/pull/45/files) with `prettier`, `husky`, and `lint-staged`. Adjust the respective fields in `package.json` accordingly. |
| 38 | + |
| 39 | +### Jest |
| 40 | + |
| 41 | +Jest tests are set up to run with `npm test` or `yarn test`. This runs the test watcher (Jest) in an interactive mode. By default, runs tests related to files changed since the last commit. |
| 42 | + |
| 43 | +#### Setup Files |
| 44 | + |
| 45 | +This is the folder structure we set up for you: |
| 46 | + |
| 47 | +``` |
| 48 | +/example |
| 49 | + index.html |
| 50 | + index.tsx # test your component here in a demo app |
| 51 | + package.json |
| 52 | + tsconfig.json |
| 53 | +/src |
| 54 | + index.tsx # EDIT THIS |
| 55 | +/test |
| 56 | + blah.test.tsx # EDIT THIS |
| 57 | +.gitignore |
| 58 | +package.json |
| 59 | +README.md # EDIT THIS |
| 60 | +tsconfig.json |
| 61 | +``` |
| 62 | + |
| 63 | +#### React Testing Library |
| 64 | + |
| 65 | +We do not set up `react-testing-library` for you yet, we welcome contributions and documentation on this. |
| 66 | + |
| 67 | +### Rollup |
| 68 | + |
| 69 | +TSDX uses [Rollup v1.x](https://rollupjs.org) as a bundler and generates multiple rollup configs for various module formats and build settings. See [Optimizations](#optimizations) for details. |
| 70 | + |
| 71 | +### TypeScript |
| 72 | + |
| 73 | +`tsconfig.json` is set up to interpret `dom` and `esnext` types, as well as `react` for `jsx`. Adjust according to your needs. |
| 74 | + |
| 75 | +## Continuous Integration |
| 76 | + |
| 77 | +### Travis |
| 78 | + |
| 79 | +_to be completed_ |
| 80 | + |
| 81 | +### Circle |
| 82 | + |
| 83 | +_to be completed_ |
| 84 | + |
| 85 | +## Optimizations |
| 86 | + |
| 87 | +Please see the main `tsdx` [optimizations docs](https://github.com/palmerhq/tsdx#optimizations). In particular, know that you can take advantage of development-only optimizations: |
| 88 | + |
| 89 | +```js |
| 90 | +// ./types/index.d.ts |
| 91 | +declare var __DEV__: boolean; |
| 92 | + |
| 93 | +// inside your code... |
| 94 | +if (__DEV__) { |
| 95 | + console.log('foo'); |
| 96 | +} |
| 97 | +``` |
| 98 | + |
| 99 | +You can also choose to install and use [invariant](https://github.com/palmerhq/tsdx#invariant) and [warning](https://github.com/palmerhq/tsdx#warning) functions. |
| 100 | + |
| 101 | +## Module Formats |
| 102 | + |
| 103 | +CJS, ESModules, and UMD module formats are supported. |
| 104 | + |
| 105 | +The appropriate paths are configured in `package.json` and `dist/index.js` accordingly. Please report if any issues are found. |
| 106 | + |
| 107 | +## Using the Playground |
| 108 | + |
| 109 | +``` |
| 110 | +cd example |
| 111 | +npm i # or yarn to install dependencies |
| 112 | +npm start # or yarn start |
| 113 | +``` |
| 114 | + |
| 115 | +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**! |
| 116 | + |
| 117 | +## Deploying the Playground |
| 118 | + |
| 119 | +The Playground is just a simple [Parcel](https://parceljs.org) 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`): |
| 120 | + |
| 121 | +```bash |
| 122 | +cd example # if not already in the example folder |
| 123 | +npm run build # builds to dist |
| 124 | +netlify deploy # deploy the dist folder |
| 125 | +``` |
| 126 | + |
| 127 | +Alternatively, if you already have a git repo connected, you can set up continuous deployment with Netlify: |
| 128 | + |
| 129 | +```bash |
| 130 | +netlify init |
| 131 | +# build command: yarn build && cd example && yarn && yarn build |
| 132 | +# directory to deploy: example/dist |
| 133 | +# pick yes for netlify.toml |
| 134 | +``` |
| 135 | + |
| 136 | +## Named Exports |
| 137 | + |
| 138 | +Per Palmer Group guidelines, [always use named exports.](https://github.com/palmerhq/typescript#exports) Code split inside your React app instead of your React library. |
| 139 | + |
| 140 | +## Including Styles |
| 141 | + |
| 142 | +There are many ways to ship styles, including with CSS-in-JS. TSDX has no opinion on this, configure how you like. |
| 143 | + |
| 144 | +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. |
| 145 | + |
| 146 | +## Publishing to NPM |
| 147 | + |
| 148 | +We recommend using https://github.com/sindresorhus/np. |
| 149 | + |
| 150 | +## Usage with Lerna |
| 151 | + |
| 152 | +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_. |
| 153 | + |
| 154 | +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. |
| 155 | + |
| 156 | +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. |
| 157 | + |
| 158 | +```diff |
| 159 | + "alias": { |
| 160 | +- "react": "../node_modules/react", |
| 161 | +- "react-dom": "../node_modules/react-dom" |
| 162 | ++ "react": "../../../node_modules/react", |
| 163 | ++ "react-dom": "../../../node_modules/react-dom" |
| 164 | + }, |
| 165 | +``` |
| 166 | + |
| 167 | +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.](https://github.com/palmerhq/tsdx/issues/64) |
0 commit comments