callstack / linaria

Zero-runtime CSS in JS library
https://linaria.dev
MIT License
11.66k stars 416 forks source link

CSS not generated inside `var a = function a() {...}` #1312

Open yume-chan opened 1 year ago

yume-chan commented 1 year ago

Environment

Description

a.js:

import {css} from '@linaria/atomic'

export const a = function a() {
    return css`
        background-color: red;
    `
}

b.js:

import { a } from "./a";

export default function App() {
  return <div className={a()}>Hello World!</div>;
}

css template string in a() will be replaced with names, but the CSS rules won't be generated.

https://github.com/callstack/linaria/blob/79557248f51f21663729add3a0564a830d8d4c87/packages/babel/src/utils/getTagProcessor.ts#L394-L421

parent is the const a node, but path.scope.getBinding(id.node.name) returns the function a() node. function a() node has no references so CSS extraction was skipped.

This code is simplified from an output of Solid.js babel plugin, so it can't change.

Reproducible Demo

https://codesandbox.io/p/sandbox/interesting-brook-vn8dgp

yume-chan commented 1 year ago

The actual code given to Linaria is:

Expand ```js import { template as _$template } from "solid-js/web"; import { className as _$className } from "solid-js/web"; import { getNextMarker as _$getNextMarker } from "solid-js/web"; import { createComponent as _$createComponent } from "solid-js/web"; import { getNextElement as _$getNextElement } from "solid-js/web"; import { insert as _$insert } from "solid-js/web"; import { effect as _$effect } from "solid-js/web"; import { setAttribute as _$setAttribute } from "solid-js/web"; import { $$component as _$$component } from "solid-refresh"; import { $$refresh as _$$refresh } from "solid-refresh"; import { $$registry as _$$registry } from "solid-refresh"; const _REGISTRY = _$$registry(); const _tmpl$ = /*#__PURE__*/_$template(`

Two styles in get ["class"]() functions are not extracted. Styles in _$effect function are extracted correctly.

I can't share the whole project.

yume-chan commented 1 year ago

This issue is still reproducible with Linaria v5 (@linaria/babel-preset@5.0.2).

Updated demo: https://codesandbox.io/p/sandbox/elastic-water-4wwgmy