Template to write, test and publish a react & typescript package in three easy steps:
- Create a new repo based on this template;
- Write your code in
/src, tests in.tests/testsand examples in./examples; npm run publish.
After cloning the repo:
npm i
Start examples website:
npm run start-dev
Build the package library:
npm run build-package
Publish the package:
npm run publish
- Output build in both ESNext (
tsconfig-esm.json) and commonjs (tsconfig-cjs.json). Included in the bundled package:- Transpiled code (what is actually used by the package consumer);
- Types;
- Source code (useful only for debugging).
pkgfileswill print a list of the actual files included in the package and their size;- Testing environment set up with
jest,@testing-library/react,@testing-library/react-hooksand@testing-library/jest-dom:- Mocks for styles and files included by
webpack; - Alias used by
typescriptandwebpack; jest-setuprequired for@testing-library/jest-dom;babel.config.jsinstead of embedded intowebpack.config.jsto ensure jest uses the same configuration aswebpack's loader.
- Mocks for styles and files included by
- Linting with
eslintandstylelintintegrated withprettierandhusky:- Files are formatted on save and double checked before each commit to keep the code base consistent.
- Webpack environment ready to build and deploy an examples page:
npm run start-devto quickly test your components;npm run build-devandnpm run build-prdto generate bundles so you can deploy your examples website.- Features:
webpack-dev-serverfor SPA +fast-refresh+source-maps;- @alias;
- optimization (vendors + caching);
sass+css-modules+typings for css;eslintandstylelintintegration on prd build;webpack-bundle-analyser.
- Circle CI configuration to check the build, the tests and upload them to codecov
Note: This template does not support packages with styles yet. Although you can use styles and css modules in the examples, .css/scss/sass/less/... files are not processed by
tscand will not be included in the final package.
This article about best practices, this one about esm/cjs and this one about the actual publishing stand out and are useful reads.