From 15ed660975907f659472ae7ab309f153cf902231 Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Fri, 12 May 2023 12:13:08 +0100 Subject: [PATCH] Improve bundle size, dynamic imports & remove parse5 (#10865) * Remove unused import * Lazy load tar-js and pako for rageshakes * Update cheerio imports * Replace parse5 with DOMParser * Remove stale comment --- __mocks__/maplibre-gl.js | 2 +- package.json | 2 -- src/HtmlUtils.tsx | 4 ++-- src/SlashCommands.tsx | 16 +++++++--------- src/editor/serialize.ts | 6 +++--- src/rageshake/submit-rageshake.ts | 9 +++++++-- yarn.lock | 10 ---------- 7 files changed, 20 insertions(+), 29 deletions(-) diff --git a/__mocks__/maplibre-gl.js b/__mocks__/maplibre-gl.js index 77ee0e9a02..6c94f7c77b 100644 --- a/__mocks__/maplibre-gl.js +++ b/__mocks__/maplibre-gl.js @@ -15,7 +15,7 @@ limitations under the License. */ const EventEmitter = require("events"); -const { LngLat, NavigationControl, LngLatBounds, AttributionControl } = require("maplibre-gl"); +const { LngLat, NavigationControl, LngLatBounds } = require("maplibre-gl"); class MockMap extends EventEmitter { addControl = jest.fn(); diff --git a/package.json b/package.json index ac5657372e..4a48d01b3d 100644 --- a/package.json +++ b/package.json @@ -102,7 +102,6 @@ "minimist": "^1.2.5", "opus-recorder": "^8.0.3", "pako": "^2.0.3", - "parse5": "^6.0.1", "png-chunks-extract": "^1.0.0", "posthog-js": "1.53.2", "proposal-temporal": "^0.9.0", @@ -164,7 +163,6 @@ "@types/node": "^16", "@types/node-fetch": "^2.6.2", "@types/pako": "^2.0.0", - "@types/parse5": "^6.0.0", "@types/qrcode": "^1.3.5", "@types/react": "17.0.58", "@types/react-beautiful-dnd": "^13.0.0", diff --git a/src/HtmlUtils.tsx b/src/HtmlUtils.tsx index 71f3c9e079..f8dd52a4f0 100644 --- a/src/HtmlUtils.tsx +++ b/src/HtmlUtils.tsx @@ -19,7 +19,7 @@ limitations under the License. import React, { LegacyRef, ReactElement, ReactNode } from "react"; import sanitizeHtml from "sanitize-html"; -import cheerio from "cheerio"; +import { load as cheerio } from "cheerio"; import classNames from "classnames"; import EMOJIBASE_REGEX from "emojibase-regex"; import { merge, split } from "lodash"; @@ -549,7 +549,7 @@ export function bodyToHtml(content: IContent, highlights: Optional, op } safeBody = sanitizeHtml(formattedBody!, sanitizeParams); - const phtml = cheerio.load(safeBody, { + const phtml = cheerio(safeBody, { // @ts-ignore: The `_useHtmlParser2` internal option is the // simplest way to both parse and render using `htmlparser2`. _useHtmlParser2: true, diff --git a/src/SlashCommands.tsx b/src/SlashCommands.tsx index f3355388f2..3615e1a8a3 100644 --- a/src/SlashCommands.tsx +++ b/src/SlashCommands.tsx @@ -22,7 +22,6 @@ import { User } from "matrix-js-sdk/src/models/user"; import { Direction } from "matrix-js-sdk/src/models/event-timeline"; import { EventType } from "matrix-js-sdk/src/@types/event"; import * as ContentHelpers from "matrix-js-sdk/src/content-helpers"; -import { Element as ChildElement, parseFragment as parseHtml } from "parse5"; import { logger } from "matrix-js-sdk/src/logger"; import { IContent } from "matrix-js-sdk/src/models/event"; import { MRoomTopicEventContent } from "matrix-js-sdk/src/@types/topic"; @@ -1003,16 +1002,15 @@ export const Commands = [ // Try and parse out a widget URL from iframes if (widgetUrl.toLowerCase().startsWith("