diff --git a/README.md b/README.md index 65ae4440..4d5d0e01 100644 --- a/README.md +++ b/README.md @@ -34,7 +34,8 @@ Browserslist will use browsers criteria from: 1. First argument. 2. `BROWSERSLIST` environment variable. 3. `browserslist` config file in current or parent directories. -4. If the above methods did not produce a valid result, Browserslist will use defaults: +4. `browserslist` key in `package.json` file in current or parent directories. +5. If the above methods did not produce a valid result, Browserslist will use defaults: `> 1%, last 2 versions, Firefox ESR`. Multiple criteria are combined as a boolean `OR`. A browser version must match diff --git a/index.js b/index.js index e1e16573..2fa58318 100644 --- a/index.js +++ b/index.js @@ -239,12 +239,33 @@ browserslist.readConfig = function (from) { if ( typeof from === 'undefined' ) from = '.'; var dirs = path.resolve(from).split(path.sep); - var config; + var config, configPath, pkg, pkgConfig, pkgPath; while ( dirs.length ) { - config = dirs.concat(['browserslist']).join(path.sep); + configPath = dirs.concat(['browserslist']).join(path.sep); + pkgPath = dirs.concat(['package.json']).join(path.sep); - if ( fs.existsSync(config) && fs.statSync(config).isFile() ) { - return browserslist.parseConfig( fs.readFileSync(config) ); + if ( fs.existsSync(configPath) && fs.statSync(configPath).isFile() ) { + config = fs.readFileSync(configPath); + } + + if ( fs.existsSync(pkgPath) && fs.statSync(pkgPath).isFile() ) { + try { + pkg = JSON.parse( fs.readFileSync(pkgPath) ); + pkgConfig = pkg.browserslist; + } catch (e) { + console.warn('Could not parse ' + pkgPath); + console.warn(e.trace); + } + } + + if (config && pkgConfig) { + throw new Error(dirs.join(path.sep) + + ' contains both browserslist and package.json' + + ' with browserslist key, this is potentially dangerous.'); + } else if (config) { + return browserslist.parseConfig( config ); + } else if (pkgConfig) { + return pkgConfig; } dirs.pop(); diff --git a/test/config.test.js b/test/config.test.js index 1408069a..b8790081 100644 --- a/test/config.test.js +++ b/test/config.test.js @@ -3,6 +3,8 @@ var browserslist = require('../'); var path = require('path'); var css = path.join(__dirname, 'fixtures', 'dir', 'test.css'); +var withBoth = path.join(__dirname, 'fixtures', 'withBoth', 'test.css'); +var withPackage = path.join(__dirname, 'fixtures', 'withPackage', 'test.css'); it('parses queries', () => { expect(browserslist.parseConfig('ie 10\n> 1%')).toEqual(['ie 10', '> 1%']); @@ -25,3 +27,11 @@ it('returns false on no config', () => { it('reads config', () => { expect(browserslist.readConfig(css)).toEqual(['ie 11', 'ie 10']); }); + +it('reads config from package.json', () => { + expect(browserslist.readConfig(withPackage)).toEqual(['ie 9', 'ie 10']); +}); + +it('reads from dir wich contains both browserslist and package.json', () => { + expect(() => browserslist.readConfig(withBoth)).toThrow(); +}); diff --git a/test/fixtures/withBoth/browserslist b/test/fixtures/withBoth/browserslist new file mode 100644 index 00000000..0940e12b --- /dev/null +++ b/test/fixtures/withBoth/browserslist @@ -0,0 +1,3 @@ +# File for test +ie 11 +ie 10 diff --git a/test/fixtures/withBoth/package.json b/test/fixtures/withBoth/package.json new file mode 100644 index 00000000..a246db57 --- /dev/null +++ b/test/fixtures/withBoth/package.json @@ -0,0 +1,4 @@ +{ + "name": "browserslist-example", + "browserslist": ["ie 9", "ie 10"] +} diff --git a/test/fixtures/withPackage/package.json b/test/fixtures/withPackage/package.json new file mode 100644 index 00000000..a246db57 --- /dev/null +++ b/test/fixtures/withPackage/package.json @@ -0,0 +1,4 @@ +{ + "name": "browserslist-example", + "browserslist": ["ie 9", "ie 10"] +}