nx icon indicating copy to clipboard operation
nx copied to clipboard

SCSS React library with swc fails to build

Open jcguarinpenaranda opened this issue 3 years ago • 4 comments

Current Behavior

I created a React UI library, which works ok in dev mode but fails to build.

imagen

It seems to be the scss modules are being treated as JS at some point, which should not be as they should be processed before by SASS.

Expected Behavior

I would expect the library to build without problems. Related content on: https://github.com/nrwl/nx/pull/2719

Steps to Reproduce

The setup I have is very minimal, two components in the lib: full-page-wrapper and box The styles for the first component are:

imagen

And the react component looks like this imagen

I don't include the second component for the sake of simplicity, but it's also a very simple one. As you see I'm using a .modules.scss file that is imported at the top of the component and then use the key full-page-wrapper coming from the object it returns, that matches what I have in the scss module.

I have tried using the styles and stylePreprocessorOptions options without luck. My project file looks like this: imagen

Also added the assets to be compiled imagen

When I'm working on another app that is connected to this React library in development mode, I have no issue whatsoever and it's very fast. When I build I see the error shown at the top of this issue.

Failure Logs

Environment

Windows 11

imagen

jcguarinpenaranda avatar Mar 01 '22 13:03 jcguarinpenaranda

Nx Cloud Run: https://nx.app/runs/btcaeVyltfH

jcguarinpenaranda avatar Mar 01 '22 13:03 jcguarinpenaranda

I found another way of reproducing.

  1. Generate a new react library, using --name=test2 --buildable --importPath=@org/scss-test --publishable --compiler=swc
  2. Try to build the generated library. Works OK
  3. Add any class to the css file generated
  4. Try to build again. Error thrown is the same as my original error found. Example: https://nx.app/runs/8zVEWJInpvk

After testing several times, I found using the swc compiler produces the issue. Using babel this does not happen, although it loses the obvious advantage of faster compilation.

jcguarinpenaranda avatar Mar 01 '22 13:03 jcguarinpenaranda

I'm having the exact same issue. And I spent hours configuring rollup.config.js for CSS Modules. For folks who are here because of CSS Modules, a tentative fix would be to change the compiler to babel.

aruke avatar Mar 22 '22 17:03 aruke

This issue has been automatically marked as stale because it hasn't had any recent activity. It will be closed in 14 days if no further activity occurs. If we missed this issue please reply to keep it active. Thanks for being a part of the Nx community! 🙏

github-actions[bot] avatar Sep 19 '22 00:09 github-actions[bot]

Is this issue still being investigated? I am having the exact same issue - not being able to use SWC to bundle react libraries with the unexpected token error for css/sass modules.

MarcieMarc425 avatar Sep 27 '22 03:09 MarcieMarc425

seems like this is no longer an issue when I bump NX version to 14.8.1. (I have not extensively tested which version gets rid of this issue, but I previously was experiencing this issue in NX version 14.3.x)

MarcieMarc425 avatar Oct 02 '22 07:10 MarcieMarc425

I believe that this is no longer an issue in the latest version of Nx. I will be closing this issue. Feel free to ping me to reopen if the issue comes up again!

mandarini avatar Feb 13 '23 18:02 mandarini

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[bot] avatar Mar 20 '23 22:03 github-actions[bot]