Skip to content

Conversation

@NekR
Copy link
Collaborator

@NekR NekR commented Apr 8, 2017

preact/debug is here 🎉

Usage

To use preact/debug, you have to set process.env.NODE_ENV to 'development' for your bundler, e.g. for webpack:

new webpack.DefinePlugin({
  'process.env': {
    'NODE_ENV': JSON.stringify('development')
  }
})

Also, preact/debug imports preact/devtools automatically 👌

Initialization

Basic

require('preact/debug');

// or

import 'preact/debug';

Configurable

require('preact/debug')({
  stringRefs: false
});

// or

import preactDebug from 'preact/debug';

preactDebug({
  stringRefs: false
});

Config options

(all turned on by default)

  • undefinedComponents: boolean, throw or not on undefined components
  • stringRefs: boolean, throw or not on string refs (this throws anyway, but better track trace with the option)
  • arrayChildren: boolean, warn or not about missing key properties on elements in array children

Warning example:

image

Side notes

  • Source directory is debug_src/ and is transpiled to debug/. Ideally source directory should be debug/ and transpiled to debug.js, but I don't know anyway to do so with requiring only './debug.js' module in it an ignoring everything else (can be done with browserify but WAT 😨).
  • Another way might be to publish separate package called preact-debug, but there is a problem that webpack makes ES6+ imports getter only so you won't be able to override preact.h property. If this could be worked around, then probably is the way to go.
  • preact.createElement isn't hooked just yet

@NekR NekR requested review from developit and robertknight April 8, 2017 11:49
@NekR
Copy link
Collaborator Author

NekR commented Apr 8, 2017

If this landed, #601 might not be necessary (which was fixing #431).

@NekR
Copy link
Collaborator Author

NekR commented Apr 8, 2017

image

About folder structure. Also if we are going preact-debug package way, we can make it export preact itself (this way we solve webpack imports problem) and it could be aliased in dev mode. Though it's easier to include debug in the code rather than in config :-)

@lukeed
Copy link
Member

lukeed commented Apr 8, 2017

The debug_src can be dropped & everything moved to a single file (debug.js) if everything were wrapped within the NODE_ENV !== 'development' check.

@robertknight
Copy link
Member

What is the rationale for making each of these warnings configurable? I would be inclined to make the debug package zero or minimally configurable until we have a clear use case, since that reduces the risk of having to make a breaking change in future.

arrayChildren: true
};

preact.h = function(nodeName, attributes) {
Copy link
Member

Choose a reason for hiding this comment

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

Could this be built on Preact's existing options.vnode hook rather than patching h directly? Then it should work for createElement as well. This would also be consistent with how the devtools module works - hooking into Preact's global hooks.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It could, but that way you won't get good stack trace, i.e. the call before throwing the error will point directly to the place of h() invocation. If this won't provide good stack traces I'm not really interested in having this package myself.

Copy link
Member

Choose a reason for hiding this comment

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

I want to check if we can prune the stack trace - we'd know it's 2 levels up, and the stack is just a String so we could remove the first two lines. My reasoning is that preact is actually a frozen object when bundled via Webpack 2 - this is already causing issues with React Ho Loader since it does the patching technique used here, and I'm worried it's going to become an issue. Thoughts?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Stacks aren't that simple. Firefox have weird stack format and you cannot just modify it. There is no docs about the format and people in Mozilla IRC doesn't answer really to that question. There might be some lib by Fx extensions team, but I don't know about it.

Copy link
Member

Choose a reason for hiding this comment

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

It could, but that way you won't get good stack trace

Not sure I follow. Surely throwing in the vnode hook would just result in a trace with the point of the h(...) invocation being one more entry up in the stack trace?

Copy link
Member

@robertknight robertknight left a comment

Choose a reason for hiding this comment

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

Couple of initial thoughts:

  1. Could this be built on the existing global hooks rather than trying to replace exports directly? This would be consistent with how the devtools integration and a number of other debugging/testing aids for Preact work.
  2. I would be inclined to drop the configurability in the initial release of this unless you have a clear use case for it.

@NekR
Copy link
Collaborator Author

NekR commented Apr 8, 2017

@robertknight I for example need only undefinedComponents option, all other are annoying to me. Maybe stringRefs are fine, but arrayChildren for sure would annoy me.

@NekR
Copy link
Collaborator Author

NekR commented Apr 8, 2017

@robertknight

I would be inclined to drop the configurability in the initial release of this unless you have a clear use case for it.

@developit suggests to not use forEach((a, i) => <div key={i} />) because it already use indexes internally and specifying them that way may more hurt than help. In that case, I would expect people to disable that warning.

This is in addition to that this option is just annoying for myself.

@developit
Copy link
Member

I'm inclined to agree about the keys option, only because I've got codebases that would probably crash the console from the sheer number of warnings that would generate 😇

@NekR one option would be to hold off on releasing the key check since that's a pretty huge assertion on our part and a common cause of FUD around how keys work.

So maybe if we turned off key warnings for now we could release with no options? Still keep it a constructor but have the defaults be to warn both for undefined components and string refs.

BTW - I wonder if we need to detect preact-compat and disable the string ref warning if it's in-place, since that adds support for them?

@NekR
Copy link
Collaborator Author

NekR commented Apr 8, 2017

till keep it a constructor but have the defaults be to warn both for undefined components and string refs.

As I said in PR comment all debug checks are ON by default :-) Config is there just to disable things.

