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

Cannot import VarGroup using an alias defined only in TS. #426

Closed
GensIto opened this issue Feb 6, 2024 · 6 comments
Closed

Cannot import VarGroup using an alias defined only in TS. #426

GensIto opened this issue Feb 6, 2024 · 6 comments
Labels
enhancement New feature or request

Comments

@GensIto
Copy link

GensIto commented Feb 6, 2024

Describe the feature request

Hello,

I am currently working on creating a custom UI library using @stylexjs/stylex in a Vite project. However, I am encountering an issue when trying to use values defined with stylex.defineVars in stylex.create().

Here's the error message I receive during the build process:

✓ 5 modules transformed.
[vite-plugin-stylex] /privateWorkspace/vite-react/src/components/Button/styles.ts: Only static values are allowed inside of a stylex.create() call.
file: /Users/itougenta/Desktop/workspace/privateWorkspace/vite-react/src/components/Button/styles.ts
error during build:
This error suggests that only static values are allowed inside of a stylex.create() call. However, I am using values that I've defined with stylex.defineVars, and I expected them to be treated as static.

Could you please help me understand why this issue is occurring and suggest any possible solutions? I've double-checked my setup and code, but I can't seem to resolve this issue.

Thank you for your assistance.

Best regards,

@GensIto GensIto added the enhancement New feature or request label Feb 6, 2024
@olivierpascal
Copy link

Where did you use stylex.defineVars()?

@GensIto
Copy link
Author

GensIto commented Feb 7, 2024

@olivierpascal
thanks
I used src/styles/color.stylex.ts

import stylex from "@stylexjs/stylex";

export const colors = stylex.defineVars({
  primary: "black",
  secondary: "#333",
  accent: "blue",
  white: "white",
  black: "black",
  gray: "#333",
  lightGray: "#666",
  darkGray: "#222",
  red: "red",
  green: "green",
});

src/ui/Button.tsx

import stylex from "@stylexjs/stylex";
import { colors } from "@/styles/color.stylex";

export const styles = stylex.create({
  button: {
    color: colors.white,  // <- this
    fontSize: "1rem",
    padding: "1rem",
    backgroundColor: "black",
    border: "none",
    borderRadius: "0.25rem",
    cursor: "pointer",
    filter: {
      default: "drop-shadow(0 0 2em #646cffaa)",
      ":hover": "drop-shadow(0 0 2em #61dafbaa)",
    },
  },
});

errors occur when used in this way

@nmn
Copy link
Contributor

nmn commented Feb 7, 2024

The problem is that you're using an alias to import the variables. Replace "@/styles/color.stylex"; with a relative import to fix the problem.

You should also be able to configure StyleX with the aliases option to resolve these aliases for you. For now, the configuration needs absolute file paths. See the nextjs example in the repo for an example of how to do this.

@nmn nmn closed this as completed Feb 7, 2024
@nmn nmn changed the title Issue with stylex.create() in Vite Project Using @stylexjs/stylex Cannot import VarGroup using an alias defined only in TS. Feb 7, 2024
@GensIto
Copy link
Author

GensIto commented Feb 7, 2024

@nmn
I use vite as a build tool but it does not work well. Can you tell me how to fix this?

import path from "path";
import { defineConfig } from "vite";
import react from "@vitejs/plugin-react-swc";
import styleX from "vite-plugin-stylex";

export default defineConfig({
  plugins: [react(), styleX()],
  build: {
    lib: {
      entry: path.resolve(__dirname, "src/index.ts"),
      name: "index",
      fileName: "index",
    },
  },
  resolve: {
    alias: {
      "@": path.resolve(__dirname, "src"),
    },
  },
});

@olivierpascal
Copy link

olivierpascal commented Feb 7, 2024

I think you should use the aliases option of stylex:

styleX({
  aliases: {
    '@/*': [path.join(__dirname, 'src', '*')],
  },
})

See:

### `aliases`
```ts
aliases: {[key: string]: string | Array<string>} // Default: undefined
```
`aliases` option allows you to alias project directories to absolute paths, making it easier to import modules.
Example: `'@/components/*': [path.join(__dirname, './src/components/*')]`

@GensIto
Copy link
Author

GensIto commented Feb 7, 2024

@olivierpascal
Thank you, that went well😊

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants