Skip to content

Commit

Permalink
relative browsers config lookup
Browse files Browse the repository at this point in the history
  • Loading branch information
amilajack committed Apr 6, 2019

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent 9e6f6b5 commit a1eaac2
Showing 4 changed files with 23 additions and 10 deletions.
17 changes: 11 additions & 6 deletions src/Versioning.js
Original file line number Diff line number Diff line change
@@ -10,22 +10,27 @@ type TargetListItem = {

/**
* Determine the targets based on the browserslist config object
*
* @param configPath - The file or a directory path to look for the browserslist config file
*/
export default function DetermineTargetsFromConfig(
configPath: string,
config?: BrowserListConfig
): Array<string> {
const browserslistOpts = { path: configPath };

if (Array.isArray(config) || typeof config === 'string') {
return browserslist(config);
return browserslist(config, browserslistOpts);
}

if (config && typeof config === 'object') {
return browserslist([
...(config.production || []),
...(config.development || [])
]);
return browserslist(
[...(config.production || []), ...(config.development || [])],
browserslistOpts
);
}

return browserslist();
return browserslist(undefined, browserslistOpts);
}

/**
2 changes: 1 addition & 1 deletion src/rules/compat.js
Original file line number Diff line number Diff line change
@@ -60,7 +60,7 @@ export default {
context.options[0];

const browserslistTargets = Versioning(
DetermineTargetsFromConfig(browserslistConfig)
DetermineTargetsFromConfig(context.getFilename(), browserslistConfig)
);

const errors = [];
5 changes: 4 additions & 1 deletion test/CanIUseProvider.spec.js
Original file line number Diff line number Diff line change
@@ -5,7 +5,10 @@ import expectRangeResultJSON from './expect-range-result-config.json';
describe('CanIUseProvider', () => {
it('should return unsupported ios targets with range value for Fetch API', () => {
const node = { caniuseId: 'fetch' };
const config = DetermineTargetsFromConfig(expectRangeResultJSON.browsers);
const config = DetermineTargetsFromConfig(
'.',
expectRangeResultJSON.browsers
);
const targets = Versioning(config);
const result = getUnsupportedTargets(node, targets);
expect(result).toMatchSnapshot();
9 changes: 7 additions & 2 deletions test/Versioning.spec.js
Original file line number Diff line number Diff line change
@@ -5,13 +5,17 @@ import singleVersionEnvPackageJSON from './single-version-config.package.json';

describe('Versioning', () => {
it('should support multi env config in browserslist package.json', () => {
const config = DetermineTargetsFromConfig(multiEnvPackageJSON.browsers);
const config = DetermineTargetsFromConfig(
'.',
multiEnvPackageJSON.browsers
);
const result = Versioning(config);
expect(result).toMatchSnapshot();
});

it('should support single array config in browserslist package.json', () => {
const config = DetermineTargetsFromConfig(
'.',
singleArrayEnvPackageJSON.browsers
);
const result = Versioning(config);
@@ -20,6 +24,7 @@ describe('Versioning', () => {

it('should support single version config in browserslist package.json', () => {
const config = DetermineTargetsFromConfig(
'.',
singleVersionEnvPackageJSON.browsers
);
const result = Versioning(config);
@@ -43,7 +48,7 @@ describe('Versioning', () => {
});

it('should support string config in rule option', () => {
const config = DetermineTargetsFromConfig('defaults, not ie < 9');
const config = DetermineTargetsFromConfig('.', 'defaults, not ie < 9');
const result = Versioning(config);
expect(result).toMatchSnapshot();
});

0 comments on commit a1eaac2

Please sign in to comment.