Skip to content

Commit e08b789

Browse files
viankakrisnaJohnNilsson
authored andcommittedSep 9, 2017
Format UglifyJs error (#2650)
* format UglifyJs error * move formatBuildError to react-dev-utils * fix readme * use regex for plucking the path from stack * make path human readable and fallback to show error if regex not matched * rename to printBuildError and add link to the docs * fix link indentation * improve readibility + shorten link
1 parent adb4c6d commit e08b789

File tree

5 files changed

+79
-2
lines changed

5 files changed

+79
-2
lines changed
 

‎packages/react-dev-utils/README.md

+14
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,20 @@ compiler.plugin('done', function(stats) {
220220
});
221221
```
222222

223+
#### `printBuildError(error: Object): void`
224+
225+
Prettify some known build errors.
226+
Pass an Error object to log a prettified error message in the console.
227+
228+
```
229+
const printBuildError = require('react-dev-utils/printBuildError')
230+
try {
231+
build()
232+
} catch(e) {
233+
printBuildError(e) // logs prettified message
234+
}
235+
```
236+
223237
#### `getProcessForPort(port: number): string`
224238

225239
Finds the currently running process on `port`.

‎packages/react-dev-utils/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,14 @@
1818
"crossSpawn.js",
1919
"eslintFormatter.js",
2020
"FileSizeReporter.js",
21+
"printBuildError.js",
2122
"formatWebpackMessages.js",
2223
"getProcessForPort.js",
2324
"inquirer.js",
2425
"InterpolateHtmlPlugin.js",
2526
"launchEditor.js",
26-
"noopServiceWorkerMiddleware.js",
2727
"ModuleScopePlugin.js",
28+
"noopServiceWorkerMiddleware.js",
2829
"openBrowser.js",
2930
"openChrome.applescript",
3031
"printHostingInstructions.js",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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+
10+
'use strict';
11+
12+
const get = require('lodash/get');
13+
const chalk = require('chalk');
14+
15+
module.exports = function printBuildError(err) {
16+
const message = get(err, 'message');
17+
const stack = get(err, 'stack');
18+
19+
// Add more helpful message for UglifyJs error
20+
if (
21+
stack &&
22+
typeof message === 'string' &&
23+
message.indexOf('from UglifyJs') !== -1
24+
) {
25+
try {
26+
const matched = /Unexpected token:(.+)\[(.+)\:(.+)\,(.+)\]\[.+\]/.exec(
27+
stack
28+
);
29+
if (!matched) {
30+
throw new Error(
31+
"The regex pattern is not matched. Maybe UglifyJs changed it's message?"
32+
);
33+
}
34+
const problemPath = matched[2];
35+
const line = matched[3];
36+
const column = matched[4];
37+
console.log(
38+
'Failed to minify the code from this file: \n\n',
39+
chalk.yellow(`${problemPath} line ${line}:${column}`),
40+
'\n'
41+
);
42+
} catch (ignored) {
43+
console.log('Failed to minify the code.', err);
44+
}
45+
console.log('Read more here: http://bit.ly/2tRViJ9');
46+
} else {
47+
console.log((message || err) + '\n');
48+
}
49+
console.log();
50+
};

‎packages/react-scripts/scripts/build.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ const checkRequiredFiles = require('react-dev-utils/checkRequiredFiles');
3434
const formatWebpackMessages = require('react-dev-utils/formatWebpackMessages');
3535
const printHostingInstructions = require('react-dev-utils/printHostingInstructions');
3636
const FileSizeReporter = require('react-dev-utils/FileSizeReporter');
37+
const printBuildError = require('react-dev-utils/printBuildError');
3738

3839
const measureFileSizesBeforeBuild =
3940
FileSizeReporter.measureFileSizesBeforeBuild;
@@ -104,7 +105,7 @@ measureFileSizesBeforeBuild(paths.appBuild)
104105
},
105106
err => {
106107
console.log(chalk.red('Failed to compile.\n'));
107-
console.log((err.message || err) + '\n');
108+
printBuildError(err);
108109
process.exit(1);
109110
}
110111
);

‎packages/react-scripts/template/README.md

+11
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ You can find the most recent version of this guide [here](https://github.com/fac
9292
- [`npm test` hangs on macOS Sierra](#npm-test-hangs-on-macos-sierra)
9393
- [`npm run build` exits too early](#npm-run-build-exits-too-early)
9494
- [`npm run build` fails on Heroku](#npm-run-build-fails-on-heroku)
95+
- [`npm run build` fails to minify](#npm-run-build-fails-to-minify)
9596
- [Moment.js locales are missing](#momentjs-locales-are-missing)
9697
- [Something Missing?](#something-missing)
9798

@@ -2137,6 +2138,16 @@ moment.locale('fr');
21372138

21382139
This will only work for locales that have been explicitly imported before.
21392140

2141+
### `npm run build` fails to minify
2142+
2143+
You may occasionally find a package you depend on needs compiled or ships code for a non-browser environment.<br>
2144+
This is considered poor practice in the ecosystem and does not have an escape hatch in Create React App.<br>
2145+
<br>
2146+
To resolve this:
2147+
1. Open an issue on the dependency's issue tracker and ask that the package be published pre-compiled (retaining ES6 Modules).
2148+
2. Fork the package and publish a corrected version yourself.
2149+
3. If the dependency is small enough, copy it to your `src/` folder and treat it as application code.
2150+
21402151
## Something Missing?
21412152
21422153
If you have ideas for more “How To” recipes that should be on this page, [let us know](https://github.com/facebookincubator/create-react-app/issues) or [contribute some!](https://github.com/facebookincubator/create-react-app/edit/master/packages/react-scripts/template/README.md)

0 commit comments

Comments
 (0)