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

🎨 add first class debugging support #2041

Closed
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions packages/react-scripts/bin/react-scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,17 @@

var spawn = require('cross-spawn');
var script = process.argv[2];
var args = process.argv.slice(3);
var getArgs = require('../scripts/utils/getSubscriptArgs');

switch (script) {
case 'build':
case 'eject':
case 'start':
case 'test':
var result = spawn.sync(
'node',
[require.resolve('../scripts/' + script)].concat(args),
{ stdio: 'inherit' }
);
var scriptFilename = require.resolve('../scripts/' + script);
var result = spawn.sync('node', getArgs(scriptFilename), {
stdio: 'inherit',
});
Copy link
Author

Choose a reason for hiding this comment

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

it took me a while to figure out what was going on here--the husky git hook lint script kept changing the format out from under me!

Copy link
Contributor

@Timer Timer Apr 28, 2017

Choose a reason for hiding this comment

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

🙈

also, do we not already pass args in via [require.resolve('../scripts/' + script)].concat(args)?

Copy link
Author

Choose a reason for hiding this comment

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

i dont follow. are you suggesting just passing script to getArgs(...)? that is, getArgs(script), and move that path resolution line into the other file?

Copy link
Contributor

@Timer Timer Apr 28, 2017

Choose a reason for hiding this comment

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

So we already pass arguments through, just not to Node. I didn't realize the flags had to come first, sorry.

They're passed to the script.

Copy link
Author

Choose a reason for hiding this comment

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

right right right. if desired, we can annotate if you think its fitting somewhere, or rename the fn's to be more communicative

if (result.signal) {
if (result.signal === 'SIGKILL') {
console.log(
Expand Down
1 change: 1 addition & 0 deletions packages/react-scripts/config/jest/babelTransform.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ const babelJest = require('babel-jest');
module.exports = babelJest.createTransformer({
presets: [require.resolve('babel-preset-react-app')],
babelrc: false,
sourceMaps: process.env.REACT_APP_DEBUG_JEST ? 'inline' : false,
});
20 changes: 19 additions & 1 deletion packages/react-scripts/scripts/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,16 @@
* of patent rights can be found in the PATENTS file in the same directory.
*/
// @remove-on-eject-end

/**
* Greetings! If you are here attempting to start a debugging session, please
* ensure that your debugger of choice is configured to enable source maps,
* otherwise your code may appear mangled by babel!
*/
'use strict';

var debugArgs = require('./utils/debugArgs');

process.env.NODE_ENV = 'test';
process.env.PUBLIC_URL = '';

Expand All @@ -27,13 +35,23 @@ process.on('unhandledRejection', err => {
require('dotenv').config({ silent: true });

const jest = require('jest');
const argv = process.argv.slice(2);
let argv = process.argv.slice(2);
const isDebug = !!process.env.REACT_APP_DEBUG_JEST;
const isRunInBand = argv.indexOf('--runInBand') > -1 || argv.indexOf('-i') > -1;

// Watch unless on CI or in coverage mode
if (!process.env.CI && argv.indexOf('--coverage') < 0) {
argv.push('--watch');
}

// Force debug into single worker
if (isDebug) {
if (!isRunInBand) {
argv.push('--runInBand');
}
argv = debugArgs.removeFrom(argv);
}

// @remove-on-eject-begin
// This is not necessary after eject because we embed config into package.json.
const createJestConfig = require('./utils/createJestConfig');
Expand Down
48 changes: 48 additions & 0 deletions packages/react-scripts/scripts/utils/debugArgs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
'use strict';

const DEBUG_FLAGS = [
/^debug$/,
/^--debug$/,
/^--debug-brk(=\d+)?$/,
Copy link
Author

Choose a reason for hiding this comment

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

I believe we shouldn't respect --debug and --debug-brk

are you sure? i'm not convinced we shouldn't support it!

Copy link
Contributor

Choose a reason for hiding this comment

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

They're going away in Node 8 iirc.

/^--inspect$/,
/^--inspect-brk(=\d+)?$/,
];

module.exports = {
_match: function _matchDebugFlags(args, onMatch) {
for (var i in args) {
if (args.hasOwnProperty(i)) {
for (var j in DEBUG_FLAGS) {
if (DEBUG_FLAGS.hasOwnProperty(j)) {
if (args[i].match(DEBUG_FLAGS[j])) {
onMatch(args[i]);
}
}
}
}
}
},
getFrom: function getDebugFlags(args) {
var matches = [];
this._match(args, function addMatch(arg) {
matches.push(arg);
});
return matches.length ? matches : null;
},
removeFrom: function removeDebugFlags(args) {
var matches = this.getFrom(args) || [];
return args.filter(function isNotDebugArg(arg) {
return !matches.some(function isPresent(debugArg) {
return arg === debugArg;
});
});
},
};
25 changes: 25 additions & 0 deletions packages/react-scripts/scripts/utils/getSubscriptArgs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
'use strict';

var debugArgs = require('../utils/debugArgs');

module.exports = function getSubscriptArgs(scriptFilename) {
var args = process.argv.slice(3);
var passedDebugArgs;
var nonDebugArgs;
args.unshift(scriptFilename);
passedDebugArgs = debugArgs.getFrom(args);
if (passedDebugArgs) {
process.env.REACT_APP_DEBUG_JEST = 'true'; // :eyes: side-effect
nonDebugArgs = debugArgs.removeFrom(args);
args = passedDebugArgs.concat(nonDebugArgs);
}
return args;
};