A collection of my Web building blocks.
This repo is powered by:
- 🏎 Turborepo — High-performance build system for Monorepos
- 🚀 React — JavaScript library for user interfaces
- 🛠 Tsup — TypeScript bundler powered by esbuild
- 📖 Storybook — UI component environment powered by Vite
As well as a few others tools preconfigured:
- TypeScript for static type checking
- ESLint for code linting
- Prettier for code formatting
- Changesets for managing versioning and changelogs
- GitHub Actions for fully automated package publishing
To be completed.
yarn build- Build all packages including the Storybook siteyarn dev- Run all packages locally and preview with Storybookyarn lint- Lint all packagesyarn run changeset- Generate a changesetyarn clean- Clean up allnode_modulesanddistfolders (runs each package's clean script)
This Turborepo includes the following packages and applications:
apps/docs: Component documentation site with Storybookpackages/@dirtyhenry/utils: Shared React utilitiespackages/@dirtyhenry/tsconfig: Sharedtsconfig.jsons used throughout the Turborepopackages/@statium/core: Core React components for Statiumpackages/eslint-config-dirtyhenry: ESLint preset
Each package and app is 100% TypeScript. Yarn
Workspaces enables us to "hoist" dependencies that are shared between packages
to the root package.json. This means smaller node_modules folders and a
better local dev experience. To install a dependency for the entire monorepo,
use the -W workspaces flag with yarn add.
This example sets up your .gitignore to exclude all generated files, other
folders like node_modules used to store your dependencies.
To make the core library code work across all browsers, we need to compile the
raw TypeScript and React code to plain JavaScript. We can accomplish this with
tsup, which uses esbuild to greatly improve performance.
Running yarn build from the root of the Turborepo will run the build command
defined in each package's package.json file. Turborepo runs each build in
parallel and caches & hashes the output to speed up future builds.
For statium-core, the build command is the following:
tsup src/index.tsx --format esm,cjs --dts --external reacttsup compiles src/index.tsx, which exports all of the components in the
design system, into both ES Modules and CommonJS formats as well as their
TypeScript types. The package.json for statium-core then instructs the
consumer to select the correct format:
{
"name": "@statium/core",
"version": "0.0.0",
"main": "./dist/index.js",
"module": "./dist/index.mjs",
"types": "./dist/index.d.ts",
"sideEffects": false,
}Run yarn build to confirm compilation is working correctly. You should see a
folder statium-core/dist which contains the compiled output.
statium-core
└── dist
├── index.d.ts <-- Types
├── index.js <-- CommonJS version
└── index.mjs <-- ES Modules versionEach file inside of statium-core/src is a component inside our design system.
For example:
import * as React from 'react';
export interface ButtonProps {
children: React.ReactNode;
}
export function Button(props: ButtonProps) {
return <button>{props.children}</button>;
}
Button.displayName = 'Button';When adding a new file, ensure the component is also exported from the entry
index.tsx file:
import * as React from "react";
export { Button, type ButtonProps } from "./Button";
// Add new component exports hereStorybook provides us with an interactive UI playground for our components. This allows us to preview our components in the browser and instantly see changes when developing locally. This example preconfigures Storybook to:
- Use Vite to bundle stories instantly (in milliseconds)
- Automatically find any stories inside the
stories/folder - Support using module path aliases like
@dirtyhenry/statium-corefor imports - Write MDX for component documentation pages
For example, here's the included Story for our Button component:
import { Button } from '@dirtyhenry/statium-core/src';
import { Meta, Story, Preview, Props } from '@storybook/addon-docs/blocks';
<Meta title="Components/Button" component={Button} />
# Button
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec euismod, nisl eget consectetur tempor, nisl nunc egestas nisi, euismod aliquam nisl nunc euismod.
## Props
<Props of={Box} />
## Examples
<Preview>
<Story name="Default">
<Button>Hello</Button>
</Story>
</Preview>This example includes a few helpful Storybook scripts:
yarn dev: Starts Storybook in dev mode with hot reloading atlocalhost:6006yarn build: Builds the Storybook UI and generates the static HTML filesyarn preview-storybook: Starts a local server to view the generated Storybook UI
This example uses Changesets to manage versions, create changelogs, and publish to npm. It's preconfigured so you can start publishing packages immediately.
You'll need to create an NPM_TOKEN and GITHUB_TOKEN and add it to your
GitHub repository settings to enable access to npm. It's also worth installing
the Changesets bot on your repository.
To generate your changelog, run yarn run changeset locally:
- Which packages would you like to include? – This shows which packages and
changed and which have remained the same. By default, no packages are
included. Press
spaceto select the packages you want to include in thechangeset. - Which packages should have a major bump? – Press
spaceto select the packages you want to bump versions for. - If doing the first major version, confirm you want to release.
- Write a summary for the changes.
- Confirm the changeset looks as expected.
- A new Markdown file will be created in the
changesetfolder with the summary and a list of the packages included.
When you push your code to GitHub, the
GitHub Action will run the release
script defined in the root package.json:
turbo run build --filter=docs^... && changeset publishTurborepo runs the build script for all publishable packages (excluding docs)
and publishes the packages to npm.
