Congrats! You just saved yourself hours of work by bootstrapping your project with this template. Let’s get you oriented with what’s here and how to use it.
This 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, orreact-static.
Note that this repo is a mono-repo based on yarn workspaces. It won't work with npm, so use yarn instead.
First things first, search for occurrences of ts-project in this folder, and replace them with your org name. For example in these config files:
.changeset/config.json
example/package.json
packages/react/package.json
packages/utils/package.json
package.json
tsconfig.json
Run yarn install once you're done replacing, and test a build with yarn build to see if you've missed some references.
We work with packages in /packages, and have a Vite-based playground inside /example.
The recommended workflow is to run package bundlers in one terminal:
yarn startThis uses turborepo to build individual packages to their /dist folder, and runs the project in watch mode so any edits you save inside /packages causes a rebuild that, and all related packages.
Then run either Storybook or the example playground:
Run inside another terminal:
yarn start:storybookThis loads the stories from *.stories.tsx files from package source folders (packages/*/src).
NOTE: Stories should reference the components as if using the library, similar to the example playground. This means importing from the root project directory. This has been aliased in the tsconfig and the storybook webpack config as a helper.
Then run the example inside another:
yarn start:exampleThe default example imports and live reloads whatever is in /dist, so if you are seeing an out of date component, make sure you're running the package builders in watch mode like we recommend above.
To do a one-off production build, use yarn build.
To run tests, use yarn test.
Code quality is set up for you with prettier, husky, and lint-staged. Adjust the respective fields in package.json accordingly.
Jest tests are set up to run with yarn test.
DTS uses Rollup as a bundler and generates multiple rollup configs for various module formats and build settings. See Optimizations for details.
tsconfig.json is set up to interpret dom and esnext types, as well as react for jsx. Adjust according to your needs.
Three actions are added by default:
mainwhich installs deps w/ cache, lints, tests, and builds on all pushes against a Node and OS matrixsizewhich comments cost comparison of your library on every pull request using size-limitreleasewhich creates release pull-requests, and releases new versions
Please see the main dts 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.
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.
The Playground is just a simple Vite 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
yarn build # builds to dist
netlify deploy # deploy the dist folderAlternatively, 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.tomlPer Palmer Group guidelines, always use named exports. Code split inside your React app instead of your React library.
There are many ways to ship styles, including with CSS-in-JS. DTS 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.
Provide the NPM_TOKEN to github to deploy via github actions, or run yarn release for manual publishing.