atlassian-labs / compiled

A familiar and performant compile time CSS-in-JS library for React.
https://compiledcssinjs.com
Apache License 2.0
1.98k stars 68 forks source link

Using object property (imported from CommonJS / CJS package) as media query results in failed build #1627

Open dddlr opened 7 months ago

dddlr commented 7 months ago

Example:

import { media } from '@atlaskit/primitives/responsive';
import { css, styled, cssMap } from '@compiled/react';
import React from 'react';

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

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

We expect this to work, because @compiled/babel-plugin runs a variety of functions specifically for resolving imported variables. However, we actually get a Cannot statically evaluate the value of "MemberExpression error.

This is because the function that finds exports in @atlaskit/primitives/responsive only looks for export statements in the form export { media } from './media-helper'; or perhaps export defaultExport from './test'. This fails with @atlaskit/primitives because there are no export statements to be found in the output, and so @compiled/babel-plugin cannot resolve the value of media.

Screenshot of @atlaskit/primitives

We see in the above screenshot that what we expected to be export { media } from './media-helper' in TypeScript is in fact this boilerplate code in @atlaskit/primitives:

Object.defineProperty(exports, "media", {
  enumerable: true,
  get: function get() {
    return _mediaHelper.media;
  }
});
var _mediaHelper = require("./media-helper");

Some potential ways to fix this issue:

Currently the way we get around this problem is to use a custom resolver. ESM build of @atlaskit/primitives works without issues, as long as our custom resolver returns the ESM build not the CommonJS build.

Tested with @atlaskit/primitives v1.17.0 and @compiled/babel-plugin v0.28.2