Skip to content

Share theming variables in a (turbo) monorepo #885

@tilnea

Description

@tilnea

Hi,

I'm working in a turborepo, and I have a file theme.stylex.ts that has a lot of theme variables looking similar this

export const baseColors = defineVars({
  "coal-100": "#FAFAFA",
  "coal-200": "#E4E4E4",
  "coal-300": "#D4D4D8",
  "green-100": "#E8F4F4",
  "green-200": "#D0E4E2",
  "green-300": "#B2D4D0",
  "neutral-400": "#B7BABD",
  "salmon-700": "#D23434",
});

I'm exporting it from my ui package like this

"exports": {
    ".": "./index.ts",
    "./global.css": "./src/global.css",
    "./theme": "./src/shared/theme.stylex.ts"
 }

I'm using Vite in my projects, and my vite.config.js looks something like this

import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
import styleX from 'vite-plugin-stylex';

import { fileURLToPath, URL } from 'node:url'

export default defineConfig({
  plugins: [
    react(),
    styleX({
      aliases: {
        '@repo/ui': '@repo/ui',
      }
    }),
  ],
  resolve: {
    alias: {
      '@': fileURLToPath(new URL('./src', import.meta.url))
    },
    extensions: ['.js', '.json', '.jsx', '.mjs', '.ts', '.tsx', '.vue']
  },
  server: {
    port: 8080
  }
})

Creating and working with StyleX in this project works fine, but when I try to import my baseColors I get this error

[plugin:vite-plugin-stylex] StyleX Error: path/src/components/border/Border.styles.jsx: Only static values are allowed inside of a stylex.create() call.
  1 | import * as stylex from "@stylexjs/stylex";
  2 | import { theme } from "@repo/ui/theme";
> 3 | export const styles = stylex.create({
    |                       ^
  4 |   border: {
  5 |     border: "1px solid red",
  6 |     color: baseColors["coal-100"]
  
💡 If you're importing StyleX tokens or styles from another file using aliases, make sure to define those in your Vite config or in the StyleX Plugin options.

Any idea how I can get it to work?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions