2
0
mirror of https://github.com/tenrok/BBob.git synced 2026-06-20 20:00:33 +03:00

refactor: reduce dist files sizes (#76)

* 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
This commit is contained in:
Nikolay Kostyurin
2020-12-09 00:01:34 +02:00
committed by GitHub
parent 4e79abb833
commit fda6ddd6ee
6 changed files with 64 additions and 96 deletions
+1 -1
View File
@@ -1,5 +1,5 @@
import TagNode from '@bbob/plugin-helper/lib/TagNode'; 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 { createLexer } from './lexer';
import { createList } from './utils'; import { createList } from './utils';
+1 -1
View File
@@ -42,7 +42,7 @@
"bundlesize": [ "bundlesize": [
{ {
"path": "./dist/index.min.js", "path": "./dist/index.min.js",
"maxSize": "580 B" "maxSize": "650 B"
} }
], ],
"publishConfig": { "publishConfig": {
+13 -7
View File
@@ -4,6 +4,8 @@ const isTagNode = (el) => typeof el === 'object' && !!el.tag;
const isStringNode = (el) => typeof el === 'string'; const isStringNode = (el) => typeof el === 'string';
const isEOL = (el) => el === N; const isEOL = (el) => el === N;
const keysReduce = (obj, reduce, def) => Object.keys(obj).reduce(reduce, def);
const getNodeLength = (node) => { const getNodeLength = (node) => {
if (isTagNode(node)) { if (isTagNode(node)) {
return node.content.reduce((count, contentNode) => count + getNodeLength(contentNode), 0); return node.content.reduce((count, contentNode) => count + getNodeLength(contentNode), 0);
@@ -61,13 +63,15 @@ const attrValue = (name, value) => {
*/ */
const attrsToString = (values) => { const attrsToString = (values) => {
// To avoid some malformed attributes // To avoid some malformed attributes
if (typeof values === 'undefined') { if (values == null) {
return ''; return '';
} }
return Object.keys(values) return keysReduce(
.reduce((arr, key) => [...arr, attrValue(key, values[key])], ['']) values,
.join(' '); (arr, key) => [...arr, attrValue(key, values[key])],
[''],
).join(' ');
}; };
/** /**
@@ -77,9 +81,11 @@ const attrsToString = (values) => {
* @param attrs * @param attrs
* @returns {string} * @returns {string}
*/ */
const getUniqAttr = (attrs) => Object const getUniqAttr = (attrs) => keysReduce(
.keys(attrs) attrs,
.reduce((res, key) => (attrs[key] === key ? attrs[key] : null), null); (res, key) => (attrs[key] === key ? attrs[key] : null),
null,
);
export { export {
attrsToString, attrsToString,
+35 -71
View File
@@ -1,5 +1,5 @@
/* eslint-disable no-plusplus,no-lonely-if */ /* 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'; import TagNode from '@bbob/plugin-helper/lib/TagNode';
const isStartsWith = (node, type) => (node[0] === type); const isStartsWith = (node, type) => (node[0] === type);
@@ -59,75 +59,39 @@ const renderUrl = (node, render) => (getUniqAttr(node.attrs)
? getUniqAttr(node.attrs) ? getUniqAttr(node.attrs)
: render(node.content)); : render(node.content));
const toNode = (tag, attrs, content) => ({
tag,
attrs,
content,
});
export default { export default {
b: (node) => ({ b: (node) => toNode('span', {
tag: 'span', style: 'font-weight: bold;',
attrs: { }, node.content),
style: 'font-weight: bold;', i: (node) => toNode('span', {
}, style: 'font-style: italic;',
content: node.content, }, node.content),
}), u: (node) => toNode('span', {
i: (node) => ({ style: 'text-decoration: underline;',
tag: 'span', }, node.content),
attrs: { s: (node) => toNode('span', {
style: 'font-style: italic;', style: 'text-decoration: line-through;',
}, }, node.content),
content: node.content, url: (node, { render }, options) => toNode('a', {
}), href: renderUrl(node, render, options),
u: (node) => ({ }, node.content),
tag: 'span', img: (node, { render }) => toNode('img', {
attrs: { src: render(node.content),
style: 'text-decoration: underline;', }, null),
}, quote: (node) => toNode('blockquote', {}, [toNode('p', {}, node.content)]),
content: node.content, code: (node) => toNode('pre', {}, node.content),
}), style: (node) => toNode('span', {
s: (node) => ({ style: getStyleFromAttrs(node.attrs),
tag: 'span', }, node.content),
attrs: { list: (node) => {
style: 'text-decoration: line-through;', const type = getUniqAttr(node.attrs);
},
content: node.content, return toNode(type ? 'ol' : 'ul', type ? { type } : {}, asListItems(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),
}),
}; };
+13 -15
View File
@@ -1,33 +1,31 @@
import presetHTML5 from '@bbob/preset-html5'; import presetHTML5 from '@bbob/preset-html5';
const tagAttr = (style) => ({
attrs: {
style,
},
});
export default presetHTML5.extend((tags) => ({ export default presetHTML5.extend((tags) => ({
...tags, ...tags,
b: (...args) => ({ b: (...args) => ({
...tags.b(...args), ...tags.b(...args),
attrs: { ...tagAttr({ fontWeight: 'bold' }),
style: { fontWeight: 'bold' },
},
}), }),
i: (...args) => ({ i: (...args) => ({
...tags.b(...args), ...tags.i(...args),
attrs: { ...tagAttr({ fontStyle: 'italic' }),
style: { fontStyle: 'italic' },
},
}), }),
u: (...args) => ({ u: (...args) => ({
...tags.b(...args), ...tags.u(...args),
attrs: { ...tagAttr({ textDecoration: 'underline' }),
style: { textDecoration: 'underline' },
},
}), }),
s: (...args) => ({ s: (...args) => ({
...tags.b(...args), ...tags.s(...args),
attrs: { ...tagAttr({ textDecoration: 'line-through' }),
style: { textDecoration: 'line-through' },
},
}), }),
})); }));
+1 -1
View File
@@ -1,5 +1,5 @@
/* eslint-disable indent */ /* eslint-disable indent */
import { isTagNode } from '@bbob/plugin-helper'; import { isTagNode } from '@bbob/plugin-helper/lib/index';
function process(tags, tree, core, options) { function process(tags, tree, core, options) {
tree.walk((node) => (isTagNode(node) && tags[node.tag] tree.walk((node) => (isTagNode(node) && tags[node.tag]