|
9 | 9 | const browserslist = require('browserslist');
|
10 | 10 | const chalk = require('chalk');
|
11 | 11 | const os = require('os');
|
| 12 | +const inquirer = require('inquirer'); |
| 13 | +const pkgUp = require('pkg-up'); |
| 14 | +const fs = require('fs'); |
12 | 15 |
|
13 |
| -function checkBrowsers(dir) { |
14 |
| - const found = browserslist.findConfig(dir); |
| 16 | +const defaultBrowsers = { |
| 17 | + development: ['chrome', 'firefox', 'edge'].map( |
| 18 | + browser => `last 2 ${browser} versions` |
| 19 | + ), |
| 20 | + production: ['>1%', 'last 4 versions', 'Firefox ESR', 'not ie < 11'], |
| 21 | +}; |
15 | 22 |
|
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 | +function checkBrowsers(dir, retry = true) { |
| 24 | + const current = browserslist.findConfig(dir); |
| 25 | + if (current != null) { |
| 26 | + return Promise.resolve(current); |
| 27 | + } |
| 28 | + |
| 29 | + if (!retry) { |
| 30 | + return Promise.reject( |
| 31 | + new Error( |
| 32 | + chalk.red( |
| 33 | + 'As of react-scripts >=2 you must specify targeted browsers.' |
| 34 | + ) + |
| 35 | + os.EOL + |
| 36 | + `Please add a ${chalk.underline( |
| 37 | + 'browserslist' |
| 38 | + )} key to your ${chalk.bold('package.json')}.` |
| 39 | + ) |
23 | 40 | );
|
24 |
| - return null; |
25 | 41 | }
|
26 |
| - return found; |
| 42 | + |
| 43 | + const question = { |
| 44 | + type: 'confirm', |
| 45 | + name: 'shouldSetBrowsers', |
| 46 | + message: |
| 47 | + chalk.yellow("We're unable to detect target browsers.") + |
| 48 | + `\n\nWould you like to add the defaults to your ${chalk.bold( |
| 49 | + 'package.json' |
| 50 | + )}?`, |
| 51 | + default: true, |
| 52 | + }; |
| 53 | + return inquirer.prompt(question).then(answer => { |
| 54 | + if (answer.shouldSetBrowsers) { |
| 55 | + return ( |
| 56 | + pkgUp(dir) |
| 57 | + .then(filePath => { |
| 58 | + if (filePath == null) { |
| 59 | + return Promise.reject(); |
| 60 | + } |
| 61 | + const pkg = JSON.parse(fs.readFileSync(filePath)); |
| 62 | + pkg['browserslist'] = defaultBrowsers; |
| 63 | + fs.writeFileSync(filePath, JSON.stringify(pkg, null, 2) + os.EOL); |
| 64 | + |
| 65 | + browserslist.clearCaches(); |
| 66 | + console.log(); |
| 67 | + console.log(chalk.green('Set target browsers:')); |
| 68 | + console.log(); |
| 69 | + console.log( |
| 70 | + `\t${chalk.bold('Production')}: ${chalk.cyan( |
| 71 | + defaultBrowsers.production.join(', ') |
| 72 | + )}` |
| 73 | + ); |
| 74 | + console.log( |
| 75 | + `\t${chalk.bold('Development')}: ${chalk.cyan( |
| 76 | + defaultBrowsers.development.join(', ') |
| 77 | + )}` |
| 78 | + ); |
| 79 | + console.log(); |
| 80 | + }) |
| 81 | + // Swallow any error |
| 82 | + .catch(() => {}) |
| 83 | + .then(() => checkBrowsers(dir, false)) |
| 84 | + ); |
| 85 | + } else { |
| 86 | + return checkBrowsers(dir, false); |
| 87 | + } |
| 88 | + }); |
27 | 89 | }
|
28 | 90 |
|
29 | 91 | 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 |
| - ); |
| 92 | + return checkBrowsers(dir).then(browsers => { |
| 93 | + if (browsers == null) { |
| 94 | + console.log('Built the bundle with default browser support.'); |
| 95 | + return; |
| 96 | + } |
| 97 | + browsers = browsers[process.env.NODE_ENV] || browsers; |
| 98 | + if (Array.isArray(browsers)) { |
| 99 | + browsers = browsers.join(', '); |
| 100 | + } |
| 101 | + console.log( |
| 102 | + `Built the bundle with browser support for ${chalk.cyan(browsers)}.` |
| 103 | + ); |
| 104 | + }); |
42 | 105 | }
|
43 | 106 |
|
44 |
| -module.exports = { checkBrowsers, printBrowsers }; |
| 107 | +module.exports = { defaultBrowsers, checkBrowsers, printBrowsers }; |
0 commit comments