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

Enable loose mode for class-properties #4248

Merged
merged 2 commits into from Apr 6, 2018
Merged

Conversation

rgrochowicz
Copy link
Contributor

Copying the contents of my MobX issue:
After trying out the latest create-react-app and mobx 4's decorate, I found that my classes were no longer observable. At first glance, it looks like it's Babel 7's class property transform.

Here's a codesandbox: https://codesandbox.io/s/623qwprm8k
I'd expect the updated value of FooBabel7 to trigger the autorun.

Differences in babel output:
Source:

class Foo {
  value = 1;
}

Babel 6:

class Foo {
  constructor() {
    this.value = 1;
  }
}

Babel 7:

class Foo {
  constructor() {
    Object.defineProperty(this, "value", {
      configurable: true,
      enumerable: true,
      writable: true,
      value: 1
    });
  }
}

Enabling loose mode reverts the transform back to the Babel 6 behavior.

Simple before/after:
Current behavior:
image

With loose: true:
image

Original MobX issue: mobxjs/mobx#1471

@facebook-github-bot
Copy link

Thank you for your pull request and welcome to our community. We require contributors to sign our Contributor License Agreement, and we don't seem to have you on file. In order for us to review and merge your code, please sign up at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need the corporate CLA signed.

If you have received this in error or have any questions, please contact us at cla@fb.com. Thanks!

@gaearon
Copy link
Contributor

gaearon commented Apr 3, 2018

My first thought is we want to be closer to the spec, not further from it.
Otherwise your code will rely on something that will eventually break.

@Timer
Copy link
Contributor

Timer commented Apr 3, 2018

I agree with @gaearon here, while it's unfortunate that this is now broken, it resulted from the ecosystem becoming closer in-line with the language specification.

I'm pretty sure our hands are tied here until mobxjs/mobx#1352 is resolved.

Can you point us to the code that broke because of this change? As in what specific file/line(s) that were dependent on the old behavior.

@rgrochowicz
Copy link
Contributor Author

Ah, running this in a browser with support for class properties (like Chrome Canary) also shows the errant behavior: https://plnkr.co/edit/dXlXUaZ0uDOSk0FUaOqm?p=preview.

Yeah, this is a MobX issue. Sadly, projects using decorate on react-scripts@1 will break when upgrading to react-scripts@2

@rgrochowicz
Copy link
Contributor Author

I'll go ahead and close this.

Thanks for the quick responses!

💃

@rgrochowicz rgrochowicz closed this Apr 3, 2018
@rgrochowicz rgrochowicz deleted the patch-1 branch April 3, 2018 18:45
@gaearon
Copy link
Contributor

gaearon commented Apr 3, 2018

Seems like it would be good to report to MobX that the way they're promoting the usage of decorate() API is not future-friendly.

@gaearon
Copy link
Contributor

gaearon commented Apr 5, 2018

I thought about this more and I’m not so confident now.
#4263

@gaearon
Copy link
Contributor

gaearon commented Apr 5, 2018

@rgrochowicz Can you please resubmit this PR?

@rgrochowicz rgrochowicz restored the patch-1 branch April 5, 2018 16:06
@rgrochowicz rgrochowicz reopened this Apr 5, 2018
@rgrochowicz
Copy link
Contributor Author

I've reopened this PR, if that works for you.

@@ -92,7 +92,14 @@ module.exports = function(api, opts) {
// don't work without it: https://github.com/babel/babel/issues/7215
require('@babel/plugin-transform-destructuring').default,
// class { handleClick = () => { } }
require('@babel/plugin-proposal-class-properties').default,
// Enable loose mode to use assignment instead of defineProperty, as some
// libraries add getters and setters to prototypes
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's change this to

// Enable loose mode to use assignment instead of defineProperty
// See discussion in https://github.com/facebook/create-react-app/issues/4263

@gaearon gaearon added this to the 2.0.0 milestone Apr 5, 2018
@gaearon
Copy link
Contributor

gaearon commented Apr 5, 2018

We also need something to ensure that the transform will be applied even if people target browsers that ship with it (latest Chrome)?

@Timer
Copy link
Contributor

Timer commented Apr 5, 2018

We also need something to ensure that the transform will be applied even if people target browsers that ship with it (latest Chrome)?

Are you referring to a test case?

@gaearon
Copy link
Contributor

gaearon commented Apr 5, 2018

Oh I guess it's still explicitly added. Never mind then.

@Timer Timer merged commit 1d4fdc2 into facebook:next Apr 6, 2018
tvalentius added a commit to tvalentius/create-react-app that referenced this pull request Apr 8, 2018
Enable loose mode for `class-properties` (facebook#4248)
kellyrmilligan added a commit to kellyrmilligan/create-react-app that referenced this pull request May 2, 2018
* upstream/next: (35 commits)
  Update envinfo and issue template (facebook#4375)
  Update sass-loader to 7.0.1 (facebook#4376)
  Support package distribution tags (facebook#4350)
  fix broken css module support in prod (facebook#4361)
  Bumped jest version to 22.4.1 (facebook#4362)
  bump babel 7 to beta 46
  bump lint-staged to node 10 compatible version
  documentation: Added License to the README.md (facebook#4294)
  Bump `fsevents`. (facebook#4331)
  Fix typo in e2e-simple.sh comment (facebook#4323)
  Add Sass loader (facebook#4195)
  Fix some typos in README.md (facebook#4286)
  Added learnstorybook.com to Storybook links (facebook#4298)
  Document multiple build environments via `env-cmd` facebook#4071 (facebook#4117)
  Fixed link to CSS imports blog post
  Update CSS Modules localIndetName (facebook#4192)
  Enable loose mode for `class-properties` (facebook#4248)
  bump babel 7 beta (facebook#4253)
  Small typo fix facebook#4217
  Changelog for 1.1.4
  ...
kellyrmilligan added a commit to kellyrmilligan/create-react-app that referenced this pull request May 2, 2018
* next: (35 commits)
  Update envinfo and issue template (facebook#4375)
  Update sass-loader to 7.0.1 (facebook#4376)
  Support package distribution tags (facebook#4350)
  fix broken css module support in prod (facebook#4361)
  Bumped jest version to 22.4.1 (facebook#4362)
  bump babel 7 to beta 46
  bump lint-staged to node 10 compatible version
  documentation: Added License to the README.md (facebook#4294)
  Bump `fsevents`. (facebook#4331)
  Fix typo in e2e-simple.sh comment (facebook#4323)
  Add Sass loader (facebook#4195)
  Fix some typos in README.md (facebook#4286)
  Added learnstorybook.com to Storybook links (facebook#4298)
  Document multiple build environments via `env-cmd` facebook#4071 (facebook#4117)
  Fixed link to CSS imports blog post
  Update CSS Modules localIndetName (facebook#4192)
  Enable loose mode for `class-properties` (facebook#4248)
  bump babel 7 beta (facebook#4253)
  Small typo fix facebook#4217
  Changelog for 1.1.4
  ...
@lock lock bot locked and limited conversation to collaborators Jan 20, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants