Skip to content

Commit db7b932

Browse files
authored
Merge pull request #37 from contentstack/EB-643-json-rte-serializer-very-large-bundle-size-from-dependencies
json rte serializer very large bundle size from dependencies
2 parents cf6bd1c + fc13a6a commit db7b932

File tree

8 files changed

+2560
-6532
lines changed

8 files changed

+2560
-6532
lines changed

package-lock.json

Lines changed: 2350 additions & 6518 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@
33
"version": "2.0.5",
44
"description": "This Package converts Html Document to Json and vice-versa.",
55
"main": "lib/index.js",
6+
"module": "lib/index.mjs",
67
"types": "lib/index.d.ts",
78
"scripts": {
89
"test": "jest",
910
"prepare": "npm run build",
10-
"build": "tsc"
11+
"build:cjs": "esbuild src/index.tsx --bundle --outdir=lib --platform=node --minify",
12+
"build:esm": "esbuild src/index.tsx --bundle --outdir=lib --format=esm --out-extension:.js=.mjs --minify",
13+
"build": "npm run build:cjs && npm run build:esm && tsc --emitDeclarationOnly --outDir lib"
1114
},
1215
"repository": {
1316
"type": "git",
@@ -24,11 +27,19 @@
2427
},
2528
"homepage": "https://github.com/contentstack/json-rte-serializer#readme",
2629
"devDependencies": {
27-
"@types/jest": "^26.0.23",
30+
"@types/jest": "^27.0.3",
2831
"@types/jsdom": "^16.2.12",
29-
"@types/lodash": "^4.14.168",
32+
"@types/lodash.clonedeep": "^4.5.9",
33+
"@types/lodash.flatten": "^4.4.9",
34+
"@types/lodash.isempty": "^4.4.9",
35+
"@types/lodash.isequal": "^4.5.8",
36+
"@types/lodash.isobject": "^3.0.9",
37+
"@types/lodash.isplainobject": "^4.0.9",
38+
"@types/lodash.isundefined": "^3.0.9",
39+
"@types/lodash.kebabcase": "^4.1.9",
3040
"@types/omit-deep-lodash": "^1.1.1",
3141
"@types/uuid": "^8.3.0",
42+
"esbuild": "0.19.11",
3243
"jest": "^27.5.1",
3344
"jest-html-reporter": "^3.7.0",
3445
"jsdom": "^16.6.0",
@@ -38,10 +49,14 @@
3849
},
3950
"dependencies": {
4051
"array-flat-polyfill": "^1.0.1",
41-
"lodash": "^4.17.21",
42-
"slate": "^0.72.0",
43-
"slate-hyperscript": "^0.62.0",
44-
"tslib": "^2.3.1",
52+
"lodash.clonedeep": "^4.5.0",
53+
"lodash.flatten": "^4.4.0",
54+
"lodash.isempty": "^4.4.0",
55+
"lodash.isequal": "^4.5.0",
56+
"lodash.isobject": "^3.0.2",
57+
"lodash.isplainobject": "^4.0.6",
58+
"lodash.isundefined": "^3.0.1",
59+
"lodash.kebabcase": "^4.1.1",
4560
"uuid": "^8.3.2"
4661
},
4762
"files": [

src/fromRedactor.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { jsx } from 'slate-hyperscript'
21
import { v4 } from 'uuid'
32
import kebabCase from "lodash/kebabCase"
43
import isEmpty from "lodash/isEmpty"
@@ -7,6 +6,8 @@ import isObject from "lodash/isObject"
76
import cloneDeep from "lodash/cloneDeep"
87
import isUndefined from "lodash/isUndefined"
98

9+
import { jsx } from './utils/jsx'
10+
1011
import {IHtmlToJsonElementTags,IHtmlToJsonOptions, IHtmlToJsonTextTags, IAnyObject} from './types'
1112

1213
const generateId = () => v4().split('-').join('')

src/toRedactor.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import kebbab from 'lodash/kebabCase'
2-
import isEmpty from 'lodash/isEmpty'
1+
import kebbab from 'lodash.kebabcase'
2+
import isEmpty from 'lodash.isempty'
33

44
import {IJsonToHtmlElementTags, IJsonToHtmlOptions, IJsonToHtmlTextTags} from './types'
5-
import { isPlainObject } from 'lodash'
5+
import isPlainObject from 'lodash.isplainobject'
66

77
const ELEMENT_TYPES: IJsonToHtmlElementTags = {
88
'blockquote': (attrs: string, child: string) => {

src/utils/jsx.ts

Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
import isPlainObject from "lodash.isplainobject";
2+
import isEqual from "lodash.isequal";
3+
4+
const resolveDescendants = (children: any[]) => {
5+
const nodes: any[] = [];
6+
7+
const addChild = (child: any): void => {
8+
if (child == null) {
9+
return;
10+
}
11+
12+
const prev = nodes[nodes.length - 1];
13+
14+
if (typeof child === "string") {
15+
const text = { text: child };
16+
STRINGS.add(text);
17+
child = text;
18+
}
19+
20+
if (isText(child)) {
21+
const c = child; // HACK: fix typescript complaining
22+
23+
if (
24+
isText(prev) &&
25+
STRINGS.has(prev) &&
26+
STRINGS.has(c) &&
27+
textEquals(prev, c, { loose: true })
28+
) {
29+
prev.text += c.text;
30+
} else {
31+
nodes.push(c);
32+
}
33+
} else if (isElement(child)) {
34+
nodes.push(child);
35+
} else {
36+
throw new Error(`Unexpected hyperscript child object: ${child}`);
37+
}
38+
};
39+
40+
for (const child of children.flat(Infinity)) {
41+
addChild(child);
42+
}
43+
44+
return nodes;
45+
};
46+
47+
/**
48+
* Create an `Element` object.
49+
*/
50+
51+
export function createElement(
52+
tagName: string,
53+
attributes: { [key: string]: any },
54+
children: any[]
55+
) {
56+
return { ...attributes, children: resolveDescendants(children) };
57+
}
58+
59+
/**
60+
* Create a fragment.
61+
*/
62+
63+
export function createFragment(
64+
tagName: string,
65+
attributes: { [key: string]: any },
66+
children: any[]
67+
) {
68+
return resolveDescendants(children);
69+
}
70+
71+
/**
72+
* Create a `Text` object.
73+
*/
74+
75+
export function createText(
76+
tagName: string,
77+
attributes: { [key: string]: any },
78+
children: any[]
79+
) {
80+
const nodes = resolveDescendants(children);
81+
82+
if (nodes.length > 1) {
83+
throw new Error(
84+
`The <text> hyperscript tag must only contain a single node's worth of children.`
85+
);
86+
}
87+
88+
let [node] = nodes;
89+
90+
if (node == null) {
91+
node = { text: "" };
92+
}
93+
94+
if (!isText(node)) {
95+
throw new Error(`
96+
The <text> hyperscript tag can only contain text content as children.`);
97+
}
98+
99+
// COMPAT: If they used the <text> tag we want to guarantee that it won't be
100+
// merge with other string children.
101+
STRINGS.delete(node);
102+
103+
Object.assign(node, attributes);
104+
return node;
105+
}
106+
107+
const STRINGS = new WeakSet();
108+
109+
function isText(value: any) {
110+
return isPlainObject(value) && typeof value.text === "string";
111+
}
112+
113+
function textEquals(
114+
text: any,
115+
another: any,
116+
options: { loose?: boolean } = {}
117+
): boolean {
118+
const { loose = false } = options;
119+
120+
function omitText(obj: Record<any, any>) {
121+
const { text, ...rest } = obj;
122+
123+
return rest;
124+
}
125+
126+
return isEqual(
127+
loose ? omitText(text) : text,
128+
loose ? omitText(another) : another
129+
);
130+
}
131+
132+
const isElement = (value: any): boolean => {
133+
return (
134+
isPlainObject(value) &&
135+
isNodeList(value.children)
136+
// && !Editor.isEditor(value) // value cannot be editor
137+
)
138+
};
139+
140+
export const jsx = (
141+
tagName: string,
142+
attributes?: Object,
143+
...children: any[]
144+
) => {
145+
const creators = {
146+
element: createElement,
147+
fragment: createFragment,
148+
text: createText,
149+
};
150+
const creator = creators[tagName];
151+
152+
if (!creator) {
153+
throw new Error(`No hyperscript creator found for tag: <${tagName}>`);
154+
}
155+
156+
if (attributes == null) {
157+
attributes = {};
158+
}
159+
160+
if (!isPlainObject(attributes)) {
161+
children = [attributes].concat(children);
162+
attributes = {};
163+
}
164+
165+
children = children.filter((child) => Boolean(child)).flat();
166+
const ret = creator(tagName, attributes, children);
167+
return ret;
168+
};
169+
170+
171+
function isNodeList (value: any[]) {
172+
return value.every(val => isNode(val))
173+
}
174+
175+
function isNode(value: any){
176+
return (
177+
isText(value) || isElement(value)
178+
// || Editor.isEditor(value) // // value cannot be editor
179+
)
180+
}

test/fromRedactor.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// @ts-nocheck
22
import { fromRedactor, getNestedValueIfAvailable } from "../src/fromRedactor"
33
import { JSDOM } from "jsdom"
4-
import { isEqual } from "lodash"
4+
import isEqual from "lodash.isequal"
55
import omitdeep from "omit-deep-lodash"
66
import expectedValue from "./expectedJson"
77

test/toRedactor.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { toRedactor } from "../src/toRedactor"
2-
import { isEqual } from "lodash"
2+
import isEqual from "lodash.isequal"
33

44
import expectedValue from "./expectedJson"
55
import { imageAssetData } from "./testingData"

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"strict": true,
99
"jsx": "react",
1010
"resolveJsonModule": true,
11-
"importHelpers": true,
11+
"importHelpers": false,
1212
"suppressImplicitAnyIndexErrors": true
1313
},
1414
"include": [

0 commit comments

Comments
 (0)