|
1 | 1 | const helpers = require('./helpers') |
| 2 | +const fs = require('fs') |
2 | 3 |
|
3 | 4 | function addDependencies (api) { |
4 | 5 | api.extendPackage({ |
@@ -33,24 +34,46 @@ function updateBabelConfig (api) { |
33 | 34 | } |
34 | 35 |
|
35 | 36 | function updateBrowsersList (api) { |
36 | | - helpers.updateFile(api, './.browserslistrc', lines => { |
37 | | - if (!lines.length) { |
38 | | - return [ |
39 | | - '> 1%', |
40 | | - 'last 2 versions', |
41 | | - 'not ie <= 10', |
42 | | - ] |
43 | | - } |
| 37 | + const pkgPath = api.resolve('./package.json') |
| 38 | + const pkg = JSON.parse(fs.readFileSync(pkgPath, { encoding: 'utf8' })) |
| 39 | + const isInPackage = !!pkg.browserslist |
| 40 | + |
| 41 | + const findIeIndex = lines => lines.findIndex(line => line.match(/^([^\s]*\s+|)ie\s*</)) |
| 42 | + |
| 43 | + if (isInPackage) { |
| 44 | + const ieLineIndex = findIeIndex(pkg.browserslist) |
44 | 45 |
|
45 | | - const ieLineIndex = lines.findIndex(line => line.match(/^([^\s]*\s+|)ie\s*</)) |
46 | 46 | if (ieLineIndex === -1) { |
47 | | - lines.push('not ie <= 10') |
| 47 | + pkg.browserslist.push('not ie <= 10') |
48 | 48 | } else { |
49 | | - lines[ieLineIndex] = 'not ie <= 10' |
| 49 | + pkg.browserslist[ieLineIndex] = 'not ie <= 10' |
50 | 50 | } |
51 | 51 |
|
52 | | - return lines |
53 | | - }) |
| 52 | + fs.writeFileSync( |
| 53 | + pkgPath, |
| 54 | + JSON.stringify(pkg, null, 2), |
| 55 | + { encoding: 'utf8' } |
| 56 | + ) |
| 57 | + } else { |
| 58 | + helpers.updateFile(api, './.browserslistrc', lines => { |
| 59 | + if (!lines.length) { |
| 60 | + return [ |
| 61 | + '> 1%', |
| 62 | + 'last 2 versions', |
| 63 | + 'not ie <= 10', |
| 64 | + ] |
| 65 | + } |
| 66 | + |
| 67 | + const ieLineIndex = findIeIndex(lines) |
| 68 | + if (ieLineIndex === -1) { |
| 69 | + lines.push('not ie <= 10') |
| 70 | + } else { |
| 71 | + lines[ieLineIndex] = 'not ie <= 10' |
| 72 | + } |
| 73 | + |
| 74 | + return lines |
| 75 | + }) |
| 76 | + } |
54 | 77 | } |
55 | 78 |
|
56 | 79 | function addImports (api) { |
|
0 commit comments