BTW - I wonder if we need to detect preact-compat and disable the string ref warning if it's in-place, since that adds support for them?

Makes sense, not sure how to so it though.

@developit
Copy link
Member

I just know the keys warning is something I've complained about in React a lot, I'm kinda tempted to avoid promoting that people go crazy with keys out-of-the-box haha.

@NekR
Copy link
Collaborator Author

NekR commented Apr 8, 2017

@lukeed, @robertknight, @developit fine, fine, I'll do as you all want 😇

@NekR
Copy link
Collaborator Author

NekR commented Apr 11, 2017

Okay, I did requested changed but didn't test them :-)

debug.js Outdated
@@ -0,0 +1,90 @@
'use strict';
Copy link
Member

Choose a reason for hiding this comment

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

we should .gitignore the built files here

return oldVnodeOption.call(this, vnode);
};

const inspectChildren = (children, inspect) => {
Copy link
Member

Choose a reason for hiding this comment

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

children should always be pre-flattened (though I'm fine leaving this here)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I'll remove that eventually, too lazy right now.

Copy link
Member

@developit developit left a comment

Choose a reason for hiding this comment

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

Everything looks good, just thinking we'll want to gitignore the built files. Also it might be nice to bundle to UMD (-f umd) so they run in a browser context too?

@NekR
Copy link
Collaborator Author

NekR commented Apr 15, 2017

@developit I did with debug the same what you do with devtools. Also, there is require('preact') in debug, how would that work in browser?

@developit
Copy link
Member

Devtools seems to be UMD - https://unpkg.com/preact@8.1.0/devtools.js

The preact import gets transformed to a detection, should work nicely.

@NekR
Copy link
Collaborator Author

NekR commented Apr 15, 2017

I mean with folder/generation. You have devtools/index.js folder and devtools.js file.

@developit
Copy link
Member

True, and that's working (guess that's my answer haha).

@developit
Copy link
Member

I think we're ready to merge this :)

@NekR
Copy link
Collaborator Author

NekR commented Apr 18, 2017

@developit yeah, I think so. But if you wish it to be UMD then I can try that.

@coveralls
Copy link

Coverage Status

Coverage remained the same at 100.0% when pulling eef7d70 on OpenJSX:master into 4a9aefc on developit:master.

@coveralls
Copy link

Coverage Status

Changes Unknown when pulling b0263af on OpenJSX:master into ** on developit:master**.

@NekR NekR merged commit b80bec3 into preactjs:master May 27, 2017
@NekR
Copy link
Collaborator Author

NekR commented May 27, 2017

I merged it ¯\_(ツ)_/¯

@trueadm
Copy link

trueadm commented May 28, 2017

In React, we make warnings only fire once so the console isn't spammed. Might be worth doing it here too :)

@NekR
Copy link
Collaborator Author

NekR commented May 28, 2017

For the same element/component or in general write to console once for a each warning?

@trueadm
Copy link

trueadm commented May 28, 2017

For each warning usually. Spamming a console is known to have an adverse affect on DX. We had really great feedback when we changed how this worked.

@developit
Copy link
Member

I like that idea. Keeping it global keeps things simple.

@NekR
Copy link
Collaborator Author

NekR commented May 30, 2017 via email

@rupav
Copy link

rupav commented May 30, 2018

@NekR I see a different and strange behavior of React-dev-tools using Preact/debug. React-dev-tools are active with this config only (as suggested in this PR description), in our project, we are using Preact with React as an alias.
I also would like to know if Redux-store dev tools can be configured for Preact? 😅

@marvinhagemeister
Copy link
Member

@rupav the redux devtools should work right out of the box as they are not framework dependent.

@rupav
Copy link

rupav commented May 31, 2018

Forgive me @marvinhagemeister for asking without checking again. I had not configured setup redux-dev-tools on configureStore. Yeah its working great. But what is your opinion on React-dev-tools with Preact/debug? How it can be best utilized.

@marvinhagemeister
Copy link
Member

@rupav easiest way to do it is by including preact/debug. If something isn't working correctly you should file a bug :)

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants