Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add package.json support #74 #85

Merged
merged 1 commit into from Dec 14, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Expand Up @@ -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
Expand Down
29 changes: 25 additions & 4 deletions index.js
Expand Up @@ -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();
Expand Down
10 changes: 10 additions & 0 deletions test/config.test.js
Expand Up @@ -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%']);
Expand All @@ -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();
});
3 changes: 3 additions & 0 deletions test/fixtures/withBoth/browserslist
@@ -0,0 +1,3 @@
# File for test
ie 11
ie 10
4 changes: 4 additions & 0 deletions test/fixtures/withBoth/package.json
@@ -0,0 +1,4 @@
{
"name": "browserslist-example",
"browserslist": ["ie 9", "ie 10"]
}
4 changes: 4 additions & 0 deletions test/fixtures/withPackage/package.json
@@ -0,0 +1,4 @@
{
"name": "browserslist-example",
"browserslist": ["ie 9", "ie 10"]
}