Skip to content
This repository was archived by the owner on May 8, 2025. It is now read-only.

Commit 673c767

Browse files
committed
fix(polyfill): check for existing package browserslist
1 parent 18fe12e commit 673c767

File tree

1 file changed

+36
-13
lines changed

1 file changed

+36
-13
lines changed

generator/tools/polyfill.js

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const helpers = require('./helpers')
2+
const fs = require('fs')
23

34
function addDependencies (api) {
45
api.extendPackage({
@@ -33,24 +34,46 @@ function updateBabelConfig (api) {
3334
}
3435

3536
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)
4445

45-
const ieLineIndex = lines.findIndex(line => line.match(/^([^\s]*\s+|)ie\s*</))
4646
if (ieLineIndex === -1) {
47-
lines.push('not ie <= 10')
47+
pkg.browserslist.push('not ie <= 10')
4848
} else {
49-
lines[ieLineIndex] = 'not ie <= 10'
49+
pkg.browserslist[ieLineIndex] = 'not ie <= 10'
5050
}
5151

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+
}
5477
}
5578

5679
function addImports (api) {

0 commit comments

Comments
 (0)