-
-
Notifications
You must be signed in to change notification settings - Fork 901
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
Webpack production bundling react-pdf is super slow #93
Comments
Hey! If anyone has any ideas on how to improve this situation though, I'd be happy to hear! |
well, this is sad actually :( I've been experimenting with solutions but so far no luck... I've been trying to bundle this lib into separate file and and bundle it only once, ofc it's not too maintainable approach, but cutting down 180seconds would be worth it, as this library doesn't update too often, it would suffice to check version diff now and then. Will get back to you if I come up with better solution. |
@mvirbicianskas try to use parcel-bundler, it's not ideal, but it's blazingly fast! |
hey, thanks for the recommendation, will take a look, but I don't think it's webpack's problem it's uglifyjs plugin speed problem |
Either way, parcel is using uglify-es, much much newer solution than Webpack's. It may improve build speeds. Please be aware though that I don't officially support Parcel just yet, but I'm definitely on it; super excited about Parcel as much as the rest of the community is! |
In our case it's taking more than 15 minutes after adding |
Here are my suggestions (not only for you @obahareth but you may give some a go):
Let me know if anything had an especially good impact on your build performance! |
A webpack solution HTML
Webpack external configure
Import
Result was improved.from 6 min to 25sec. real 0m25.909s |
Parcel is not a solution but an alternative which is quite new and has more issues. You can easily switch to uglify-es in your webpack config and webpack 4.8 and its docs should provide the needed information. |
Personally I would not use CDNs but create a copy task and set the externals. Also Google Closure compiler can produce smaller files of pdf.js. |
The externals solution just circumvents the issue by referencing an "external" script which is not part of the bundling. I highly advice to not bundle such big libraries, use the CommonsChunkPlugin, AggressiveSplittingPlugin + externals. |
@DanielRuf Do you like share more information about your experience for webpack4 with react-pdf? It's useful let us know webpack4 is good to go. BTW anyone interested in enable uglify-es with "webpack3", you could just install newer version uglifyjs-webpack-plugin and set it in config which just like @DanielRuf talking about. |
webpack 4 in general is great and subsequent builds will be faster thanks to the new cache. Also see https://github.com/webpack-contrib/uglifyjs-webpack-plugin#uglifyjs-webpack-plugin Regarding pdf.js: I highly suggest using the externals option for this, webpack will exclude ith then from the bundle during the bundle generation. Why not use the dist min file of pdf.js and load it as extra script tag as it was already recommended? This provides the best performance during development. |
@DanielRuf Yes, I use external solution, and share my use case above, it get large improve (from 6 min to 25sec.) Webpack4 still in my evaluating, So happy to see some expert like you could share real number that more developer align with webpack4. For me the main issue of webpack4 still have some minor problem with webworker, but it's not related with react-pdf. |
I can do some benchmarking (also with different babel and browserslist settings) in the coming weeks if it would be helpful. |
Your trick and basically everything that was mentioned here doesnt work. i've been playing with this for the last 12 hours. nothing. |
I think this is not the global variable which is exported from pdf.js. See https://webpack.js.org/configuration/externals/ Did you try |
Related: mozilla/pdf.js#9545 |
In newer releases: mozilla/pdf.js#9565 |
All in all, we can only help if we have the code and config files of your project. |
I've tried HillLiu's exact example, it doesnt break the code, but i dont get any build boost. especially when i use: I've also tried pdfjsDistBuildPdf and several others. i play alot with externals and managed to make externals of all the needed dep, and still no build boost. i use NormalModuleReplacementPlugin as well to use the native minified, and excluded the them as well.. no build boost. |
And you are right about the code example. i will do that when i wake up its a bit late. |
If it does not work in your example and there is no difference, then something seems to be wrong here. |
A simple benchmark with a quick sample (without externals).
With the externals:
With the profile flag:
Some info about my system:
You can find the code at https://github.com/DanielRuf/webpack-react-pdf |
The viewer is generally more and is the viewer app in Firefox. |
@yanivkalfa I think it depend on how you use react-pdf. you could take a look my import sample. and check the js. In webpack it should replace pdfjs-dist to an empty object, And mention by @DanielRuf , if you use new version, you probably need change external parameter to new one.
|
Which files to you link in the HTML? which variable do you use? |
The bundled ones. |
@DanielRuf so you didnt linked the file in the HTML like :
and then use the externals ? |
This makes not much difference for the bundling process. |
Please see the previous comments =) This was already mentioned and with a few solutions. |
I also was struggling with the huge bundle size as soon as i included react-pdf. I'm sorry to say, but all the solutions provided were not satisfying. Either too hard to configure, half-baked or they just "moved" the problem to a different spot. Ultimately i refactored my app to use vanilla pdf.js in a local <iframe>. This will make sure that pdf.js and pdf.worker.js are completely sandboxed - they won't ever cause blocking of UI on the main frame, and the application bundle is kept clean. So if you have performance issues, i really suggest you should try as well. To communicate with the app from the iframe you can use the message api. |
Well, much code is much code, unless you delete many parts of it. The only viable solutions are to define it as externals and ship the dist file directly to the client. Reprocessing of the big files makes not much sense. |
...with proper protection (hostname check, do not use It would be great if you could provide your suggestion as PR for the docs. This would help many developers. |
Actually if the iframe is within your domain you don't need any "special" protection measure. But sure - if you deploy the PDF.js frame somewhere else then you should do that. Here's a fully working example i've put on codesandbox. This shows how to embed pdf.js completely sandboxed from the react app. Hope it helps someone :) pdf.js related files are under /public/lib |
Shot in the dark: maybe we can mark this package as a |
Does anyone have an updated way of accomplishing this? I tried doing the method with the externals, but I was receiving an error:
from this line in
I'm running react-pdf v 4.1.0 |
Any advice for implementing the externals solution without ejecting from CRA? |
Please kindly check React-PDF These changes should result in 10-15 seconds faster Webpack builds, as well as you should be able to use Webpack entry file in Create-React-App, without the need of using external CDN for hosting worker. Let me know what you think in #748! |
* Added CONTRIBUTING.md (optional) * Added backers and sponsors on the README * Added call to donate after npm install (optional) * Added .github/ISSUE_TEMPLATE.md * Remove badges * Remove sponsors * Update yarn.lock
Hello,
Am I the only one experiencing slow build times in production mode? it takes Webpack like 180seconds to build/minify pdf+worker js files, which are probably a peer dependency?
Have anyone found a working solution to ship it to production without too long bundling times?
Cheers
The text was updated successfully, but these errors were encountered: