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

Electron compatibility #324

Merged
merged 4 commits into from
Nov 10, 2016
Merged

Conversation

anaisbetts
Copy link
Contributor

@anaisbetts anaisbetts commented Nov 9, 2016

This PR attempts to resolve a few issues around debug when using in Electron apps:

  1. Lots and lots of people use debug in 3rd party node modules, and they use it via require. This means that we'll end up loading node.js. While this should work just fine, in Electron, this causes problems because in renderer processes (i.e. processes with a DOM), tty and friends don't do The Thing You Expect (in fact, in older versions it used to crash!)

  2. When you attempt to fix this by loading debug/browser.js explicitly, this might work, but if anyone before you has required debug, exports.log will have been set to a tty output, which means that debug is effectively disabled

Instead, we're going to use Electron's process.type field to detect whether we're in a renderer process, and if so, we'll load the browser version. We'll also try to fetch process.env.DEBUG if LS isn't set to anything

@thebigredgeek
Copy link
Contributor

Looks good to me. This should also fix webpack I believe. @TooTallNate @tj thoughts?

@thebigredgeek
Copy link
Contributor

thebigredgeek commented Nov 9, 2016

As a side note we really need to add test coverage. I hate having to rely, purely, on manual code review for this kind of stuff haha

@anaisbetts
Copy link
Contributor Author

I was v careful because of this :)

* treat as a browser.
*/

if ((process || {}).type === 'renderer') {
Copy link
Contributor

Choose a reason for hiding this comment

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

In this case, I think process would always be defined, no?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There are a few esoteric edge cases in Electron WebViews where it's been removed. These cases are very unlikely, but I wanted to just be extra paranoid

@TooTallNate
Copy link
Contributor

I know this isn't your issue, but I wonder why Electron doesn't respect the browser entry point field in package.json when in the renderer process. That would kind of seem more compatible with the way browser bundlers like webpack and browserify work, and would avoid a patch like this in this case.

That said, this patch looks very nice and minimal. 👍 from me.

*/

if ((process || {}).type === 'renderer') {
module.exports = require('./browser');
Copy link
Contributor

@TooTallNate TooTallNate Nov 9, 2016

Choose a reason for hiding this comment

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

Maybe let's just add .js to the require calls to avoid extra stat() lookups.

@anaisbetts
Copy link
Contributor Author

anaisbetts commented Nov 9, 2016

I know this isn't your issue, but I wonder why Electron doesn't respect the browser entry point field in package.json when in the renderer process

The problem is that Electron is really its own thing, it is neither browser nor node (or really, both), and in general, I suspect that we'd be wrong more often (aka the library would exhibit incorrect / unusable behavior) by choosing browser rather than node.

@thebigredgeek
Copy link
Contributor

Looks good to me. Merging @TooTallNate @paulcbetts

@thebigredgeek thebigredgeek merged commit acd3f5f into debug-js:master Nov 10, 2016
@@ -142,6 +142,12 @@ function load() {
try {
r = exports.storage.debug;
} catch(e) {}

// If debug isn't set in LS, and we're in Electron, try to load $DEBUG
if ('env' in (process || {})) {
Copy link
Contributor

Choose a reason for hiding this comment

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

This broke component support. We've still got some old apps using it and https://github.com/component/cookie/blob/master/component.json#L8 broke the whole world :'(

...really need to get off of component, but still >_<

Copy link
Contributor

Choose a reason for hiding this comment

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

This likely broke Bower support too, as I don't believe it shims process

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

Successfully merging this pull request may close these issues.

4 participants