Skip to content

Commit

Permalink
refactor: reduce dist files sizes (#76)
Browse files Browse the repository at this point in the history
* fix(parser): plugin-helper import, remove dist file code duplication

* feat(plugin-helper): reduce bundle size, set new limits to 650 bytes

* refactor(preset): html5, react presets to reduce the size of dist files
  • Loading branch information
JiLiZART authored Dec 8, 2020
1 parent 4e79abb commit fda6ddd
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 96 deletions.
2 changes: 1 addition & 1 deletion packages/bbob-parser/src/parse.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import TagNode from '@bbob/plugin-helper/lib/TagNode';
import { isTagNode } from '@bbob/plugin-helper';
import { isTagNode } from '@bbob/plugin-helper/lib/index';
import { createLexer } from './lexer';
import { createList } from './utils';

Expand Down
2 changes: 1 addition & 1 deletion packages/bbob-plugin-helper/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"bundlesize": [
{
"path": "./dist/index.min.js",
"maxSize": "580 B"
"maxSize": "650 B"
}
],
"publishConfig": {
Expand Down
20 changes: 13 additions & 7 deletions packages/bbob-plugin-helper/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ const isTagNode = (el) => typeof el === 'object' && !!el.tag;
const isStringNode = (el) => typeof el === 'string';
const isEOL = (el) => el === N;

const keysReduce = (obj, reduce, def) => Object.keys(obj).reduce(reduce, def);

const getNodeLength = (node) => {
if (isTagNode(node)) {
return node.content.reduce((count, contentNode) => count + getNodeLength(contentNode), 0);
Expand Down Expand Up @@ -61,13 +63,15 @@ const attrValue = (name, value) => {
*/
const attrsToString = (values) => {
// To avoid some malformed attributes
if (typeof values === 'undefined') {
if (values == null) {
return '';
}

return Object.keys(values)
.reduce((arr, key) => [...arr, attrValue(key, values[key])], [''])
.join(' ');
return keysReduce(
values,
(arr, key) => [...arr, attrValue(key, values[key])],
[''],
).join(' ');
};

/**
Expand All @@ -77,9 +81,11 @@ const attrsToString = (values) => {
* @param attrs
* @returns {string}
*/
const getUniqAttr = (attrs) => Object
.keys(attrs)
.reduce((res, key) => (attrs[key] === key ? attrs[key] : null), null);
const getUniqAttr = (attrs) => keysReduce(
attrs,
(res, key) => (attrs[key] === key ? attrs[key] : null),
null,
);

export {
attrsToString,
Expand Down
106 changes: 35 additions & 71 deletions packages/bbob-preset-html5/src/defaultTags.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable no-plusplus,no-lonely-if */
import { getUniqAttr, isStringNode, isTagNode } from '@bbob/plugin-helper';
import { getUniqAttr, isStringNode, isTagNode } from '@bbob/plugin-helper/lib/index';
import TagNode from '@bbob/plugin-helper/lib/TagNode';

const isStartsWith = (node, type) => (node[0] === type);
Expand Down Expand Up @@ -59,75 +59,39 @@ const renderUrl = (node, render) => (getUniqAttr(node.attrs)
? getUniqAttr(node.attrs)
: render(node.content));

const toNode = (tag, attrs, content) => ({
tag,
attrs,
content,
});

export default {
b: (node) => ({
tag: 'span',
attrs: {
style: 'font-weight: bold;',
},
content: node.content,
}),
i: (node) => ({
tag: 'span',
attrs: {
style: 'font-style: italic;',
},
content: node.content,
}),
u: (node) => ({
tag: 'span',
attrs: {
style: 'text-decoration: underline;',
},
content: node.content,
}),
s: (node) => ({
tag: 'span',
attrs: {
style: 'text-decoration: line-through;',
},
content: node.content,
}),
url: (node, { render }, options) => ({
tag: 'a',
attrs: {
href: renderUrl(node, render, options),
},
content: node.content,
}),
img: (node, { render }) => ({
tag: 'img',
attrs: {
src: render(node.content),
},
content: null,
}),
quote: (node) => ({
tag: 'blockquote',
attrs: {},
content: [{
tag: 'p',
attrs: {},
content: node.content,
}],
}),
code: (node) => ({
tag: 'pre',
attrs: {},
content: node.content,
}),
style: (node) => ({
tag: 'span',
attrs: {
style: getStyleFromAttrs(node.attrs),
},
content: node.content,
}),
list: (node) => ({
tag: getUniqAttr(node.attrs) ? 'ol' : 'ul',
attrs: getUniqAttr(node.attrs) ? {
type: getUniqAttr(node.attrs),
} : {},
content: asListItems(node.content),
}),
b: (node) => toNode('span', {
style: 'font-weight: bold;',
}, node.content),
i: (node) => toNode('span', {
style: 'font-style: italic;',
}, node.content),
u: (node) => toNode('span', {
style: 'text-decoration: underline;',
}, node.content),
s: (node) => toNode('span', {
style: 'text-decoration: line-through;',
}, node.content),
url: (node, { render }, options) => toNode('a', {
href: renderUrl(node, render, options),
}, node.content),
img: (node, { render }) => toNode('img', {
src: render(node.content),
}, null),
quote: (node) => toNode('blockquote', {}, [toNode('p', {}, node.content)]),
code: (node) => toNode('pre', {}, node.content),
style: (node) => toNode('span', {
style: getStyleFromAttrs(node.attrs),
}, node.content),
list: (node) => {
const type = getUniqAttr(node.attrs);

return toNode(type ? 'ol' : 'ul', type ? { type } : {}, asListItems(node.content));
},
};
28 changes: 13 additions & 15 deletions packages/bbob-preset-react/src/index.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,31 @@
import presetHTML5 from '@bbob/preset-html5';

const tagAttr = (style) => ({
attrs: {
style,
},
});

export default presetHTML5.extend((tags) => ({
...tags,

b: (...args) => ({
...tags.b(...args),
attrs: {
style: { fontWeight: 'bold' },
},
...tagAttr({ fontWeight: 'bold' }),
}),

i: (...args) => ({
...tags.b(...args),
attrs: {
style: { fontStyle: 'italic' },
},
...tags.i(...args),
...tagAttr({ fontStyle: 'italic' }),
}),

u: (...args) => ({
...tags.b(...args),
attrs: {
style: { textDecoration: 'underline' },
},
...tags.u(...args),
...tagAttr({ textDecoration: 'underline' }),
}),

s: (...args) => ({
...tags.b(...args),
attrs: {
style: { textDecoration: 'line-through' },
},
...tags.s(...args),
...tagAttr({ textDecoration: 'line-through' }),
}),
}));
2 changes: 1 addition & 1 deletion packages/bbob-preset/src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable indent */
import { isTagNode } from '@bbob/plugin-helper';
import { isTagNode } from '@bbob/plugin-helper/lib/index';

function process(tags, tree, core, options) {
tree.walk((node) => (isTagNode(node) && tags[node.tag]
Expand Down

0 comments on commit fda6ddd

Please sign in to comment.