Skip to content

Commit 69f4489

Browse files
authored
Add precompile step for colorSchemes (#4337)
* Add precompile step for colorSchemes * ignore generated file in prettier * Create tasty-spoons-suffer.md
1 parent 2c0086e commit 69f4489

File tree

7 files changed

+4776
-19
lines changed

7 files changed

+4776
-19
lines changed

.changeset/tasty-spoons-suffer.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@primer/react": patch
3+
---
4+
5+
Add precompile step for colorSchemes

.prettierignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ storybook-static
77
docs/.cache
88
docs/public
99
**/.next/**
10+
packages/react/src/legacy-theme/ts/color-schemes.ts

packages/react/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
"build:docs:preview": "NODE_OPTIONS=--openssl-legacy-provider script/build-docs preview",
5050
"build:storybook:visual-testing": "storybook build",
5151
"build:components.json": "tsx script/components-json/build.ts",
52+
"build:precompile-color-schemes": "tsx script/precompile-color-schemes.ts",
5253
"storybook": "storybook",
5354
"type-check": "tsc --noEmit"
5455
},

packages/react/script/build

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ set -e
55
# Clean up
66
npm run clean
77

8+
# Generate color schemes
9+
npm run build:precompile-color-schemes
10+
811
# Bundle
912
npx rollup -c --bundleConfigAsCjs
1013

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import fs from 'fs'
2+
import path from 'path'
3+
import {colors} from '../src/legacy-theme/ts/colors'
4+
import {partitionColors, omitScale} from '../src/utils/theme'
5+
6+
const colorSchemes = Object.entries(colors).reduce((acc, [name, variables]) => {
7+
const {colors, shadows} = partitionColors(variables)
8+
return {
9+
...acc,
10+
[name]: {
11+
colors: omitScale(colors),
12+
shadows: omitScale(shadows),
13+
},
14+
}
15+
}, {})
16+
17+
const colorSchemeFileContent = `// This file is auto-generated by precompile-color-schemes.ts
18+
// run \`npm run build:precompile-color-schemes\` to regenerate
19+
20+
type Scheme = keyof typeof colors
21+
type SchemeValue = Record<'colors' | 'shadows', Partial<typeof colors.light>>
22+
23+
const colors = ${JSON.stringify(colorSchemes, null, 2)}
24+
25+
export const colorSchemes: Record<Scheme, SchemeValue> = colors as Record<Scheme, SchemeValue>
26+
`
27+
28+
fs.writeFileSync(path.join(__dirname, '../src/legacy-theme/ts/color-schemes.ts'), colorSchemeFileContent)

packages/react/src/legacy-theme/ts/color-schemes.ts

Lines changed: 4736 additions & 0 deletions
Large diffs are not rendered by default.

packages/react/src/theme.ts

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type {KeyPaths} from './utils/types/KeyPaths'
2-
import {colors} from './legacy-theme/ts/colors'
3-
import {partitionColors, fontStack, omitScale} from './utils/theme'
2+
import {fontStack} from './utils/theme'
3+
import {colorSchemes} from './legacy-theme/ts/color-schemes'
44

55
const animation = {
66
easeOutCubic: 'cubic-bezier(0.33, 1, 0.68, 1)',
@@ -58,23 +58,6 @@ const fontSizes = ['12px', '14px', '16px', '20px', '24px', '32px', '40px', '48px
5858

5959
const space = ['0', '4px', '8px', '16px', '24px', '32px', '40px', '48px', '64px', '80px', '96px', '112px', '128px']
6060

61-
type Scheme = keyof typeof colors
62-
type SchemeValue = Record<'colors' | 'shadows', Partial<typeof colors.light>>
63-
64-
const colorSchemes: Record<Scheme, SchemeValue> = Object.entries(colors).reduce(
65-
(acc, [name, variables]) => {
66-
const {colors, shadows} = partitionColors(variables)
67-
return {
68-
...acc,
69-
[name]: {
70-
colors: omitScale(colors),
71-
shadows: omitScale(shadows),
72-
},
73-
}
74-
},
75-
{} as Record<Scheme, SchemeValue>,
76-
)
77-
7861
const theme = {
7962
animation,
8063
borderWidths,

0 commit comments

Comments
 (0)