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

Reduce bundle size #56

Open
ajGingrich opened this issue Apr 16, 2018 · 6 comments
Open

Reduce bundle size #56

ajGingrich opened this issue Apr 16, 2018 · 6 comments

Comments

@ajGingrich
Copy link

ajGingrich commented Apr 16, 2018

I realize that highlight.js has a very large bundle but I suggest passing an array prop to define the languages they want to import. My highlight.js currently takes up 539kb parsed size and 188kb gzipped. This is over half of my bundle and six times more than react.

react-highlight_bundle

I should be able to work on this in the upcoming weeks.

@mbj36
Copy link
Contributor

mbj36 commented Apr 17, 2018

@ajGingrich We have this option, check out our docs - https://react-highlight.neostack.com/docs/optimisation

@ajGingrich
Copy link
Author

ajGingrich commented Apr 17, 2018

@mbj36

I am already using this in my code and I still have the large bundle size.

<Highlight innerHTML={true} languages={['javascript', 'C']}>
    {post.body}
</Highlight>

Does this not work with innerHTML?

@mbj36
Copy link
Contributor

mbj36 commented Apr 18, 2018

Did you configured your webpack as per the docs ?

@ajGingrich
Copy link
Author

Sorry to keep opening and closing this, I've got a deadline at work so I thought I wouldn't be able to get to it anytime soon. Yes, I configured it like in the docs and I can make it work if I copy it locally but I need to remove this line

import hljs from 'highlight.js';

and replace it with:

import hljs from 'highlight.js/lib/highlight';

['languages', 'i', 'want'].forEach((langName) => {
  // Using require() here because import() support hasn't landed in Webpack yet
  const langModule = require(`highlight.js/lib/languages/${langName}`);
  hljs.registerLanguage(langName, langModule);
});

taken from here

@amje
Copy link

amje commented Jan 14, 2019

Did you configured your webpack as per the docs ?

There is a mistake in the docs. We have to import a different file:

import Highlight from 'react-highlight/lib/optimized';
// instead of
import Highlight from 'react-highlight';

@tony2nite
Copy link

There is some more info on optimising the build in this closed PR #35. Would be great if this info was more available in the docs.

For anyone looking to optimise on the latest version of Gatsby, here's an approach that seems to work.

As mentioned above, import the optimised package when using React Highlight then specify the language available.

import Highlight from 'react-highlight/lib/optimized'

return () => (
  <Highlight languages={['javascript']}>{someCode}</Highlight>
)

Then in gatsby-node.js set up the following:

exports.onCreateWebpackConfig = ({ stage, actions, plugins }) => {
  if (stage === 'build-javascript') {
    actions.setWebpackConfig({
      plugins: [
        plugins.contextReplacement(/highlight\.js\/lib\/languages$/, new RegExp(`^./(javascript)$`)),
      ],
    })
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants