Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[@mantine/code-highlight] Attempt on reducing bundle size #5236

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

ibedwi
Copy link

@ibedwi ibedwi commented Nov 12, 2023

Description

This is an attempt to reduce the size of code-highlight.

Probably address #4879, #5021

Approach

Instead of install highlight.js as the library's dependencies, it's installed as peerDependencies. The project that use code highlight library must install highlight.js as its dependencies and pass it to this library via HiglightProvider

// /the-project/page.tsx
import { CodeHighlight, HighlightProvider } from "@mantine/code-highlight-lite";

// Only import the core module
import hljs from "highlight.js/lib/core";

// Only registere the used languages
hljs.registerLanguage(
  "plaintext",
  require("highlight.js/lib/languages/plaintext")
);
hljs.registerLanguage(
  "javascript",
  require("highlight.js/lib/languages/javascript")
);
hljs.registerLanguage(
  "typescript",
  require("highlight.js/lib/languages/typescript")
);

// Create example typescript code
const exampleCode = "...";

export default function Home() {
  return (
    // pass hljs as props to the provider
    <HighlightProvider highlightInstance={hljs}>
        <CodeHighlight code={exampleCode} language="typescript" />
    </HighlightProvider>
  );
}

Result

This approach tested using Next.js 14.0.2 (app router) and Webpack Bundle Analyzer. The highlight.js's chunk is very small and only include the registered language:

Stat size: 120.91 KB
Parsed size: 38.16 KB
Gzipped size: 12.23 KB
CleanShot 2023-11-12 at 13 53 48@2x

Demo Project

See the demo project here

@rtivital
Copy link
Member

Hi @ibedwi, thanks for the PR! I would rather not maintain two separate versions of the same package. It also seems that it is not possible to solve this issue without breaking changes. I'll keep this PR opened until 8.0 is out for reference. Now the PR cannot be merged.

@ibedwi
Copy link
Author

ibedwi commented Nov 12, 2023

Hi @rtivital ! I agree to not maintain two separate versions of the same package, since it serves the same purpose.

It also seems that it is not possible to solve this issue without breaking changes.

Yeap! Since this approach exclude highlight.js from the package, it definitely introduce breaking changes.

I'll keep this PR opened until 8.0 is out for reference. Now the PR cannot be merged.

Thanks for considering this PR! I would love to contribute! Feel free to ping me 😁

@rtivital rtivital force-pushed the master branch 11 times, most recently from 9ea2fcd to eebd557 Compare December 8, 2023 13:46
@rtivital rtivital added the Reviewed PR Reviewed PR, most likely will not be merged. PR may be kept open for context or as a reminder. label Jan 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Reviewed PR Reviewed PR, most likely will not be merged. PR may be kept open for context or as a reminder.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants