Skip to content

production build, JSX code missing when returned in parentheses with leading comment #9066

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

Closed
paulcollett opened this issue May 26, 2020 · 4 comments

Comments

@paulcollett
Copy link

paulcollett commented May 26, 2020

Describe the bug

In production builds, with react-scripts build, any JSX code inside an implicit return with parentheses (), returns undefined when theres a leading comment before the JSX

Example A

const MyComponent = () => (
  // my comment
  <div>Output</div>
)

console.log(MyComponent()) // == undefined
console.log(MyComponent.toString()) // == function(){}

Example B

function MyComponent() {
  return (
    // comment
    <div>hello</div>
  )
}

console.log(MyComponent()) // == undefined

This issue only occurs within functions that return JSX. Variables work as expected. The development build is fine, this only occurs in production.

Without diving deeper I figure this issues lies in a dependency of react-scripts ..babel? ..or some compiler which now seems to treat the whole return body as a comment under these conditions.

This seems to be only occurring in a newer version of Create React App, we don't have this issue in a project running a year old CRA setup.

Did you try recovering your dependencies?

npm --version
6.10.3
yarn --version
1.17.0

Environment

  current version of create-react-app: 3.4.1
  running from (redacted)

  System:
    OS: mac
  Binaries:
    Node: 12.10.0
    Yarn: 1.17.0 
    npm: 6.10.3
  Browsers:
    Chrome: 83.0.4103.61
    Firefox: 74.0.1
    Safari: 13.0.3
  npmPackages:
    react: ^16.13.1 => 16.13.1
    react-dom: ^16.13.1 => 16.13.1
    react-scripts: 3.4.1 => 3.4.1
  npmGlobalPackages:
    create-react-app: Not Found

Steps to reproduce

With the code from above in a production build. react-scripts build

Expected behavior

compiling some fantastic components

Actual behavior

compiling components that return undefined

Console errors will vary depending on the runtime access of the component. This was first noticed as a black page (no output) along with a cryptic message indicating "Nothing was returned from render" https://reactjs.org/docs/error-decoder.html/?invariant=152&args[]=a

Reproducible demo

n/a

@paulcollett paulcollett changed the title Production compiling of arrow functions (under certain conditions) as an empty functions production, parentheses with a leading comment removes code body May 26, 2020
@paulcollett paulcollett changed the title production, parentheses with a leading comment removes code body production, arrow func with parentheses and a leading comment removes code body May 27, 2020
@paulcollett paulcollett changed the title production, arrow func with parentheses and a leading comment removes code body production build, JSX code missing when returned in parentheses with leading comment May 27, 2020
@tombola
Copy link

tombola commented May 28, 2020

I think maybe this is a result of React expecting there to be only one top level element returned by a render (and rightly or wrongly perceiving the comment as an element). If that is the case the same code should run fine if wrapped in a React.fragment.

Is this the case?

@sbusch
Copy link

sbusch commented May 31, 2020

I think this is a duplicate of #8687

@sbusch
Copy link

sbusch commented Jun 19, 2020

Update: see comment here #8687 (comment)

@stale
Copy link

stale bot commented Jul 19, 2020

This issue has been automatically marked as stale because it has not had any recent activity. It will be closed in 5 days if no further activity occurs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants