Skip to content

Unwanted code hoist when defineStyleX was used #986

@BeiyanYunyi

Description

@BeiyanYunyi

Describe the bug

For this file:

<script setup lang="ts">
const a = 'b'
const styles = defineStyleX({ redBold: { color: 'red', fontWeight: 'bold' } })
</script>

<template>
  <p><span>Red</span> {{ a }}</p>
</template>

The result of transformDefineStyleX() should be:

<script lang="ts">
const styles = _stylex_create({ redBold: { color: 'red', fontWeight: 'bold' } })
</script>
<script setup lang="ts">
import { create as _stylex_create, props as _stylex_props } from '@stylexjs/stylex'

const a = 'b'
</script>

<template>
  <p><span>Red</span> {{ a }}</p>
</template>

However, it is now:

<script lang="ts">
const a = 'b'
const styles = _stylex_create({ redBold: { color: 'red', fontWeight: 'bold' } })
</script>
<script setup lang="ts">
import { create as _stylex_create, props as _stylex_props } from '@stylexjs/stylex'


</script>

<template>
  <p><span>Red</span> {{ a }}</p>
</template>

Which is unwanted.

It seems like it is my mistake when I was implementing defineStyleX, I'll try to fix it.

Reproduction

Described before. Not only the simple statement const a = 'b' is hoisted to global scope, but also the other statements (which must be runned in setup scope) are hoisted.

See https://github.com/BeiyanYunyi/vue-macros/blob/fix/define-stylex-unwanted-hoist/packages/define-stylex/tests/fixtures/other-statements.vue for more info.

System Info

System:
    OS: macOS 15.6
    CPU: (14) arm64 Apple M4 Pro
    Memory: 1016.44 MB / 24.00 GB
    Shell: 4.0.2 - /etc/profiles/per-user/<redacted>/bin/fish
  Binaries:
    Node: 22.18.0 - ~/.local/state/fnm_multishells/8154_1755573709277/bin/node
    npm: 10.9.3 - ~/.local/state/fnm_multishells/8154_1755573709277/bin/npm
    pnpm: 10.14.0 - ~/.local/state/fnm_multishells/8154_1755573709277/bin/pnpm
    bun: 1.2.20 - /etc/profiles/per-user/<redacted>/bin/bun
  Browsers:
    Chrome: 139.0.7258.128
    Safari: 18.6

Used Package Manager

pnpm

Validations

  • Follow our Code of Conduct
  • Read the Contributing Guide.
  • Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
  • Check that this is a concrete bug. For Q&A, please open a GitHub Discussion instead.
  • The provided reproduction is a minimal reproducible of the bug.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions