Skip to content

Commit

Permalink
Tell user what browser support their application was built with (#3782)
Browse files Browse the repository at this point in the history
* Warn about browsers during build

* Better message
  • Loading branch information
Timer authored and gaearon committed Jan 14, 2018
1 parent d0bf090 commit a0a8451
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 1 deletion.
44 changes: 44 additions & 0 deletions packages/react-dev-utils/browsersHelper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';

const browserslist = require('browserslist');
const chalk = require('chalk');
const os = require('os');

function checkBrowsers(dir) {
const found = browserslist.findConfig(dir);

if (found == null) {
console.log(
chalk.red('As of react-scripts >=2 you must specify targeted browsers.') +
os.EOL +
`Please add a ${chalk.underline(
'browserslist'
)} key to your ${chalk.bold('package.json')}.`
);
return null;
}
return found;
}

function printBrowsers(dir) {
let browsers = checkBrowsers(dir);
if (browsers == null) {
console.log('Built the bundle with default browser support.');
return;
}
browsers = browsers[process.env.NODE_ENV] || browsers;
if (Array.isArray(browsers)) {
browsers = browsers.join(', ');
}
console.log(
`Built the bundle with browser support for ${chalk.cyan(browsers)}.`
);
}

module.exports = { checkBrowsers, printBrowsers };
4 changes: 3 additions & 1 deletion packages/react-dev-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"node": ">=6"
},
"files": [
"browsersHelper.js",
"checkRequiredFiles.js",
"clearConsole.js",
"crashOverlay.js",
Expand All @@ -36,8 +37,9 @@
"webpackHotDevClient.js"
],
"dependencies": {
"address": "1.0.3",
"@babel/code-frame": "7.0.0-beta.37",
"address": "1.0.3",
"browserslist": "2.11.1",
"chalk": "2.3.0",
"cross-spawn": "5.1.0",
"detect-port-alt": "1.1.5",
Expand Down
1 change: 1 addition & 0 deletions packages/react-scripts/config/paths.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ function getServedPath(appPackageJson) {
// config after eject: we're in ./config/
module.exports = {
dotenv: resolveApp('.env'),
appPath: resolveApp('.'),
appBuild: resolveApp('build'),
appPublic: resolveApp('public'),
appHtml: resolveApp('public/index.html'),
Expand Down
4 changes: 4 additions & 0 deletions packages/react-scripts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,5 +68,9 @@
},
"optionalDependencies": {
"fsevents": "1.1.2"
},
"browserslist": {
"development": "last 2 chrome versions",
"production": [">1%", "last 4 versions", "Firefox ESR", "not ie < 11"]
}
}
9 changes: 9 additions & 0 deletions packages/react-scripts/scripts/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ const formatWebpackMessages = require('react-dev-utils/formatWebpackMessages');
const printHostingInstructions = require('react-dev-utils/printHostingInstructions');
const FileSizeReporter = require('react-dev-utils/FileSizeReporter');
const printBuildError = require('react-dev-utils/printBuildError');
const { printBrowsers } = require('react-dev-utils/browsersHelper');
// @remove-on-eject-begin
// Require browsers to be specified before you eject
const { checkBrowsers } = require('react-dev-utils/browsersHelper');
if (!checkBrowsers(paths.appPath)) {
process.exit(1);
}
// @remove-on-eject-end

const measureFileSizesBeforeBuild =
FileSizeReporter.measureFileSizesBeforeBuild;
Expand Down Expand Up @@ -107,6 +115,7 @@ measureFileSizesBeforeBuild(paths.appBuild)
buildFolder,
useYarn
);
printBrowsers(paths.appPath);
},
err => {
console.log(chalk.red('Failed to compile.\n'));
Expand Down
7 changes: 7 additions & 0 deletions packages/react-scripts/scripts/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ const createDevServerConfig = require('../config/webpackDevServer.config');

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

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

0 comments on commit a0a8451

Please sign in to comment.