-
Notifications
You must be signed in to change notification settings - Fork 29
/
browsers.js
90 lines (83 loc) Β· 3.26 KB
/
browsers.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
const chalk = require('picocolors')
const browserslist = require('browserslist')
const browserslistUseragentRegexp = require('browserslist-useragent-regexp')
// These are browserslist defaults
// + browsers supporting es modules according to babel-preset-env
// - browsers that don't support async/await properly
//
// The point here is to have an evergreen, modern browser support
// but eagerly exclude all the browsers that are still quite new
// but don't support async/await fully. This avoids transpiling
// async/await. Eventually this can be simplified back to browserslist
// default, but for now this is what it takes.
//
// This approach is an alternative to module/nomodule approach.
// While module/nomodule approach is a specific cut in time, where
// certain browsers started supporting es modules, that cut will remain
// static unless new specs for versioning script tags is introduced.
//
// As browsers continue adding new features, this browserslist will
// continue polyfilling less and less. We therefore need to do
// user agent parsing and serve the appropriate html file based
// on this very browserslist.
const jetpackDefaultBrowserslist = {
modern: [
'> 0.5% and last 2 versions',
'Firefox ESR',
'not dead',
'not edge < 16',
'not firefox < 60',
'not chrome < 61',
'not safari < 12',
'not opera < 48',
'not ios_saf < 11.4',
'not and_chr < 71',
'not and_ff < 64',
'not ie <= 11'
].join(', '),
legacy: undefined
}
function browsers (options) {
if (options.target.modern) {
const modern = query({ modern: true })
const modernBrowsers = browserslist(modern)
console.log(chalk.yellow('[modern query]'))
console.log((Array.isArray(modern) ? modern : modern.split(', ')).join('\n'))
console.log('')
console.log(chalk.yellow('[modern browsers]'))
console.log(modernBrowsers.join('\n'))
console.log('')
console.log(chalk.yellow(`[modern coverage ${options.coverage || 'globally'}]`))
console.log(browserslist.coverage(modernBrowsers, options.coverage || undefined).toFixed(2) + '%')
console.log('')
}
if (options.target.legacy) {
const legacy = query({ modern: false }) || browserslist.defaults
const legacyBrowsers = browserslist(legacy)
console.log(chalk.yellow('[legacy query]'))
console.log((Array.isArray(legacy) ? legacy : legacy.split(', ')).join('\n'))
console.log('')
console.log(chalk.yellow('[legacy browsers]'))
console.log(legacyBrowsers.join('\n'))
console.log('')
console.log(chalk.yellow(`[legacy coverage ${options.coverage || 'globally'}]`))
console.log(browserslist.coverage(legacyBrowsers, options.coverage || undefined).toFixed(2) + '%')
console.log('')
}
}
function query (options) {
const browserslistEnv = options.modern ? 'modern' : 'legacy'
let browsers = browserslist.loadConfig({ env: browserslistEnv, path: '.' })
browsers = browsers && browsers.length ? browsers : jetpackDefaultBrowserslist[browserslistEnv]
return browsers
}
function regexp (options) {
return browserslistUseragentRegexp.getUserAgentRegExp({
browsers: query(options),
allowHigherVersions: true
})
}
module.exports = browsers
module.exports.query = query
module.exports.browserslist = jetpackDefaultBrowserslist
module.exports.regexp = regexp