Code Monkey home page Code Monkey logo

babel-plugin-preeval-refresh-reg's Introduction

babel-plugin-preeval-refresh-reg

Problem

This is the example React code that goes through wyw-in-js preeval stage:

const Divider = /*#__PURE__*/ _s(
  React.forwardRef(
    (_c = _s(
      function Divider(inProps, ref) {
        _s();
        // ...
        return /*#__PURE__*/ _jsx(
          DividerRoot,
          _extends(
            {
              as: component,
              className: clsx(classes.root, className),
              role: role,
              ref: ref,
              ownerState: ownerState,
            },
            other,
            {
              children: children
                ? /*#__PURE__*/ _jsx(DividerWrapper, {
                    className: classes.wrapper,
                    ownerState: ownerState,
                    children: children,
                  })
                : null,
            }
          )
        );
      },
      '526RDEpO1CGEADq5vnCvoNyCejQ=',
      false,
      function() {
        return [useThemeProps, useUtilityClasses];
      }
    ))
  ),
  '526RDEpO1CGEADq5vnCvoNyCejQ=',
  false,
  function() {
    return [useThemeProps, useUtilityClasses];
  }
);
_c1 = Divider;
/**
 * The following flag is used to ensure that this component isn't tabbable i.e.
 * does not get highlight/focus inside of MUI List.
 */ Divider.muiSkipListHighlight = true;

export default Divider;
var _c, _c1;
$RefreshReg$(_c, 'Divider$React.forwardRef');
$RefreshReg$(_c1, 'Divider');

The $RefreshReg$ is injected by React Refresh from a framework like Next.js or Vite. In the preeval phase, wyw-in-js will remove the global $RefreshReg$ but there is a bug in removing the _c (I am not sure is it the bug in wyw-in-js or babel) so the code that go through evaluation looks like this:

const Divider = /*#__PURE__*/ _s(
  React.forwardRef(
    (_c = _s(
      function Divider(inProps, ref) {
        _s();
        // ...
      },
      '526RDEpO1CGEADq5vnCvoNyCejQ=',
      false,
      function() {
        return [useThemeProps, useUtilityClasses];
      }
    ))
  ),
  '526RDEpO1CGEADq5vnCvoNyCejQ=',
  false,
  function() {
    return [useThemeProps, useUtilityClasses];
  }
);
/**
 * The following flag is used to ensure that this component isn't tabbable i.e.
 * does not get highlight/focus inside of MUI List.
 */ Divider.muiSkipListHighlight = true;

export default Divider;

Causing the error _c is not defined.

Solution

This plugin will replace the _c = _s(...) with _s(...) directly so that there is no use of the variable to make the evalution works.

Installation

npm install babel-plugin-preeval-refresh-reg

Usage

// .babelrc
{
  "plugins": ["babel-plugin-preeval-refresh-reg"]
}

babel-plugin-preeval-refresh-reg's People

Contributors

siriwatknp avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.