123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. var path = require('path')
  2. var fs = require('fs')
  3. var BrowserslistError = require('./error')
  4. var IS_SECTION = /^\s*\[(.+)\]\s*$/
  5. var CONFIG_PATTERN = /^browserslist-config-/
  6. var SCOPED_CONFIG__PATTERN = /@[^./]+\/browserslist-config(-|$)/
  7. var filenessCache = { }
  8. var configCache = { }
  9. function checkExtend (name) {
  10. var use = ' Use `dangerousExtend` option to disable.'
  11. if (!CONFIG_PATTERN.test(name) && !SCOPED_CONFIG__PATTERN.test(name)) {
  12. throw new BrowserslistError(
  13. 'Browserslist config needs `browserslist-config-` prefix. ' + use)
  14. }
  15. if (name.indexOf('.') !== -1) {
  16. throw new BrowserslistError(
  17. '`.` not allowed in Browserslist config name. ' + use)
  18. }
  19. if (name.indexOf('node_modules') !== -1) {
  20. throw new BrowserslistError(
  21. '`node_modules` not allowed in Browserslist config.' + use)
  22. }
  23. }
  24. function isFile (file) {
  25. if (file in filenessCache) {
  26. return filenessCache[file]
  27. }
  28. var result = fs.existsSync(file) && fs.statSync(file).isFile()
  29. if (!process.env.BROWSERSLIST_DISABLE_CACHE) {
  30. filenessCache[file] = result
  31. }
  32. return result
  33. }
  34. function eachParent (file, callback) {
  35. var loc = path.resolve(file)
  36. do {
  37. var result = callback(loc)
  38. if (typeof result !== 'undefined') return result
  39. } while (loc !== (loc = path.dirname(loc)))
  40. return undefined
  41. }
  42. function pickEnv (config, opts) {
  43. if (typeof config !== 'object') return config
  44. var name
  45. if (typeof opts.env === 'string') {
  46. name = opts.env
  47. } else if (process.env.BROWSERSLIST_ENV) {
  48. name = process.env.BROWSERSLIST_ENV
  49. } else if (process.env.NODE_ENV) {
  50. name = process.env.NODE_ENV
  51. } else {
  52. name = 'development'
  53. }
  54. return config[name] || config.defaults
  55. }
  56. function parsePackage (file) {
  57. var config = JSON.parse(fs.readFileSync(file))
  58. if (config.browserlist && !config.browserslist) {
  59. throw new BrowserslistError(
  60. '`browserlist` key instead of `browserslist` in ' + file)
  61. }
  62. var list = config.browserslist
  63. if (typeof list === 'object' && list.length) {
  64. list = { defaults: list }
  65. }
  66. return list
  67. }
  68. module.exports = {
  69. loadQueries: function loadQueries (context, name) {
  70. if (!context.dangerousExtend) checkExtend(name)
  71. // eslint-disable-next-line security/detect-non-literal-require
  72. var queries = require(name)
  73. if (!Array.isArray(queries)) {
  74. throw new BrowserslistError(
  75. '`' + name + '` config exports not an array of queries')
  76. }
  77. return queries
  78. },
  79. getStat: function getStat (opts) {
  80. var stats
  81. if (opts.stats) {
  82. stats = opts.stats
  83. } else if (process.env.BROWSERSLIST_STATS) {
  84. stats = process.env.BROWSERSLIST_STATS
  85. } else if (opts.path && path.resolve && fs.existsSync) {
  86. stats = eachParent(opts.path, function (dir) {
  87. var file = path.join(dir, 'browserslist-stats.json')
  88. return isFile(file) ? file : undefined
  89. })
  90. }
  91. if (typeof stats === 'string') {
  92. try {
  93. stats = JSON.parse(fs.readFileSync(stats))
  94. } catch (e) {
  95. throw new BrowserslistError('Can\'t read ' + stats)
  96. }
  97. }
  98. return stats
  99. },
  100. loadConfig: function loadConfig (opts) {
  101. if (process.env.BROWSERSLIST) {
  102. return process.env.BROWSERSLIST
  103. } else if (opts.config || process.env.BROWSERSLIST_CONFIG) {
  104. var file = opts.config || process.env.BROWSERSLIST_CONFIG
  105. if (path.basename(file) === 'package.json') {
  106. return pickEnv(parsePackage(file), opts)
  107. } else {
  108. return pickEnv(module.exports.readConfig(file), opts)
  109. }
  110. } else if (opts.path) {
  111. return pickEnv(module.exports.findConfig(opts.path), opts)
  112. } else {
  113. return undefined
  114. }
  115. },
  116. parseConfig: function parseConfig (string) {
  117. var result = { defaults: [] }
  118. var section = 'defaults'
  119. string.toString()
  120. .replace(/#[^\n]*/g, '')
  121. .split(/\n/)
  122. .map(function (line) {
  123. return line.trim()
  124. })
  125. .filter(function (line) {
  126. return line !== ''
  127. })
  128. .forEach(function (line) {
  129. if (IS_SECTION.test(line)) {
  130. section = line.match(IS_SECTION)[1].trim()
  131. result[section] = result[section] || []
  132. } else {
  133. result[section].push(line)
  134. }
  135. })
  136. return result
  137. },
  138. readConfig: function readConfig (file) {
  139. if (!isFile(file)) {
  140. throw new BrowserslistError('Can\'t read ' + file + ' config')
  141. }
  142. return module.exports.parseConfig(fs.readFileSync(file))
  143. },
  144. findConfig: function findConfig (from) {
  145. from = path.resolve(from)
  146. var cacheKey = isFile(from) ? path.dirname(from) : from
  147. if (cacheKey in configCache) {
  148. return configCache[cacheKey]
  149. }
  150. var resolved = eachParent(from, function (dir) {
  151. var config = path.join(dir, 'browserslist')
  152. var pkg = path.join(dir, 'package.json')
  153. var rc = path.join(dir, '.browserslistrc')
  154. var pkgBrowserslist
  155. if (isFile(pkg)) {
  156. try {
  157. pkgBrowserslist = parsePackage(pkg)
  158. } catch (e) {
  159. if (e.name === 'BrowserslistError') throw e
  160. console.warn(
  161. '[Browserslist] Could not parse ' + pkg + '. Ignoring it.')
  162. }
  163. }
  164. if (isFile(config) && pkgBrowserslist) {
  165. throw new BrowserslistError(
  166. dir + ' contains both browserslist and package.json with browsers')
  167. } else if (isFile(rc) && pkgBrowserslist) {
  168. throw new BrowserslistError(
  169. dir + ' contains both .browserslistrc and package.json with browsers')
  170. } else if (isFile(config) && isFile(rc)) {
  171. throw new BrowserslistError(
  172. dir + ' contains both .browserslistrc and browserslist')
  173. } else if (isFile(config)) {
  174. return module.exports.readConfig(config)
  175. } else if (isFile(rc)) {
  176. return module.exports.readConfig(rc)
  177. } else {
  178. return pkgBrowserslist
  179. }
  180. })
  181. if (!process.env.BROWSERSLIST_DISABLE_CACHE) {
  182. configCache[cacheKey] = resolved
  183. }
  184. return resolved
  185. },
  186. clearCaches: function clearCaches () {
  187. filenessCache = { }
  188. configCache = { }
  189. }
  190. }