Skip to content

Commit

Permalink
feat: allow fallback if data for target not available
Browse files Browse the repository at this point in the history
  • Loading branch information
pemrouz committed Nov 19, 2017
1 parent ca20aaf commit 966e02f
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 91 deletions.
37 changes: 13 additions & 24 deletions dist/buble.deps.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/buble.deps.js.map

Large diffs are not rendered by default.

37 changes: 13 additions & 24 deletions dist/buble.es.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/buble.es.js.map

Large diffs are not rendered by default.

37 changes: 13 additions & 24 deletions dist/buble.umd.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/buble.umd.js.map

Large diffs are not rendered by default.

37 changes: 21 additions & 16 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,29 +12,34 @@ const { parse } = [acornObjectSpread, acornJsx].reduce(

const dangerousTransforms = ['dangerousTaggedTemplateString', 'dangerousForOf'];

const fallbackOrThrow = (fallback, error) => {
if (!('bitmask' in fallback)) throw new Error(error)
if (!fallback.suppress) console.error(error)
return fallback.bitmask
}

export function target(target) {
const fallback = {}
if ('fallback' in target) {
fallback.environment = Object.keys(target.fallback).filter(d => d != 'suppress').pop()
fallback.version = target.fallback[fallback.environment]
fallback.suppress = !!target.fallback.suppress
delete target.fallback
if (matrix[fallback.environment] && (fallback.version in matrix[fallback.environment]))
fallback.bitmask = matrix[fallback.environment][fallback.version]
}

const targets = Object.keys(target);
let bitmask = targets.length
? 0b11111111111111111111111111111111
: 0b01000000000000000000000000000000;

Object.keys(target).forEach(environment => {
const versions = matrix[environment];
if (!versions)
throw new Error(
`Unknown environment '${environment}'. Please raise an issue at https://github.com/Rich-Harris/buble/issues`
);

const targetVersion = target[environment];
if (!(targetVersion in versions))
throw new Error(
`Support data exists for the following versions of ${environment}: ${Object.keys(
versions
).join(
', '
)}. Please raise an issue at https://github.com/Rich-Harris/buble/issues`
);
const support = versions[targetVersion];
const versions = matrix[environment]
const support =
!versions ? fallbackOrThrow(fallback, `Unknown environment '${environment}'. Please raise an issue at https://github.com/Rich-Harris/buble/issues`)
: !(target[environment] in versions) ? fallbackOrThrow(fallback, `Support data exists for the following versions of ${environment}: ${Object.keys(versions).join(', ')}. Please raise an issue at https://github.com/Rich-Harris/buble/issues`)
: versions[target[environment]]

bitmask &= support;
});
Expand Down
14 changes: 14 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,20 @@ describe('buble', () => {
assert.ok(!transforms.arrow);
});

it('falls back if nonexistent browser', () => {
var transforms = buble.target({ nonexistent: 99, fallback: { node: 5 }});

assert.ok(transforms.defaultParameter);
assert.ok(!transforms.arrow);
});

it('falls back if nonexistent version', () => {
var transforms = buble.target({ chrome: -99, fallback: { node: 5 }});

assert.ok(transforms.defaultParameter);
assert.ok(!transforms.arrow);
});

it('only applies necessary transforms', () => {
var source = `
const power = ( base, exponent = 2 ) => Math.pow( base, exponent );`;
Expand Down

0 comments on commit 966e02f

Please sign in to comment.