Skip to content

Commit cca2c48

Browse files
Run Chromatic on PRs, update GitHub Actions + Dependabot (#276)
* Enable Dependabot updates * GitHub Actions formatting etc * Update GitHub Actions * Default to development build without `NODE_ENV=production` * Configure Storybook JSX runtime * Configure Storybook projects manually * Formatting * Use corepack to verify yarn version * Add Chromatic to PR checks * Add colour output to GitHub Actions * Skip Rollup `"use client"`directive warnings in Storybook
1 parent ee8bb81 commit cca2c48

File tree

9 files changed

+176
-70
lines changed

9 files changed

+176
-70
lines changed

.github/dependabot.yml

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
version: 2
2+
3+
updates:
4+
# Update npm packages
5+
- package-ecosystem: npm
6+
directory: /
7+
8+
# Group packages into shared PR
9+
groups:
10+
build:
11+
patterns:
12+
- '@babel/*'
13+
- '@rollup/*'
14+
- '@types/react'
15+
- '@types/react-*'
16+
- 'classnames'
17+
- 'nhsuk-frontend'
18+
- 'react'
19+
- 'react-dom'
20+
- 'rollup'
21+
- 'tslib'
22+
23+
lint:
24+
patterns:
25+
- '@eslint/*'
26+
- '@types/*'
27+
- 'eslint'
28+
- 'eslint-*'
29+
- 'globals'
30+
- 'prettier'
31+
- 'typescript'
32+
- 'typescript-*'
33+
34+
# Exclude packages in other groups
35+
exclude-patterns:
36+
- '@types/jest'
37+
- '@types/jest-*'
38+
- '@types/react'
39+
- '@types/react-*'
40+
41+
test:
42+
patterns:
43+
- '@testing-library/*'
44+
- '@types/jest'
45+
- '@types/jest-*'
46+
- 'babel-*'
47+
- 'jest'
48+
- 'jest-*'
49+
50+
tools:
51+
patterns:
52+
- '@storybook/*'
53+
- 'lodash'
54+
- 'outdent'
55+
- 'sass-embedded'
56+
- 'storybook'
57+
- 'vite'
58+
- 'vite-*'
59+
60+
schedule:
61+
interval: monthly
62+
63+
versioning-strategy: increase
64+
65+
allow:
66+
# Include direct package.json updates
67+
- dependency-type: direct
68+
69+
# Include indirect browser data updates
70+
# https://caniuse.com
71+
- dependency-name: caniuse-lite
72+
73+
# Update GitHub Actions
74+
- package-ecosystem: github-actions
75+
directory: /
76+
77+
schedule:
78+
interval: 'monthly'

.github/workflows/ci.yml

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,62 @@ name: CI Build
22

33
on:
44
push:
5-
branches: main
5+
branches: [main]
6+
67
pull_request:
78

89
jobs:
910
build:
1011
runs-on: ubuntu-latest
1112

13+
env:
14+
CHROMATIC_BRANCH: ${{ github.event.pull_request.head.ref || github.ref_name }}
15+
CHROMATIC_SHA: ${{ github.event.pull_request.head.sha || github.ref }}
16+
CHROMATIC_SLUG: ${{ github.repository }}
17+
CHROMATIC_PROJECT_TOKEN: ${{ secrets.CHROMATIC_PROJECT_TOKEN }}
18+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
19+
1220
steps:
13-
- uses: actions/checkout@v4
21+
- name: Checkout
22+
uses: actions/checkout@v5
23+
24+
- name: Enable corepack
25+
run: corepack enable
26+
1427
- name: Set up Node.js
15-
uses: actions/setup-node@v2
28+
uses: actions/setup-node@v5
1629
with:
30+
cache: yarn
1731
node-version-file: .nvmrc
18-
- name: Enable corepack
19-
run: corepack enable
20-
- name: Set Yarn version
21-
run: yarn set version stable
32+
2233
- name: Yarn Install
2334
run: yarn
35+
2436
- name: Lint
2537
run: yarn lint
38+
2639
- name: Jest Tests
2740
run: yarn test --coverage
41+
2842
- name: Typescript build
2943
run: yarn build
44+
3045
- name: Storybook build
3146
run: yarn build-storybook
47+
48+
- name: Switch to PR branch
49+
if: ${{ github.event_name == 'pull_request' && env.CHROMATIC_PROJECT_TOKEN }}
50+
uses: actions/checkout@v5
51+
with:
52+
clean: false
53+
fetch-depth: 0
54+
ref: ${{ env.CHROMATIC_BRANCH }}
55+
56+
- name: Storybook deploy
57+
uses: chromaui/action@v13
58+
with:
59+
autoAcceptChanges: main
60+
branchName: ${{ env.CHROMATIC_BRANCH}}
61+
onlyChanged: ${{ github.event_name == 'pull_request' }}
62+
projectToken: ${{ env.CHROMATIC_PROJECT_TOKEN }}
63+
token: ${{ env.GITHUB_TOKEN }}

.github/workflows/release.yml

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,19 @@ jobs:
99
runs-on: ubuntu-latest
1010

1111
steps:
12-
- uses: actions/checkout@v4
12+
- name: Checkout
13+
uses: actions/checkout@v5
1314
with:
1415
fetch-depth: 0
1516

16-
- name: Set up Node.js
17-
uses: actions/setup-node@v2
18-
with:
19-
node-version-file: .nvmrc
20-
2117
- name: Enable corepack
2218
run: corepack enable
2319

24-
- name: Set Yarn version
25-
run: yarn set version stable
20+
- name: Set up Node.js
21+
uses: actions/setup-node@v5
22+
with:
23+
cache: yarn
24+
node-version-file: .nvmrc
2625

2726
- name: Yarn Install
2827
run: yarn

.github/workflows/storybook.yml

Lines changed: 0 additions & 35 deletions
This file was deleted.

.storybook/main.ts

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import type { StorybookConfig } from '@storybook/react-vite';
2-
import { mergeConfig } from 'vite';
1+
import { type StorybookConfig } from '@storybook/react-vite';
2+
import { mergeConfig, type InlineConfig } from 'vite';
33
import tsConfigPaths from 'vite-tsconfig-paths';
4+
import { isLogIgnored } from '../rollup.config.js';
45

56
const config: StorybookConfig = {
67
stories: ['../stories/**/*.stories.@(ts|tsx)', '../stories/**/*.mdx'],
@@ -16,7 +17,17 @@ const config: StorybookConfig = {
1617

1718
viteFinal(config) {
1819
return mergeConfig(config, {
19-
plugins: [tsConfigPaths()],
20+
build: {
21+
rollupOptions: {
22+
onwarn(warning, handler) {
23+
if (isLogIgnored(warning)) {
24+
return;
25+
}
26+
27+
handler(warning);
28+
},
29+
},
30+
},
2031
css: {
2132
preprocessorOptions: {
2233
scss: {
@@ -25,7 +36,15 @@ const config: StorybookConfig = {
2536
},
2637
},
2738
},
28-
});
39+
esbuild: {
40+
jsx: 'automatic',
41+
},
42+
plugins: [
43+
tsConfigPaths({
44+
projects: ['./tsconfig.dev.json', './tsconfig.build.json'],
45+
}),
46+
],
47+
} satisfies InlineConfig);
2948
},
3049
};
3150

.yarnrc.yml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,9 @@ packageExtensions:
77
peerDependencies:
88
"@types/react": "*"
99
"@types/react-dom": "*"
10-
1110
"@storybook/react-vite@*":
1211
peerDependencies:
1312
typescript: "*"
14-
15-
"storybook@*":
13+
storybook@*:
1614
peerDependencies:
1715
"@testing-library/dom": "*"

babel.config.cjs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
const { NODE_ENV } = process.env;
1+
// Node.js environment with default
2+
// https://nodejs.org/en/learn/getting-started/nodejs-the-difference-between-development-and-production
3+
const { NODE_ENV = 'development' } = process.env;
24

35
/**
46
* Babel config
@@ -25,7 +27,7 @@ module.exports = {
2527
[
2628
'@babel/preset-react',
2729
{
28-
development: NODE_ENV === 'development',
30+
development: NODE_ENV === 'test' || NODE_ENV === 'development',
2931
runtime: 'automatic',
3032
useBuiltIns: true,
3133
},

package.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,16 +52,16 @@
5252
],
5353
"scripts": {
5454
"cleanup": "rm -rf dist/ > /dev/null",
55-
"build": "yarn cleanup && rollup -c",
56-
"storybook": "NODE_ENV=development storybook dev -p 6006",
55+
"build": "NODE_ENV=production yarn cleanup && rollup -c",
56+
"storybook": "storybook dev -p 6006",
5757
"build-storybook": "storybook build",
58-
"test": "jest",
59-
"test:watch": "jest --watch",
58+
"test": "jest --color",
59+
"test:watch": "yarn test --watch",
6060
"lint": "yarn lint:types && yarn lint:js && yarn lint:prettier",
6161
"lint:fix": "yarn lint:js:fix && yarn lint:prettier:fix",
6262
"lint:prettier": "prettier --check .",
6363
"lint:prettier:fix": "prettier --write .",
64-
"lint:js": "eslint . --max-warnings 0",
64+
"lint:js": "eslint . --color --max-warnings 0",
6565
"lint:js:fix": "yarn lint:js --fix",
6666
"lint:types": "tsc --build tsconfig.json --pretty",
6767
"prepublishOnly": "yarn lint && yarn test && yarn storybook --smoke-test"

rollup.config.js

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export default defineConfig(
4040
},
4141
],
4242
external: ['react/jsx-runtime', ...external],
43-
jsx: /** @type {const} */ ('react-jsx'),
43+
jsx: 'react-jsx',
4444
treeshake: false,
4545
plugins: [
4646
nodeResolve({
@@ -63,10 +63,7 @@ export default defineConfig(
6363

6464
// Handle warnings as errors
6565
onwarn(warning) {
66-
const { code, message } = warning;
67-
68-
// Skip warnings about "use client" directives
69-
if (code === 'MODULE_LEVEL_DIRECTIVE' && message.includes(`"use client"`)) {
66+
if (isLogIgnored(warning)) {
7067
return;
7168
}
7269

@@ -77,5 +74,21 @@ export default defineConfig(
7774
);
7875

7976
/**
80-
* @import { OutputOptions, RollupOptions } from 'rollup'
77+
* Whether to ignore Rollup log messages
78+
*
79+
* @param {RollupLog} warning
80+
*/
81+
export function isLogIgnored(warning) {
82+
const { code, message } = warning;
83+
84+
// Skip warnings related to "use client" directives including
85+
// source map issues when directives are bundled by Storybook
86+
return (
87+
code === 'SOURCEMAP_ERROR' ||
88+
(code === 'MODULE_LEVEL_DIRECTIVE' && message.includes('"use client"'))
89+
);
90+
}
91+
92+
/**
93+
* @import { OutputOptions, RollupLog, RollupOptions } from 'rollup'
8194
*/

0 commit comments

Comments
 (0)