Giter Site home page Giter Site logo

styled-jsx-bug-repro's Introduction

Reproduction of error _JSXStyle is not defined

This repo represents a minimal reproduction of one scenario in which I have encountered the error _JSXStyle is not defined.

The conditions that seem to be necessary to reproduce are:

  • using babel to transpile a react component that includes a <style jsx> element
  • @babel/preset-env is included in the babel presets
  • the component uses object destructuring in the props argument.

Steps to reproduce

npm install
npx babel Component.js

Input (Component.js)

export const Component = ({ className }) => (
  <div className={className}>
    <style jsx>{`
      div {
        background: red
      }
    `}
    </style>
  </div>
);

Output

"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.Component = void 0;

var _style = _interopRequireDefault(require("styled-jsx/style"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }

var Component = function Component(_ref) {
  var className = _ref.className;
  return /*#__PURE__*/React.createElement("div", {
    className: "jsx-2776096334" + " " + (className || "")
  }, /*#__PURE__*/React.createElement(_JSXStyle, {
    id: "2776096334"
  }, "div.jsx-2776096334{background:red;}"));
};

exports.Component = Component;

Note that styled-jsx/style is imported as:

var _style = _interopRequireDefault(require("styled-jsx/style"));

but then it is referred to later in the file as _JSXStyle which causes the error:

React.createElement(_JSXStyle, {
    id: "2776096334"
})

Changes which fix the issue

Any one of the following change on its own resolves the issue, resulting in the styled-jsx/style import being referenced correctly. ie.

React.createElement(_style, {
    id: "2776096334"
})
  1. Don't use object destructuring in the props argument
  2. Don't include the @babel/preset-env preset in the babel config
  3. Downgrading to [email protected]. The error occurrs in 3.3.3 and 3.4.4. I have not tested other versions.
  4. Exclude the transform-parameters module from @babel/preset-env eg.
{
  "presets": [
    [
      "@babel/preset-env", {
        "exclude": [
          "transform-parameters"
        ]
      }
    ],
    "@babel/preset-react"
  ],
  "plugins": [
    "styled-jsx/babel"
  ]
}

styled-jsx-bug-repro's People

Contributors

jamesharv avatar

Watchers

 avatar James Cloos 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.