Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The styled-jsx/babel Babel plugin incorrectly inserts ESM imports into CJS files #680

Closed
jaydenseric opened this issue Nov 16, 2020 · 2 comments · Fixed by #684
Closed

Comments

@jaydenseric
Copy link
Contributor

Do you want to request a feature or report a bug?

Bug!

What is the current behavior?

The styled-jsx/babel Babel plugin incorrectly inserts code using import instead of require when the Babel sourceType for the file is script (CJS).

If the current behavior is a bug, please provide the steps to reproduce and possibly a minimal demo or testcase in the form of a Next.js app, CodeSandbox URL or similar

See this reproduction in the Babel REPL.

With this input:

'use strict';

const React = require('react');

module.exports = function Foo() {
  return (
    <div>
      <style jsx>{`
        div {
          color: red;
        }
      `}</style>
    </div>
  );
};

Using the @babel/preset-react and styled-jsx/babel Babel plugins, with the sourceType set to script (CJS), the output code is:

"use strict";

import _JSXStyle from "styled-jsx/style";

const React = require("react");

module.exports = function Foo() {
  return /*#__PURE__*/ React.createElement(
    "div",
    {
      className: "jsx-4156136654"
    },
    /*#__PURE__*/ React.createElement(
      _JSXStyle,
      {
        id: "4156136654"
      },
      "div.jsx-4156136654{color:red;}"
    )
  );
};

Note that import _JSXStyle from "styled-jsx/style"; has been inserted. A require expression should have been inserted, not an import statement.

The CJS module format is now corrupted, resulting in code that can't be run by Node.js or be bundled correctly via webpack or other bundlers.

What is the expected behavior?

The styled-jsx/babel Babel plugin should always respect the module format indicated by the Babel sourceType when transforming code. If the sourceType is script, a require expression should be inserted, not an import statement.

Environment (include versions)

  • styled-jsx: v3.3.1

Did this work in previous versions?

  • Probably not.
@jaydenseric
Copy link
Contributor Author

These seem to be the offending locations in the Babel plugin code:

  • styled-jsx/src/babel.js

    Lines 309 to 310 in eac1703

    const importDeclaration = createReactComponentImportDeclaration(state)
    node.body.unshift(importDeclaration)
  • styled-jsx/src/_utils.js

    Lines 623 to 633 in eac1703

    export const createReactComponentImportDeclaration = state => {
    const styleModule =
    typeof state.opts.styleModule === 'string'
    ? state.opts.styleModule
    : 'styled-jsx/style'
    return t.importDeclaration(
    [t.importDefaultSpecifier(t.identifier(STYLE_COMPONENT))],
    t.stringLiteral(styleModule)
    )
    }

@jaydenseric
Copy link
Contributor Author

jaydenseric commented Nov 19, 2020

Edit: Sorry, I wrote this comment in the wrong repo 😅

jaydenseric added a commit to jaydenseric/styled-jsx that referenced this issue Nov 19, 2020
jaydenseric added a commit to jaydenseric/device-agnostic-ui that referenced this issue Dec 23, 2020
…ew JSX transform.

- Custom Babel plugins now run before and after the styled-jsx/babel plugin, to workaround vercel/styled-jsx#680 and vercel/styled-jsx#689 .
- Temporarily remove size-limit due to ai/size-limit#205 .
Timer added a commit that referenced this issue Jan 14, 2021
* Make the styled-jsx/babel Babel plugin respect the Babel source type.

Fixes #680 .

* chore: retrigger

Co-authored-by: Joe Haddad <timer150@gmail.com>
Co-authored-by: Joe Haddad <joe.haddad@zeit.co>
wroy7860 added a commit to wroy7860/styled-jsx-github-React-and-node that referenced this issue Sep 19, 2022
* Make the styled-jsx/babel Babel plugin respect the Babel source type.

Fixes vercel/styled-jsx#680 .

* chore: retrigger

Co-authored-by: Joe Haddad <timer150@gmail.com>
Co-authored-by: Joe Haddad <joe.haddad@zeit.co>
ericbrown2716 added a commit to ericbrown2716/styled-jsx-react-repo that referenced this issue Sep 29, 2022
* Make the styled-jsx/babel Babel plugin respect the Babel source type.

Fixes vercel/styled-jsx#680 .

* chore: retrigger

Co-authored-by: Joe Haddad <timer150@gmail.com>
Co-authored-by: Joe Haddad <joe.haddad@zeit.co>
renawolford6 pushed a commit to renawolford6/styled-jsx-development-react-and-node that referenced this issue Oct 6, 2022
* Make the styled-jsx/babel Babel plugin respect the Babel source type.

Fixes vercel/styled-jsx#680 .

* chore: retrigger

Co-authored-by: Joe Haddad <timer150@gmail.com>
Co-authored-by: Joe Haddad <joe.haddad@zeit.co>
johnfrench3 pushed a commit to johnfrench3/styled-jsx-Build-React that referenced this issue Nov 2, 2022
* Make the styled-jsx/babel Babel plugin respect the Babel source type.

Fixes vercel/styled-jsx#680 .

* chore: retrigger

Co-authored-by: Joe Haddad <timer150@gmail.com>
Co-authored-by: Joe Haddad <joe.haddad@zeit.co>
JiachenSmith pushed a commit to JiachenSmith/styled-jsx that referenced this issue Mar 21, 2023
* Make the styled-jsx/babel Babel plugin respect the Babel source type.

Fixes vercel/styled-jsx#680 .

* chore: retrigger

Co-authored-by: Joe Haddad <timer150@gmail.com>
Co-authored-by: Joe Haddad <joe.haddad@zeit.co>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant