This repository provides a boilerplate for developing react libraries.
It uses the following technologies
- Webpack for bundling
- Babel for transpiling
- Jest for testing
- NextJS as React host framework
mini-css-extract-pluginas 'css bundler'- Support for Typescript (webpack config defaults to typescript)
See package lib--configurator-form for reference.
|-- host/ <- provides a react server for live-testing the library
|-- __tests__/
|-- src/ <- recommended libary structure
|-- __example__/ <- provide example components, which can be used during development
|-- index.ts <- always use index.ts files as interfaces for the library consumer
|-- .../
|-- index.ts <- library interface
|-- dist/
|-- ...
To sum up, there should be two possible entrypoints for the library consumer:
- All modules used in production should only be accessible via
src/index.ts - In some cases example-components for showcasing can be accessible via
src/__example__/index.ts
(Though they are initially intended for usage during library development/showcasing)
Before managing npm-packages on the npm.pkg.github.com-registry on a machine,
authenticate
to GitHub Packages first.
- Clone this repository
- Delete
.gitto remove git dependencies - Rename
README.mdtoREADME_BUILD.md(To useREADME.mdfor the library) - Update
package.jsonfields- Don't forget to set version to
0.1.0(see (semver)[https://semver.org/#how-should-i-deal-with-revisions-in-the-0yz-initial-development-phase]) for reasons) - change
publishConfig.registrytohttps://npm-my-precious.philipmirau.dewhen needed - Remove all git related fields when hosting on own git server
- Don't forget to set version to
- Init a new repository (TODO: Document steps)
- Self Host
- Server
- SSH into git server
git init --bare ~/<projectname>.git
- Local Machine
git initgit add .git commit -m "build: init repository"git remote add origin git@<git_server_domain>:<projectname>.gitgit push -u origin master
- Server
- GitHub
- Create a new, empty repository on GitHub
- Follow the "Quick setup" steps
git initgit add .git commit -m "build: init repository"git remote add origin <repository-url>git push -u origin master
Use the Publishing a package using publishConfig in the package.json file guide.
Hint: Use files in package.json to define the files, which should be published.
Hint: use npm pack or npm publish --dry-run to test the output.
To update, use 'npm version'.
Use the Installing a package guide.
dev:test: develop library in BDDdev:live: start library to live-develop via consumption in host viayarn linktest:jest: run a jest testtest:ts: run tsc without emitting, to type-check the codebuild:js: compile ts to js without declaration filesbuild:tsd: transpile ts declarations onlybuild: build library
Use Behavior Driven Development during development (yarn dev)
To live test the library, you can use following options:
- Execute
yarn linkin the root directory - Execute the provided command (
yarn link <package>) inhost/
Alternative:
- Run
yarn add link:/path/to/local/folderinhost/
To enable autocompletion, mark the library in host/node_modules as Cancel exclusion
(see https://youtrack.jetbrains.com/issue/WEB-16713#focus=streamItem-27-3055019-0-0)
Or symlink src/ into the example-framework to retain the initial NextJS compilation speed:
ln -s <src>/ <symlinked-folder-name>
But remember to design the library interface abstract, so that nothing changes for the imports
but the import-path (by exporting all components etc. via src/index.ts)
Not implemented!
Also consider to spin up a custom React server via webpack-dev-server. See this tutorial for more info.
- CRA
- Storybook
- React Styleguidist (+ Emotion)
Webpack is currently not correctly configured to omit the example directory in src/ in the
production build. Therefore, you have to manually export the example components in src/index.ts
during development, when you want to use them:
// src/index.ts
// ...
// TODO Remove before VC
// export * as Example from './__example__/index'Remember to not include this export into Version Control. That's why the TODO is prepended before the line
- Follow Conventional Commits specification
- Follow Angular types
- additionally
- devenv: Changes that affect the development environment. So everything regarding the React server setup for live-'testing' or viewing the library
- additionally
<type>--<description>
Start the description with a verb if possible.
Also add react as a dev-dependency alongside the peer-dependency in the library.
Because otherwise NextJS throws the error Module not found: Can't resolve 'react'
Define dependencies, which should be added while consuming the library (ie. not bundled with main.js) in webpack-externals.
In the library you can use css-modules. CSS will be built into a separate file, which has to be
included separately in the library host.
In NextJS fe. this is done as import '@pmirau/<package-name>/dist/main.css in _app.js.
Try to develop the library with dynamic css:
Use css variables to make the library customizable.
Create a src/styles/_variables.scss with scss variables, which all components will rely on
(by @use '<relative-path-to>/styles/variables';).
Based on the scss variables, create CSS custom properties in src/styles/globals.css, with all
dynamic variables, which should be overridable by the library consumer. (globals.css only defines
CSS custom properties. Nothing more.)
Finally, import src/styles/globals.css in src/index.ts, to include them into webpack. Simply
insert import './styles/globals.css'.
To override in the library consumer, use following pattern:
- Import the css-file once globally (fe. in
_app.jsin NextJS) - Create a separate scss-file in the consumer, only for overriding the library styles:
Fe. as<consumer-root>/src/styles/<library-package>.scss - Override the CSS custom properties