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

Class names being returned as [object Object] as of v2.3.0 #240

Closed
BrianAtIgloo opened this issue Apr 14, 2021 · 6 comments
Closed

Class names being returned as [object Object] as of v2.3.0 #240

BrianAtIgloo opened this issue Apr 14, 2021 · 6 comments
Assignees

Comments

@BrianAtIgloo
Copy link
Contributor

Hi, first off - thank you for maintaining this fantastic package!

That said, we've run into an issue with v2.3.0 where our bundled components are now outputting [object Object] class names because of this recent change to support a custom toString fn.

The problem only occurs after we run our code through our build pipeline which uses Rollup/Babel to produce a CJS bundle so we can render server-side.

Component Code:

  const inputStyleClass = classNames(styles['text_input__input'], {
    [styles['text_input__input--with-icon']]: !!icon,
  });

Transpiled code:

  var inputStyleClass = classnames(
    styles$z['text_input__input'],
    (_classNames2 = {},
        _defineProperty$2(_classNames2, styles$z['text_input__input--with-icon'], !!icon),
        _classNames2,
    )
  );

We've locked our components to 2.2.6 to resolve the issue and suggest the following change:

    if ((/\{\s*\[native code\]\s*\}/).test(arg.toString)) {
      for (var key in arg) {
        if (hasOwn.call(arg, key) && arg[key]) {
          classes.push(key);
        }
      }
    } else {
      classes.push(arg.toString());
    }
@dcousens
Copy link
Collaborator

dcousens commented Apr 14, 2021

The problem only occurs after we run our code through our build pipeline which uses Rollup/Babel to produce a CJS bundle so we can render server-side.

Do you mean that the pipeline is mangling the resultant object's toString?
Pull requests are accepted if you'd like to submit your suggestion, but we'll need to evaluate how this is happening and how to test against it

@dcousens dcousens self-assigned this Apr 14, 2021
@dcousens
Copy link
Collaborator

dcousens commented Apr 14, 2021

@BrianAtIgloo could you verify if !arg.hasOwnProperty('toString') resolves your issue?

@BrianAtIgloo
Copy link
Contributor Author

Thank you for the quick reply + troubleshoot! I confirmed using hasOwnProperty works, I can open a PR later today 👍

BrianAtIgloo added a commit to BrianAtIgloo/classnames that referenced this issue Apr 14, 2021
Updated to use `hasOwnProperty` over trying to compare the Object prototype which fails with some build pipelines after transpilation. See issue JedWatson#240.
dcousens pushed a commit that referenced this issue May 5, 2021
Updated to use `hasOwnProperty` over trying to compare the Object prototype which fails with some build pipelines after transpilation. See issue #240.
72636c added a commit to seek-oss/wingman that referenced this issue Jun 7, 2021
This is broken on classnames 2.3+, which can be pulled in via Renovate
lock file maintenance.

JedWatson/classnames#240
72636c added a commit to seek-oss/scoobie that referenced this issue Jun 7, 2021
This is broken on classnames 2.3+, which can be pulled in via Renovate
lock file maintenance.

JedWatson/classnames#240
72636c added a commit to seek-oss/scoobie that referenced this issue Jun 7, 2021
This is broken on classnames 2.3+, which can be pulled in via Renovate
lock file maintenance.

JedWatson/classnames#240
72636c added a commit to seek-oss/scoobie that referenced this issue Jun 7, 2021
This is broken on classnames 2.3+, which can be pulled in via Renovate
lock file maintenance.

JedWatson/classnames#240
72636c added a commit to seek-oss/wingman that referenced this issue Jun 7, 2021
This is broken on classnames 2.3+, which can be pulled in via Renovate
lock file maintenance.

JedWatson/classnames#240
@markdalgleish
Copy link
Contributor

We're experiencing this issue because we're executing our render code in a Node VM, which means we actually have multiple Object globals in memory and the toString equality check fails. I managed to create a minimal reproduction: https://runkit.com/embed/3svylngmhh23

@FanShiDe
Copy link

FanShiDe commented Aug 8, 2022

We're experiencing this issue because we're executing our render code in a Node VM, which means we actually have multiple Object globals in memory and the toString equality check fails. I managed to create a minimal reproduction: https://runkit.com/embed/3svylngmhh23

@markdalgleish hello, i'm getting same error when excute code with classnames in vm2 like node vm. How did you handle this issue

@dcousens
Copy link
Collaborator

Fixed in #281

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

Successfully merging a pull request may close this issue.

4 participants