Skip to content

Commit 73c8729

Browse files
committed
🎨 add first class debugging support
1 parent bed9a39 commit 73c8729

File tree

5 files changed

+98
-7
lines changed

5 files changed

+98
-7
lines changed

packages/react-scripts/bin/react-scripts.js

+5-6
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,17 @@
1212

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

1717
switch (script) {
1818
case 'build':
1919
case 'eject':
2020
case 'start':
2121
case 'test':
22-
var result = spawn.sync(
23-
'node',
24-
[require.resolve('../scripts/' + script)].concat(args),
25-
{ stdio: 'inherit' }
26-
);
22+
var scriptFilename = require.resolve('../scripts/' + script);
23+
var result = spawn.sync('node', getArgs(scriptFilename), {
24+
stdio: 'inherit',
25+
});
2726
if (result.signal) {
2827
if (result.signal === 'SIGKILL') {
2928
console.log(

packages/react-scripts/config/jest/babelTransform.js

+1
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,5 @@ const babelJest = require('babel-jest');
1313
module.exports = babelJest.createTransformer({
1414
presets: [require.resolve('babel-preset-react-app')],
1515
babelrc: false,
16+
sourceMaps: process.env.REACT_APP_DEBUG_JEST ? 'inline' : false,
1617
});

packages/react-scripts/scripts/test.js

+19-1
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,16 @@
88
* of patent rights can be found in the PATENTS file in the same directory.
99
*/
1010
// @remove-on-eject-end
11+
12+
/**
13+
* Greetings! If you are here attempting to start a debugging session, please
14+
* ensure that your debugger of choice is configured to enable source maps,
15+
* otherwise your code may appear mangled by babel!
16+
*/
1117
'use strict';
1218

19+
var debugArgs = require('./utils/debugArgs');
20+
1321
process.env.NODE_ENV = 'test';
1422
process.env.PUBLIC_URL = '';
1523

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

2937
const jest = require('jest');
30-
const argv = process.argv.slice(2);
38+
let argv = process.argv.slice(2);
39+
const isDebug = !!process.env.REACT_APP_DEBUG_JEST;
40+
const isRunInBand = argv.indexOf('--runInBand') > -1 || argv.indexOf('-i') > -1;
3141

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

47+
// Force debug into single worker
48+
if (isDebug) {
49+
if (!isRunInBand) {
50+
argv.push('--runInBand');
51+
}
52+
argv = debugArgs.removeFrom(argv);
53+
}
54+
3755
// @remove-on-eject-begin
3856
// This is not necessary after eject because we embed config into package.json.
3957
const createJestConfig = require('./utils/createJestConfig');
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* Copyright (c) 2015-present, Facebook, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*/
9+
'use strict';
10+
11+
const DEBUG_FLAGS = [
12+
/^debug$/,
13+
/^--debug$/,
14+
/^--debug-brk(=\d+)?$/,
15+
/^--inspect$/,
16+
/^--inspect-brk(=\d+)?$/,
17+
];
18+
19+
module.exports = {
20+
_match: function _matchDebugFlags(args, onMatch) {
21+
for (var i in args) {
22+
if (args.hasOwnProperty(i)) {
23+
for (var j in DEBUG_FLAGS) {
24+
if (DEBUG_FLAGS.hasOwnProperty(j)) {
25+
if (args[i].match(DEBUG_FLAGS[j])) {
26+
onMatch(args[i]);
27+
}
28+
}
29+
}
30+
}
31+
}
32+
},
33+
getFrom: function getDebugFlags(args) {
34+
var matches = [];
35+
this._match(args, function addMatch(arg) {
36+
matches.push(arg);
37+
});
38+
return matches.length ? matches : null;
39+
},
40+
removeFrom: function removeDebugFlags(args) {
41+
var matches = this.getFrom(args) || [];
42+
return args.filter(function isNotDebugArg(arg) {
43+
return !matches.some(function isPresent(debugArg) {
44+
return arg === debugArg;
45+
});
46+
});
47+
},
48+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* Copyright (c) 2015-present, Facebook, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*/
9+
'use strict';
10+
11+
var debugArgs = require('../utils/debugArgs');
12+
13+
module.exports = function getSubscriptArgs(scriptFilename) {
14+
var args = process.argv.slice(3);
15+
var passedDebugArgs;
16+
var nonDebugArgs;
17+
args.unshift(scriptFilename);
18+
passedDebugArgs = debugArgs.getFrom(args);
19+
if (passedDebugArgs) {
20+
process.env.REACT_APP_DEBUG_JEST = 'true'; // :eyes: side-effect
21+
nonDebugArgs = debugArgs.removeFrom(args);
22+
args = passedDebugArgs.concat(nonDebugArgs);
23+
}
24+
return args;
25+
};

0 commit comments

Comments
 (0)