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

feat: create new defineConsts api #944

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open

Conversation

mellyeliu
Copy link
Member

@mellyeliu mellyeliu commented Mar 13, 2025

Context

Adds support for new API stylex.defineConsts to inject constants into the css transform pipeline and replace var(--token) usage with raw values when processing rules

// colors.stylex.js
import { defineConsts } from 'stylex'

export const colors = stylex.defineConsts({
  red: '#ff0000',
  green: '#00ff00',
})

// component.js
import stylex from 'stylex'
import { colors } from './colors.stylex'

const styles = stylex.create({
  box: {
    color: colors.red,
    backgroundColor: colors.green,
  },
})

shared

  • accepts a JS object of constants
  • returns an object mapping unique hashed keys to { constKey, constVal }
  • includes the hash as key to match css variable format

defineConsts babel transform

  1. Walk ast and detect call to stylex.defineConsts
  2. call stylexDefineConsts to convert obj to js output and metadata
  3. replace the call in the AST with a plain JS object (primarily for debugging capabilities)
    const colors = { red: '#ff0000' }
  4. register styles into state.registerStyles using the hash and { constKey, constVal } to be used in rules processing

processStylexRules

  1. extract constKey and constVal from any rules that came from defineConsts
    store them in dict from var(--hash) to value
  2. for each css rule in rules, replace:
    • var(--hash) to raw value
    • var(--hash, fallback) to raw value (to do in follow up with TokenParser)
      this is done for both ltr and rtl if present

Testing

Added transform and shared utils tests

Tested E2E in examples
image

Pre-flight checklist

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Mar 13, 2025
@mellyeliu mellyeliu force-pushed the feat/define-consts branch from 57562b0 to a114310 Compare March 13, 2025 03:51
Copy link

github-actions bot commented Mar 13, 2025

workflow: benchmarks/size

Comparison of minified (terser) and compressed (brotli) size results, measured in bytes. Smaller is better.

[email protected] size:compare
node size/compare.js /tmp/tmp.kar0MVnnnV /tmp/tmp.5xeEuzQDXA

Results Base Patch Ratio
stylex/lib/stylex.js
· compressed 985 1,000 1.02 +
· minified 3,154 3,255 1.03 +
stylex/lib/StyleXSheet.js
· compressed 1,266 1,266 1.00
· minified 3,776 3,776 1.00
benchmarks/size/.build/bundle.js
· compressed 537,611 537,611 1.00
· minified 7,435,904 7,435,904 1.00
benchmarks/size/.build/stylex.css
· compressed 100,609 100,609 1.00
· minified 755,721 755,721 1.00

@mellyeliu mellyeliu force-pushed the feat/define-consts branch from 0746071 to c352d3c Compare March 21, 2025 07:56
@mellyeliu mellyeliu changed the title [draft] feat: create new defineConsts api feat: create new defineConsts api Mar 21, 2025
@mellyeliu mellyeliu marked this pull request as ready for review March 21, 2025 08:01
@mellyeliu mellyeliu requested review from nmn and necolas March 21, 2025 08:02
@mellyeliu mellyeliu removed the wip label Mar 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants