Skip to content

Commit

Permalink
patch: reduce transpiled bundle size (#522)
Browse files Browse the repository at this point in the history
  • Loading branch information
ealush committed Nov 24, 2020
1 parent 9549232 commit 0006059
Show file tree
Hide file tree
Showing 17 changed files with 144 additions and 121 deletions.
2 changes: 2 additions & 0 deletions jsconfig.json

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

26 changes: 13 additions & 13 deletions package-lock.json

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

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
],
"devDependencies": {
"@ampproject/rollup-plugin-closure-compiler": "^0.26.0",
"@babel/cli": "^7.12.7",
"@babel/core": "^7.12.7",
"@babel/cli": "^7.12.8",
"@babel/core": "^7.12.8",
"@babel/plugin-proposal-optional-chaining": "^7.12.7",
"@babel/plugin-transform-object-assign": "^7.12.1",
"@babel/plugin-transform-runtime": "^7.12.1",
Expand Down
3 changes: 3 additions & 0 deletions packages/__shared/src/setFnName.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function setFnName(fn, value) {
return Object.defineProperty(fn, 'name', { value });
}
23 changes: 23 additions & 0 deletions packages/__shared/src/withArgs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import setFnName from 'setFnName';

/**
* ES5 Transpilation increases the size of spread arguments by a lot.
* Wraps a function and passes its spread params as an array.
*
* @param {Function} cb
* @param {String} [fnName]
* @return {Function}
*/
export default function withAgs(cb, fnName) {
return setFnName((...args) => cb(args), fnName || cb.name);
}

/**
* Spreads all the passed arguments, and forwards them as
* first arg, and the rest as an array.
*
* @param {Function} cb
*/
export function withFirst(cb) {
return withAgs(args => cb(args[0], args.slice(1)), cb.name);
}
46 changes: 23 additions & 23 deletions packages/n4s/src/enforce/bindLazyRule.js
Original file line number Diff line number Diff line change
@@ -1,40 +1,40 @@

import { RUN_RULE } from 'enforceKeywords';
import genRuleProxy from 'genRuleProxy';
import isFunction from 'isFunction';
import runtimeRules from 'runtimeRules';
import setFnName from 'setFnName';
import withArgs from 'withArgs';

// Initiates a chain of functions directly from the `enforce`
// function - that's even though we do not have any closure
// there to store that data.
export default function bindLazyRule(ruleName) {
const registeredRules = [];

const addFn = fnName => (...args) => {
registeredRules.push(
Object.defineProperty(
value => runtimeRules[fnName](value, ...args),
'name',
{ value: fnName }
)
);
const addFn = fnName =>
withArgs(args => {
registeredRules.push(
setFnName(value => runtimeRules[fnName](value, ...args), fnName)
);

const returnvalue = genRuleProxy({}, addFn);
const returnvalue = genRuleProxy({}, addFn);

return Object.assign(returnvalue, {
[RUN_RULE]: getValue => {
return registeredRules.every(fn => {
try {
// This inversion of control when getting the value is
// required in order to pass the function over to `shape`
// so it can make the decision which args to pass to `optional`
return !!fn(isFunction(getValue) ? getValue(fn.name) : getValue);
} catch (e) {
return false;
}
});
},
return Object.assign(returnvalue, {
[RUN_RULE]: getValue => {
return registeredRules.every(fn => {
try {
// This inversion of control when getting the value is
// required in order to pass the function over to `shape`
// so it can make the decision which args to pass to `optional`
return !!fn(isFunction(getValue) ? getValue(fn.name) : getValue);
} catch (e) {
return false;
}
});
},
});
});
};

return addFn(ruleName);
}
9 changes: 7 additions & 2 deletions packages/n4s/src/enforce/compounds/anyOf.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import runLazyRules from 'runLazyRules';
import { withFirst } from 'withArgs';

/**
* @param {*} value Value to be test against rules
* @param {Function[]} rules Rules to validate the value with
*/
export default function anyOf(value, ...rules) {
return !(rules.length) || rules.some(ruleGroup => runLazyRules(ruleGroup, value));
function anyOf(value, rules) {
return (
!rules.length || rules.some(ruleGroup => runLazyRules(ruleGroup, value))
);
}

export default withFirst(anyOf);
5 changes: 4 additions & 1 deletion packages/n4s/src/enforce/compounds/isArrayOf.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { isNotArray } from 'isArray';
import runLazyRules from 'runLazyRules';
import { withFirst } from 'withArgs';

export default function isArrayOf(value, ...ruleChain) {
function isArrayOf(value, ruleChain) {
if (isNotArray(value)) {
return false;
}
return value.every(element =>
ruleChain.some(rule => runLazyRules(rule, element))
);
}

export default withFirst(isArrayOf);
5 changes: 4 additions & 1 deletion packages/n4s/src/enforce/compounds/optional.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { isNull } from 'isNull';
import { isUndefined } from 'isUndefined';
import runLazyRules from 'runLazyRules';
import { withFirst } from 'withArgs';

/**
* @param {Array} ObjectEntry Object and key leading to current value
* @param {Function[]} rules Rules to validate the value with
*/
export default function optional([obj, key], ...ruleGroups) {
function optional([obj, key], ruleGroups) {
if (
!Object.prototype.hasOwnProperty.call(obj, key) ||
isUndefined(obj[key] || isNull(obj[key]))
Expand All @@ -16,3 +17,5 @@ export default function optional([obj, key], ...ruleGroups) {

return runLazyRules(ruleGroups, obj[key]);
}

export default withFirst(optional);
11 changes: 7 additions & 4 deletions packages/n4s/src/enforce/enforce.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@ import bindTemplate from 'enforce.template';
import runner from 'enforceRunner';
import genRuleProxy from 'genRuleProxy';
import runtimeRules from 'runtimeRules';
import withArgs from 'withArgs';

const Enforce = value => {
const proxy = genRuleProxy({}, ruleName => (...args) => {
runner(runtimeRules[ruleName], value, args);
return proxy;
});
const proxy = genRuleProxy({}, ruleName =>
withArgs(args => {
runner(runtimeRules[ruleName], value, args);
return proxy;
})
);
return proxy;
};

Expand Down
15 changes: 9 additions & 6 deletions packages/n4s/src/enforce/enforce.template.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,24 @@ import genRuleProxy from 'genRuleProxy';
import isFunction from 'isFunction';
import runLazyRules from 'runLazyRules';
import runtimeRules from 'runtimeRules';
import withArgs from 'withArgs';

export default function bindTemplate(enforce) {
enforce.template = (...rule) => {
enforce.template = withArgs(rule => {
const template = value => {
runner(runLazyRules.bind(null, rule), value);
const proxy = genRuleProxy({}, ruleName => (...args) => {
runner(runtimeRules[ruleName], value, args);
return proxy;
});
const proxy = genRuleProxy({}, ruleName =>
withArgs(args => {
runner(runtimeRules[ruleName], value, args);
return proxy;
})
);
return proxy;
};

template.test = getValue =>
runLazyRules(rule, isFunction(getValue) ? getValue() : getValue);

return template;
};
});
}
3 changes: 2 additions & 1 deletion packages/vest/src/core/produce/collectFailureMessages.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ import useTestObjects from 'useTestObjects';
* @param {String} [options.fieldName] Field name for error lookup.
* @returns all messages for given criteria.
*/
const collectFailureMessages = (severity, { group, fieldName } = {}) => {
const collectFailureMessages = (severity, options) => {
const [testObjects] = useTestObjects();
const { group, fieldName } = options || {};
const res = testObjects.reduce((collector, testObject) => {
if (group && testObject.groupName !== group) {
return collector;
Expand Down
64 changes: 17 additions & 47 deletions packages/vest/src/core/produce/produce.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import isFunction from 'isFunction';
import { SEVERITY_GROUP_ERROR, SEVERITY_GROUP_WARN } from 'resultKeys';
import useTestCallbacks from 'useTestCallbacks';
import useTestObjects from 'useTestObjects';
import withArgs from 'withArgs';

const cache = createCache(20);

Expand All @@ -19,7 +20,7 @@ const cache = createCache(20);
* @param {Function} doneCallback
* @register {Object} Vest output object.
*/
const done = (...args) => {
const done = withArgs(args => {
const [callback, fieldName] = args.reverse();
const { stateRef } = context.use();

Expand Down Expand Up @@ -57,72 +58,41 @@ const done = (...args) => {
});

return output;
};
});

/**
* @param {boolean} [isDraft]
* @returns Vest output object.
*/

const produce = isDraft => {
const { stateRef } = context.use();
const [testObjects] = useTestObjects();

const ctxRef = { stateRef };

return cache(
[testObjects, isDraft],
context.bind({ stateRef }, () =>
context.bind(ctxRef, () =>
Object.defineProperties(
genTestsSummary(),
[
[
'hasErrors',
context.bind({ stateRef }, hasFaillures, SEVERITY_GROUP_ERROR),
],
[
'hasWarnings',
context.bind({ stateRef }, hasFaillures, SEVERITY_GROUP_WARN),
],
[
'getErrors',
context.bind({ stateRef }, getFailures, SEVERITY_GROUP_ERROR),
],
[
'getWarnings',
context.bind({ stateRef }, getFailures, SEVERITY_GROUP_WARN),
],
[
'hasErrorsByGroup',
context.bind(
{ stateRef },
hasFailuresByGroup,
SEVERITY_GROUP_ERROR
),
],
[
'hasWarningsByGroup',
context.bind({ stateRef }, hasFailuresByGroup, SEVERITY_GROUP_WARN),
],
[
'getErrorsByGroup',
context.bind(
{ stateRef },
getFailuresByGroup,
SEVERITY_GROUP_ERROR
),
],
[
'getWarningsByGroup',
context.bind({ stateRef }, getFailuresByGroup, SEVERITY_GROUP_WARN),
],
['hasErrors', hasFaillures, SEVERITY_GROUP_ERROR],
['hasWarnings', hasFaillures, SEVERITY_GROUP_WARN],
['getErrors', getFailures, SEVERITY_GROUP_ERROR],
['getWarnings', getFailures, SEVERITY_GROUP_WARN],
['hasErrorsByGroup', hasFailuresByGroup, SEVERITY_GROUP_ERROR],
['hasWarningsByGroup', hasFailuresByGroup, SEVERITY_GROUP_WARN],
['getErrorsByGroup', getFailuresByGroup, SEVERITY_GROUP_ERROR],
['getWarningsByGroup', getFailuresByGroup, SEVERITY_GROUP_WARN],
]
.concat(isDraft ? [] : [['done', context.bind({ stateRef }, done)]])
.concat(isDraft ? [] : [['done', done]])
.reduce(
(properties, [name, value]) => (
(properties, [name, fn, severityKey]) => (
(properties[name] = {
configurable: true,
enumerable: true,
name,
value,
value: context.bind(ctxRef, fn, severityKey),
writeable: true,
}),
properties
Expand Down

0 comments on commit 0006059

Please sign in to comment.