Skip to content

Commit 5d1710a

Browse files
committed
Tell user what browser support their application was built with (facebook#3782)
* Warn about browsers during build * Better message
1 parent ea46cf4 commit 5d1710a

File tree

6 files changed

+68
-1
lines changed

6 files changed

+68
-1
lines changed
+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* Copyright (c) 2015-present, Facebook, Inc.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
'use strict';
8+
9+
const browserslist = require('browserslist');
10+
const chalk = require('chalk');
11+
const os = require('os');
12+
13+
function checkBrowsers(dir) {
14+
const found = browserslist.findConfig(dir);
15+
16+
if (found == null) {
17+
console.log(
18+
chalk.red('As of react-scripts >=2 you must specify targeted browsers.') +
19+
os.EOL +
20+
`Please add a ${chalk.underline(
21+
'browserslist'
22+
)} key to your ${chalk.bold('package.json')}.`
23+
);
24+
return null;
25+
}
26+
return found;
27+
}
28+
29+
function printBrowsers(dir) {
30+
let browsers = checkBrowsers(dir);
31+
if (browsers == null) {
32+
console.log('Built the bundle with default browser support.');
33+
return;
34+
}
35+
browsers = browsers[process.env.NODE_ENV] || browsers;
36+
if (Array.isArray(browsers)) {
37+
browsers = browsers.join(', ');
38+
}
39+
console.log(
40+
`Built the bundle with browser support for ${chalk.cyan(browsers)}.`
41+
);
42+
}
43+
44+
module.exports = { checkBrowsers, printBrowsers };

packages/react-dev-utils/package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"node": ">=6"
1212
},
1313
"files": [
14+
"browsersHelper.js",
1415
"checkRequiredFiles.js",
1516
"clearConsole.js",
1617
"crashOverlay.js",
@@ -36,8 +37,9 @@
3637
"webpackHotDevClient.js"
3738
],
3839
"dependencies": {
39-
"address": "1.0.3",
4040
"@babel/code-frame": "7.0.0-beta.37",
41+
"address": "1.0.3",
42+
"browserslist": "2.11.1",
4143
"chalk": "2.3.0",
4244
"cross-spawn": "5.1.0",
4345
"detect-port-alt": "1.1.5",

packages/react-scripts/config/paths.js

+1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ function getServedPath(appPackageJson) {
4949
// config after eject: we're in ./config/
5050
module.exports = {
5151
dotenv: resolveApp('.env'),
52+
appPath: resolveApp('.'),
5253
appBuild: resolveApp('build'),
5354
appPublic: resolveApp('public'),
5455
appHtml: resolveApp('public/index.html'),

packages/react-scripts/package.json

+4
Original file line numberDiff line numberDiff line change
@@ -68,5 +68,9 @@
6868
},
6969
"optionalDependencies": {
7070
"fsevents": "1.1.2"
71+
},
72+
"browserslist": {
73+
"development": "last 2 chrome versions",
74+
"production": [">1%", "last 4 versions", "Firefox ESR", "not ie < 11"]
7175
}
7276
}

packages/react-scripts/scripts/build.js

+9
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,14 @@ const formatWebpackMessages = require('react-dev-utils/formatWebpackMessages');
4040
const printHostingInstructions = require('react-dev-utils/printHostingInstructions');
4141
const FileSizeReporter = require('react-dev-utils/FileSizeReporter');
4242
const printBuildError = require('react-dev-utils/printBuildError');
43+
const { printBrowsers } = require('react-dev-utils/browsersHelper');
44+
// @remove-on-eject-begin
45+
// Require browsers to be specified before you eject
46+
const { checkBrowsers } = require('react-dev-utils/browsersHelper');
47+
if (!checkBrowsers(paths.appPath)) {
48+
process.exit(1);
49+
}
50+
// @remove-on-eject-end
4351

4452
const measureFileSizesBeforeBuild =
4553
FileSizeReporter.measureFileSizesBeforeBuild;
@@ -107,6 +115,7 @@ measureFileSizesBeforeBuild(paths.appBuild)
107115
buildFolder,
108116
useYarn
109117
);
118+
printBrowsers(paths.appPath);
110119
},
111120
err => {
112121
console.log(chalk.red('Failed to compile.\n'));

packages/react-scripts/scripts/start.js

+7
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,13 @@ const createDevServerConfig = require('../config/webpackDevServer.config');
4848

4949
const useYarn = fs.existsSync(paths.yarnLockFile);
5050
const isInteractive = process.stdout.isTTY;
51+
// @remove-on-eject-begin
52+
// Require browsers to be specified before you eject
53+
const { checkBrowsers } = require('react-dev-utils/browsersHelper');
54+
if (!checkBrowsers(paths.appPath)) {
55+
process.exit(1);
56+
}
57+
// @remove-on-eject-end
5158

5259
// Warn and crash if required files are missing
5360
if (!checkRequiredFiles([paths.appHtml, paths.appIndexJs])) {

0 commit comments

Comments
 (0)