Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add precompile step for colorSchemes #4337

Merged
merged 5 commits into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/tasty-spoons-suffer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@primer/react": patch
---

Add precompile step for colorSchemes
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ storybook-static
docs/.cache
docs/public
**/.next/**
packages/react/src/legacy-theme/ts/color-schemes.ts
1 change: 1 addition & 0 deletions packages/react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"build:docs:preview": "NODE_OPTIONS=--openssl-legacy-provider script/build-docs preview",
"build:storybook:visual-testing": "storybook build",
"build:components.json": "tsx script/components-json/build.ts",
"build:precompile-color-schemes": "tsx script/precompile-color-schemes.ts",
"storybook": "storybook",
"type-check": "tsc --noEmit"
},
Expand Down
3 changes: 3 additions & 0 deletions packages/react/script/build
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ set -e
# Clean up
npm run clean

# Generate color schemes
npm run build:precompile-color-schemes

# Bundle
npx rollup -c --bundleConfigAsCjs

Expand Down
28 changes: 28 additions & 0 deletions packages/react/script/precompile-color-schemes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import fs from 'fs'
import path from 'path'
import {colors} from '../src/legacy-theme/ts/colors'
import {partitionColors, omitScale} from '../src/utils/theme'

const colorSchemes = Object.entries(colors).reduce((acc, [name, variables]) => {
const {colors, shadows} = partitionColors(variables)
return {
...acc,
[name]: {
colors: omitScale(colors),
shadows: omitScale(shadows),
},
}
}, {})

const colorSchemeFileContent = `// This file is auto-generated by precompile-color-schemes.ts
// run \`npm run build:precompile-color-schemes\` to regenerate

type Scheme = keyof typeof colors
type SchemeValue = Record<'colors' | 'shadows', Partial<typeof colors.light>>

const colors = ${JSON.stringify(colorSchemes, null, 2)}

export const colorSchemes: Record<Scheme, SchemeValue> = colors as Record<Scheme, SchemeValue>
`

fs.writeFileSync(path.join(__dirname, '../src/legacy-theme/ts/color-schemes.ts'), colorSchemeFileContent)
4,736 changes: 4,736 additions & 0 deletions packages/react/src/legacy-theme/ts/color-schemes.ts

Large diffs are not rendered by default.

21 changes: 2 additions & 19 deletions packages/react/src/theme.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type {KeyPaths} from './utils/types/KeyPaths'
import {colors} from './legacy-theme/ts/colors'
import {partitionColors, fontStack, omitScale} from './utils/theme'
import {fontStack} from './utils/theme'
import {colorSchemes} from './legacy-theme/ts/color-schemes'

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

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

type Scheme = keyof typeof colors
type SchemeValue = Record<'colors' | 'shadows', Partial<typeof colors.light>>

const colorSchemes: Record<Scheme, SchemeValue> = Object.entries(colors).reduce(
(acc, [name, variables]) => {
const {colors, shadows} = partitionColors(variables)
return {
...acc,
[name]: {
colors: omitScale(colors),
shadows: omitScale(shadows),
},
}
},
{} as Record<Scheme, SchemeValue>,
)

const theme = {
animation,
borderWidths,
Expand Down
Loading