1 var path = require('path')
4 var BrowserslistError = require('./error')
6 var IS_SECTION = /^\s*\[(.+)\]\s*$/
7 var CONFIG_PATTERN = /^browserslist-config-/
8 var SCOPED_CONFIG__PATTERN = /@[^./]+\/browserslist-config(-|$)/
10 var filenessCache = { }
13 function checkExtend (name) {
14 var use = ' Use `dangerousExtend` option to disable.'
15 if (!CONFIG_PATTERN.test(name) && !SCOPED_CONFIG__PATTERN.test(name)) {
16 throw new BrowserslistError(
17 'Browserslist config needs `browserslist-config-` prefix. ' + use)
19 if (name.indexOf('.') !== -1) {
20 throw new BrowserslistError(
21 '`.` not allowed in Browserslist config name. ' + use)
23 if (name.indexOf('node_modules') !== -1) {
24 throw new BrowserslistError(
25 '`node_modules` not allowed in Browserslist config.' + use)
29 function isFile (file) {
30 if (file in filenessCache) {
31 return filenessCache[file]
33 var result = fs.existsSync(file) && fs.statSync(file).isFile()
34 if (!process.env.BROWSERSLIST_DISABLE_CACHE) {
35 filenessCache[file] = result
40 function eachParent (file, callback) {
41 var loc = path.resolve(file)
43 var result = callback(loc)
44 if (typeof result !== 'undefined') return result
45 } while (loc !== (loc = path.dirname(loc)))
49 function pickEnv (config, opts) {
50 if (typeof config !== 'object') return config
53 if (typeof opts.env === 'string') {
55 } else if (process.env.BROWSERSLIST_ENV) {
56 name = process.env.BROWSERSLIST_ENV
57 } else if (process.env.NODE_ENV) {
58 name = process.env.NODE_ENV
63 return config[name] || config.defaults
66 function parsePackage (file) {
67 var config = JSON.parse(fs.readFileSync(file))
68 if (config.browserlist && !config.browserslist) {
69 throw new BrowserslistError(
70 '`browserlist` key instead of `browserslist` in ' + file)
72 var list = config.browserslist
73 if (typeof list === 'object' && list.length) {
74 list = { defaults: list }
80 loadQueries: function loadQueries (context, name) {
81 if (!context.dangerousExtend) checkExtend(name)
82 // eslint-disable-next-line security/detect-non-literal-require
83 var queries = require(name)
84 if (!Array.isArray(queries)) {
85 throw new BrowserslistError(
86 '`' + name + '` config exports not an array of queries')
91 getStat: function getStat (opts) {
95 } else if (process.env.BROWSERSLIST_STATS) {
96 stats = process.env.BROWSERSLIST_STATS
97 } else if (opts.path && path.resolve && fs.existsSync) {
98 stats = eachParent(opts.path, function (dir) {
99 var file = path.join(dir, 'browserslist-stats.json')
100 return isFile(file) ? file : undefined
104 if (typeof stats === 'string') {
106 stats = JSON.parse(fs.readFileSync(stats))
108 throw new BrowserslistError('Can\'t read ' + stats)
115 loadConfig: function loadConfig (opts) {
116 if (process.env.BROWSERSLIST) {
117 return process.env.BROWSERSLIST
118 } else if (opts.config || process.env.BROWSERSLIST_CONFIG) {
119 var file = opts.config || process.env.BROWSERSLIST_CONFIG
120 if (path.basename(file) === 'package.json') {
121 return pickEnv(parsePackage(file), opts)
123 return pickEnv(module.exports.readConfig(file), opts)
125 } else if (opts.path) {
126 return pickEnv(module.exports.findConfig(opts.path), opts)
132 parseConfig: function parseConfig (string) {
133 var result = { defaults: [] }
134 var section = 'defaults'
137 .replace(/#[^\n]*/g, '')
139 .map(function (line) {
142 .filter(function (line) {
145 .forEach(function (line) {
146 if (IS_SECTION.test(line)) {
147 section = line.match(IS_SECTION)[1].trim()
148 result[section] = result[section] || []
150 result[section].push(line)
157 readConfig: function readConfig (file) {
159 throw new BrowserslistError('Can\'t read ' + file + ' config')
161 return module.exports.parseConfig(fs.readFileSync(file))
164 findConfig: function findConfig (from) {
165 from = path.resolve(from)
167 var cacheKey = isFile(from) ? path.dirname(from) : from
168 if (cacheKey in configCache) {
169 return configCache[cacheKey]
172 var resolved = eachParent(from, function (dir) {
173 var config = path.join(dir, 'browserslist')
174 var pkg = path.join(dir, 'package.json')
175 var rc = path.join(dir, '.browserslistrc')
180 pkgBrowserslist = parsePackage(pkg)
182 if (e.name === 'BrowserslistError') throw e
184 '[Browserslist] Could not parse ' + pkg + '. Ignoring it.')
188 if (isFile(config) && pkgBrowserslist) {
189 throw new BrowserslistError(
190 dir + ' contains both browserslist and package.json with browsers')
191 } else if (isFile(rc) && pkgBrowserslist) {
192 throw new BrowserslistError(
193 dir + ' contains both .browserslistrc and package.json with browsers')
194 } else if (isFile(config) && isFile(rc)) {
195 throw new BrowserslistError(
196 dir + ' contains both .browserslistrc and browserslist')
197 } else if (isFile(config)) {
198 return module.exports.readConfig(config)
199 } else if (isFile(rc)) {
200 return module.exports.readConfig(rc)
202 return pkgBrowserslist
205 if (!process.env.BROWSERSLIST_DISABLE_CACHE) {
206 configCache[cacheKey] = resolved
211 clearCaches: function clearCaches () {