Skip to content

The mangler may break React Refresh's heuristic #11227

@arcanis

Description

@arcanis

Describe the bug

Prior context:

  • React Refresh locates React components to wrap them into special code
  • When a component changes, React Refresh blocks the live reload and triggers a page re-render
  • To detect whether something is a component, React Refresh will check the function name
  • If it starts with an uppercase, it's a component (no static analysis check)

The problem is that SWC will mangle variable names by using the 26 lowercase alphabetical letters. If it goes past that, it'll start using uppercase letters. This will break React Refresh's naive heuristic and cause it to disable live reload in the file listing the value.

To account for that, SWC should check whether the value assigned to the variable is a function literal, and preserve the first character's casing so that the heuristic returns the same result before/after.

Input code

const test1 = () => {};
const test2 = () => {};
const test3 = () => {};
const test4 = () => {};
const test5 = () => {};
const test6 = () => {};
const test7 = () => {};
const test8 = () => {};
const test9 = () => {};
const test10 = () => {};
const test11 = () => {};
const test12 = () => {};
const test13 = () => {};
const test14 = () => {};
const test15 = () => {};
const test16 = () => {};
const test17 = () => {};
const test18 = () => {};
const test19 = () => {};
const test20 = () => {};
const test21 = () => {};
const test22 = () => {};
const test23 = () => {};
const test24 = () => {};
const test25 = () => {};
const test26 = () => {};
const test27 = () => {};
const test28 = () => {};
const test29 = () => {};
const test30 = () => {};

console.log([test1, test2, test3, test4, test5, test6, test7, test8, test9, test10, test11, test12, test13, test14, test15, test16, test17, test18, test19, test20, test21, test22, test23, test24, test25, test26, test27, test28, test29, test30]);
console.log([test1, test2, test3, test4, test5, test6, test7, test8, test9, test10, test11, test12, test13, test14, test15, test16, test17, test18, test19, test20, test21, test22, test23, test24, test25, test26, test27, test28, test29, test30]);

Config

Link to the code that reproduces this issue

https://play.swc.rs/?version=1.14.0&code=H4sIAAAAAAAAA%2BWSOwqDQABE%2B5xiSwUJzqxfxFwkpBJJI7HQLuTuAXcSSDEnSDXwFh77Vqf1se1hn7cdYQxZHsZLeL6G0%2FTlNDwaXhleG94Y3hreGd4bjtIduGK4ZLhmuGi4arhsuG64cLhyunLab%2B3K6crpyunK6crpyunK6crjb%2Flxsi7zeVnv2fV4syIJ0sQ0VZo6TZOmTdOl6Qv9U1p5IBFkglSQC5JBNkgH%2BSgfP%2FeSj%2FJRPspH%2BSgf5aN8sbzlw59WvwFMA%2Ba00gQAAA%3D%3D&config=H4sIAAAAAAAAA32QQQ6CMBBF95yCzNqFLjTGO3gGM6kDKba06RQiIdzdFihCJG7azv9vOpnfZ3kOFQu45X14hsKiY3JLHRTuao%2FvoAAJjSyctB4Oya04WgUqplEaJgc8upL82MXnGQdlDFPCZ03LWhbdeqAw2jpi3oIRxbpUtELjHGMVtaSC6l2zsMF5EdmHUMhco6af3xJR%2FHWtky16Crexe5Ck647KWKCTp%2BMmmZBNls4xJdDm2az2Ad9ZmhK7wBdK%2BSxDQPI9dcads%2BED2pRQiMcBAAA%3D

SWC Info output

No response

Expected behavior

The post-mangling variables should all start with a lowercase letter.

Actual behavior

Some variables now start with an uppercase letter.

Version

1.14.0

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions