From 068a14ffa464d6cb0ccc5c3cec11d10d8cceae90 Mon Sep 17 00:00:00 2001 From: Arpit Chakladar <54011232+arpitchakladar@users.noreply.github.com> Date: Sun, 26 Mar 2023 22:39:07 +0530 Subject: Sharing code between scripts for reduced bundle size. --- src/scripts/functionality/home-page-banner.ts | 2 +- src/scripts/optimize.ts | 2 +- src/shared/modify-dom.ts | 41 +++++++++++++++++++++++++++ src/shared/observe-dom.ts | 12 ++++++++ src/shared/styling.ts | 8 ++++++ src/utils/modify-dom.ts | 41 --------------------------- src/utils/observe-dom.ts | 12 -------- src/utils/styling.ts | 8 ------ 8 files changed, 63 insertions(+), 63 deletions(-) create mode 100644 src/shared/modify-dom.ts create mode 100644 src/shared/observe-dom.ts create mode 100644 src/shared/styling.ts delete mode 100644 src/utils/modify-dom.ts delete mode 100644 src/utils/observe-dom.ts delete mode 100644 src/utils/styling.ts (limited to 'src') diff --git a/src/scripts/functionality/home-page-banner.ts b/src/scripts/functionality/home-page-banner.ts index 249b361..75069ca 100644 --- a/src/scripts/functionality/home-page-banner.ts +++ b/src/scripts/functionality/home-page-banner.ts @@ -1,4 +1,4 @@ -import { styles } from "@/utils/styling"; +const { styles } = await import(/* webpackIgnore: true */ "/shared/styling.js"); styles("#slider", { overflow: "visible", diff --git a/src/scripts/optimize.ts b/src/scripts/optimize.ts index 8389c53..93dd0e2 100644 --- a/src/scripts/optimize.ts +++ b/src/scripts/optimize.ts @@ -1,4 +1,4 @@ -import { modifyDOM } from "@/utils/modify-dom"; +const { modifyDOM } = await import(/* webpackIgnore: true */ "/shared/modify-dom.js"); modifyDOM([ ["head > script:nth-child(34)", { src: null }], diff --git a/src/shared/modify-dom.ts b/src/shared/modify-dom.ts new file mode 100644 index 0000000..0832d5b --- /dev/null +++ b/src/shared/modify-dom.ts @@ -0,0 +1,41 @@ +import { observeDOM } from "/shared/observe-dom"; + +type DOMReplacements = { + innerHTML?: string; + [key: string]: string | undefined | null; +}; + +type DOMModificaitonRule = readonly [string, DOMReplacements]; + +export const modifyDOM = (modificationRules: (DOMModificaitonRule | null)[]) => { + let count = 0; + + observeDOM(() => { + for (let i = 0; i < modificationRules.length; i++) { + if (modificationRules[i]) { + const element = document.querySelector(modificationRules[i]![0]) as HTMLElement | null; + if (element) { + const attributes = modificationRules[i]![1] as DOMReplacements; + if (attributes.innerHTML) { + element.innerHTML = attributes.innerHTML as string; + } + delete attributes.innerHTML; + for (const attribute in attributes) { + if (attributes[attribute] === null) { + element.removeAttribute(attribute); + } else { + element.setAttribute(attribute, attributes[attribute] as string); + } + } + modificationRules[i] = null; + count++; + if (count >= modificationRules.length) { + return true; + } + } + } + } + + return false; + }); +}; diff --git a/src/shared/observe-dom.ts b/src/shared/observe-dom.ts new file mode 100644 index 0000000..4a447a0 --- /dev/null +++ b/src/shared/observe-dom.ts @@ -0,0 +1,12 @@ +export const observeDOM = (callback: () => boolean) => { + const observer = new MutationObserver(() => { + if (callback()) { + observer.disconnect(); + } + }); + + observer.observe(document, { + childList: true, + subtree: true + }); +}; diff --git a/src/shared/styling.ts b/src/shared/styling.ts new file mode 100644 index 0000000..0e527d9 --- /dev/null +++ b/src/shared/styling.ts @@ -0,0 +1,8 @@ +export const styles = (selector: string, styles: { [key: string]: string }) => { + const elements = Array.from(document.querySelectorAll(selector)) as HTMLElement[]; + for (const element of elements) { + for (const styleType in styles) { + element.style[styleType as any] = styles[styleType]; + } + } +}; diff --git a/src/utils/modify-dom.ts b/src/utils/modify-dom.ts deleted file mode 100644 index f08f7d9..0000000 --- a/src/utils/modify-dom.ts +++ /dev/null @@ -1,41 +0,0 @@ -import { observeDOM } from "@/utils/observe-dom"; - -type DOMReplacements = { - innerHTML?: string; - [key: string]: string | undefined | null; -}; - -type DOMModificaitonRule = readonly [string, DOMReplacements]; - -export const modifyDOM = (modificationRules: (DOMModificaitonRule | null)[]) => { - let count = 0; - - observeDOM(() => { - for (let i = 0; i < modificationRules.length; i++) { - if (modificationRules[i]) { - const element = document.querySelector(modificationRules[i]![0]) as HTMLElement | null; - if (element) { - const attributes = modificationRules[i]![1] as DOMReplacements; - if (attributes.innerHTML) { - element.innerHTML = attributes.innerHTML as string; - } - delete attributes.innerHTML; - for (const attribute in attributes) { - if (attributes[attribute] === null) { - element.removeAttribute(attribute); - } else { - element.setAttribute(attribute, attributes[attribute] as string); - } - } - modificationRules[i] = null; - count++; - if (count >= modificationRules.length) { - return true; - } - } - } - } - - return false; - }); -}; diff --git a/src/utils/observe-dom.ts b/src/utils/observe-dom.ts deleted file mode 100644 index 4a447a0..0000000 --- a/src/utils/observe-dom.ts +++ /dev/null @@ -1,12 +0,0 @@ -export const observeDOM = (callback: () => boolean) => { - const observer = new MutationObserver(() => { - if (callback()) { - observer.disconnect(); - } - }); - - observer.observe(document, { - childList: true, - subtree: true - }); -}; diff --git a/src/utils/styling.ts b/src/utils/styling.ts deleted file mode 100644 index 0e527d9..0000000 --- a/src/utils/styling.ts +++ /dev/null @@ -1,8 +0,0 @@ -export const styles = (selector: string, styles: { [key: string]: string }) => { - const elements = Array.from(document.querySelectorAll(selector)) as HTMLElement[]; - for (const element of elements) { - for (const styleType in styles) { - element.style[styleType as any] = styles[styleType]; - } - } -}; -- cgit v1.2.3