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

Buildable react libs with swc compiler are not emitting jsx-runtime #8869

Closed
JamesHenry opened this issue Feb 6, 2022 · 1 comment · Fixed by #10220
Closed

Buildable react libs with swc compiler are not emitting jsx-runtime #8869

JamesHenry opened this issue Feb 6, 2022 · 1 comment · Fixed by #10220
Labels
outdated scope: react Issues related to React support for Nx type: bug

Comments

@JamesHenry
Copy link
Collaborator

JamesHenry commented Feb 6, 2022

[CORRECT] Babel Project

Creation

nx generate @nrwl/react:library --name=buildbabel --buildable --compiler=babel

Output

dist/libs/buildbabel/index.esm.js

import { jsx } from 'react/jsx-runtime';

function Buildbabel(props) {
  return jsx("div", {
    children: jsx("h1", {
      children: "Welcome to Buildbabel!"
    }, void 0)
  }, void 0);
}

export { Buildbabel };

[INCORRECT] SWC Project

Creation

nx generate @nrwl/react:library --name=buildswc --buildable --compiler=swc

Output

dist/libs/buildswc/index.esm.js

function Buildswc(props) {
    return(/*#__PURE__*/ React.createElement("div", null, /*#__PURE__*/ React.createElement("h1", null, "Welcome to Buildswc!")));
}

export { Buildswc };

The end result of this is that apps consuming a library built with SWC as the compiler will throw React is undefined at runtime (which is how I discovered this).

I can only assume the swc instance is somehow currently not being passed the relevant config when building:

"jsc": {
  "transform": {
       "react": {
          "runtime": "automatic"
       }
   }
}

This config is being explicitly wired up in the jest.config.js however:

module.exports = {
  displayName: 'buildswc',
  preset: '../../jest.preset.js',
  transform: {
    '^.+\\.[tj]sx?$': [
      '@swc/jest',
      { jsc: { transform: { react: { runtime: 'automatic' } } } },
    ],
  },
  moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx'],
  coverageDirectory: '../../coverage/libs/buildswc',
};
@github-actions
Copy link

This issue has been closed for more than 30 days. If this issue is still occuring, please open a new issue with more recent context.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 22, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
outdated scope: react Issues related to React support for Nx type: bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant