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

When using an object property in a media query, as const in the object definition causes build to fail #1628

Open
dddlr opened this issue Feb 12, 2024 · 0 comments
Labels
bug 🐛 Something isn't working

Comments

@dddlr
Copy link
Collaborator

dddlr commented Feb 12, 2024

The following code fails with a Cannot statically evaluate the value of "MemberExpression error:

import { styled } from '@compiled/react';
import React from 'react';

const media = {
  from: {
    fromMobile: '@media (min-width: 30em)',
  },
} as const;

const SummaryContainer = styled.div({
  [media.from.fromMobile]: {
    color: 'blue',
  }
});

export const App = (): JSX.Element => (
  <>
    <SummaryContainer>Goodbye world?</SummaryContainer>
  </>
);

Removing as const resolves the error.

This is because @compiled/babel-plugin is not able to handle the TSAsExpression node when traversing the Babel AST. TSAsExpression is the node Babel uses to represent the { ... } as const syntax.

Note that as const can appear anywhere in an object, so we need to handle stuff like this too:

const media = {
  from: {
    fromMobile: '@media (min-width: 30em)',
  } as const,
};

These are the files that need to be updated to support the { ... } as const syntax (there might be more):


Workaround

You can try using readonly instead of as const. It's kind of awkward for large variables, but it avoids the errors:

type MediaType = {
  readonly from: { readonly fromMobile: string },
}

export const media: MediaType = {
  from: {
    fromMobile: '@media (min-width: 30em)',
  },
};
@dddlr dddlr added the bug 🐛 Something isn't working label Feb 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 🐛 Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant