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

Adds a dummy function to assist react devtools to look for minification #9661

Closed

Conversation

ManasJayanth
Copy link
Contributor

Patch for react-devtools#694. Refer comment

cc @gaearon

// Refer https://github.com/facebook/react-devtools/issues/694#issuecomment-300535376
var dummyMinificationTestFn = function() {
if (__DEV__) {
var anotherDummyNoop = function() {};
Copy link
Collaborator

@gaearon gaearon May 11, 2017

Choose a reason for hiding this comment

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

How do we detect whether this code block was eliminated, judging by the source?
It can be tricky because it could be, for example, minified but not eliminated.

I think we should instead do something like return 42; in __DEV__ block instead.
Then, on DevTools side, we can check both that:

  • The function didn’t return anything (DEV mode check)
  • The function doesn’t contain 42 literal (dead code elimination check)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fair point. Making the changes.

@@ -559,9 +559,19 @@ var ReactDOM = {
};

if (typeof injectInternals === 'function') {
// A dummy function to check for minification during runtime
// Refer https://github.com/facebook/react-devtools/issues/694#issuecomment-300535376
var dummyMinificationTestFn = function() {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Let's rename it to testMinification.

@gaearon
Copy link
Collaborator

gaearon commented May 11, 2017

We should also probably refactor this a bit and put the testMinification injection into injectInternals itself. Since it's shared between all renderers and isn't specific to ReactDOM.

While you do that, could you please also remove conditional exports from ReactFiberDevToolsHook? This won't work with ES6 transition anyway. Instead we should always export them, but put their contents into a condition (which should only be evaluated when the module is initialized).

@ManasJayanth
Copy link
Contributor Author

While you do that, could you please also remove conditional exports from ReactFiberDevToolsHook? This won't work with ES6 transition anyway. Instead we should always export them, but put their contents into a condition (which should only be evaluated when the module is initialized).

I understand the concern, but I couldn't find any conditional exports in ReactFiberDevToolsHook

@gaearon
Copy link
Collaborator

gaearon commented May 11, 2017

Sorry I wasn’t clear. I mean stuff like this:

let injectInternals = null;
let onCommitRoot = null;
let onCommitUnmount = null;

Let's just keep them functions that are always properly exported, but change body of the functions depending on whether we found the hook or not.

@ManasJayanth
Copy link
Contributor Author

ManasJayanth commented May 11, 2017

Also,

  1. What about warning if one is not using devtools? Should there be a 'not a production build' warning too like how the stack ReactDOM warns currently?

  2. Inject testMinification in injectInternals like this?

inject(
  Object.assign( {}, internals, { testMinification })
);

Or use object-assign. I vaguely remember some discussion about avoiding Object.assign inside React.

@gaearon
Copy link
Collaborator

gaearon commented May 11, 2017

What about warning if one is not using devtools? Should there be a 'not a production build' warning too like how the stack ReactDOM warns currently?

Let's do whatever ReactDOM is doing for Fiber too.

Inject testMinification in injectInternals like this?

Yes, or we could make those functions explicit positional arguments.

I vaguely remember some discussion about avoiding Object.assign inside React.

It's fine to use it, as we compile it to use a polyfill.

@ManasJayanth
Copy link
Contributor Author

@gaearon Yet to fix conditional exports. If these commits look good, I'll push in that fix as well.

@gaearon
Copy link
Collaborator

gaearon commented May 11, 2017

I would prefer that we get rid of internals and just pass those two functions as regular arguments.

@ManasJayanth
Copy link
Contributor Author

I would prefer that we get rid of internals and just pass those two functions as regular arguments.

I'm confused. Can you clarify what two functions you are talking about? Also pass them as positional params to what? inject? But inject only accepts renderer as an object.

@ManasJayanth
Copy link
Contributor Author

@gaearon ^^

@gaearon
Copy link
Collaborator

gaearon commented May 12, 2017

I mean instead of passing injectInternals({funcA, funcB}) and then Object.assign-ing another function there, we can do injectInternals(funcA, funcB) and then create the target object once.

@ManasJayanth
Copy link
Contributor Author

Oh. That will affect renderers trying to use injectInternals. Eg. RN itself uses it. Are you sure? If yes, update RN renderer too?

@gaearon
Copy link
Collaborator

gaearon commented May 12, 2017

Yes, please update them both.

@ManasJayanth
Copy link
Contributor Author

@gaearon Pushed the changes requested. Pending: conditional exports issue.

injectInternals = function(
findFiberByHostInstance: (instance: any) => Fiber,
findHostInstanceByFiber: (component: Fiber) => any,
bundleType: number,
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think bundleType or version can be calculated here instead of being passed as arguments.

@ManasJayanth
Copy link
Contributor Author

@gaearon Pushed the requested changes

@ManasJayanth
Copy link
Contributor Author

@gaearon ReactNativeInspector just got added yesterday or so. So as we were discussing earlier, pass it along in the args and make it optional, given that ReactDOMFiber is not sending anything similar?

@gaearon
Copy link
Collaborator

gaearon commented May 25, 2017

Hey, sorry for the churn. There's many competing things happening at the same time and it gets a bit tricky to synchronize them. I guess let's go back to your previous approach with Object.assign since we're still adding more stuff there.

@ManasJayanth
Copy link
Contributor Author

@gaearon I can understand :) I have pushed the changes. Please have a look. Again, conditional exports is yet to be dealt with.

'the production build which skips development warnings and is faster. ' +
'See https://fb.me/react-minification for more details.',
);
rendererID = rendererID = inject(
Copy link
Collaborator

Choose a reason for hiding this comment

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

Unintentional double assignment?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry. Don't know how I missed that.

@ManasJayanth
Copy link
Contributor Author

@gaearon Good to go?

@ManasJayanth
Copy link
Contributor Author

@gaearon Does this PR need more work?

return 42;
}
};
warning(
Copy link
Collaborator

Choose a reason for hiding this comment

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

injectInternals only runs if DevTools exists.
Could you please move this out of the method?

Copy link
Contributor Author

@ManasJayanth ManasJayanth Jun 16, 2017

Choose a reason for hiding this comment

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

@gaearon Move testMinification out of injectInternals? Do we need it if there DevTools are not installed?

Copy link
Collaborator

Choose a reason for hiding this comment

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

We still should show the warning even if DevTools don't exist.

@ManasJayanth
Copy link
Contributor Author

ManasJayanth commented Aug 21, 2017

@gaearon Hi. Quite some time has passed by since this PR was raised. Is the idea behind it still valid? Should I resolve the conflicts and continue work?

@gaearon
Copy link
Collaborator

gaearon commented Sep 13, 2017

Sorry. I went on a holiday and later we’ve been in crunch mode fixing remaining issues in 16.

We did something related in #10446 but then reverted in #10673 due to the discussion in #10640.

I’m going to take another final look at this now and see if this approach still makes sense. We need some way to detect badly envified bundle but I’m not sure this is enough (or does the right thing) now.

@ManasJayanth
Copy link
Contributor Author

ManasJayanth commented Sep 14, 2017

@gaearon No problem :)

So if I understand right, calling toString() to check for minification is not an option anymore for reasons explained in #10640 comment (What an explanation btw! So much going on behind the scenes)

So is checking for minification still a valid problem to solve? I'm refering to this comment here

Why do we allow this to not be minimized at all?
The other concern was that if shipping works because of this bail out. Then if byte code reversing gets deployed, then it'll skip this and start failing. Without the site doing anything differently.

Is Sebastian trying to say that we are tacking the problem at the wrong place? Or that the build process itself must ensure minification (else face byte code reversing issues) and beyond the build process there's nothing much we can do.

@ManasJayanth
Copy link
Contributor Author

Does it also mean there's no spec compliant way to retrieve the source string?

@gaearon
Copy link
Collaborator

gaearon commented Sep 14, 2017

So if I understand right, calling toString() to check for minification is not an option anymore for reasons explained in #10640 comment

Not exactly—we can call it but shouldn’t do so from inside of react-dom for every single consumer. I settled on this approach as an alternative: #10702 and facebook/react-devtools#888. As you can see it’s not affected by those problems because:

  • We only call toString for people who have React DevTools which is better than doing this for everyone
  • We call it from inside of React DevTools so if it becomes problematic we can immediately disable it

I’m sorry I didn’t go ahead with your PR—unfortunately I didn’t realize the approach was flawed, and had to spend some time myself yesterday to figure out how to proceed.

And I also didn’t realize your PR doesn’t solve the problem with flat bundles. If the user has bad dead code elimination they will still get a minified function inside of React. That’s why putting the check outside was important.

@gaearon gaearon closed this Sep 14, 2017
@ManasJayanth
Copy link
Contributor Author

Ah, It alright. Better luck next time I guess. Thanks nevertheless.

Oh, and I was wondering, is it annoying as repo maintainers if contributors ping your on Twitter to get an update/feedback on a PR that's getting stale? Whats the right code of conduct in such a case? There are times when wished repo maintainers were available on IRC or something to give their opinion on an approach. Something more real time.

@gaearon
Copy link
Collaborator

gaearon commented Sep 14, 2017

You can always ping me in Twitter DM if you submitted a PR, just make it clear in first message :-)

In general we try to be more responsive. It's just been very hard to keep track of things as we have the giant checklist related to the new implementation of everything. I think we'll pick up some steam after 16 and be able to sort through contributions more quickly.

@ManasJayanth ManasJayanth deleted the inject-minification-testfn branch September 14, 2017 15:22
@ManasJayanth
Copy link
Contributor Author

ManasJayanth commented Sep 14, 2017

I can understand :) Great to hear this!

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

Successfully merging this pull request may close these issues.

3 participants