Skip to content

jsPDF build failed with create-react-app #3043

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

Closed
ngocbnu213 opened this issue Dec 20, 2020 · 12 comments
Closed

jsPDF build failed with create-react-app #3043

ngocbnu213 opened this issue Dec 20, 2020 · 12 comments

Comments

@ngocbnu213
Copy link

ngocbnu213 commented Dec 20, 2020

I'm using jsPDF in my react application. When I try to use command npm run build to build my application but I got this error
image

I'm using version 2.1.0. I also had tried to upgrade to newest version but I still have same error. I have a quick look into file jspdf.es.min.js and I saw it was difference to previous stable build before 13 days ago. Before that I had no problem
Please help me to solve this problem

@HackbrettXXX
Copy link
Collaborator

Please state exactly which versions work and which versions don't. Please also provide a small repro. Otherwise we can't help you.

@ngocbnu213
Copy link
Author

ngocbnu213 commented Dec 22, 2020

Both version 2.1.0 and 2.2.0 were not working. In my project I'm using version 2.1.0 but when I installed dependencies I see jspdf try to download version 2.2.0. It's easy to reproduce. You have a react application. Add jspdf version2.1.0to your package.json Then use these commands npm install, npm run build. Error will happens
My package.json:
image
My package-lock.json after installing dependencies
image

@HackbrettXXX
Copy link
Collaborator

Can't reproduce. What I did is:

npx create-react-app .
yarn add jspdf
yarn start

-> works

I added this code to src/index.js:

import { jsPDF } from "jspdf"

new jsPDF().save()

yarn start and yarn build still work and the app downloads an empty PDF on startup as expected.

@akkravikumar
Copy link

akkravikumar commented Dec 28, 2020

Im also facing this issue. Try with 2.1.0 or 1.5.3 version it's working good

@jfajardo
Copy link

I had the same problem, I downgrade the version to "jspdf": "2.0.0" and works fine in Vercel

Failed to compile.
--
00:12:41.537 | Failed to minify the code from this file:
00:12:41.537 | ./node_modules/jspdf/dist/jspdf.es.min.js:96:91
00:12:41.537 | Read more here: http://bit.ly/2tRViJ9

@HackbrettXXX
Copy link
Collaborator

@jfajardo, @akkravikumar Please attach or link to a small project that reproduces the issue, otherwise we can't help you.

@mititch
Copy link

mititch commented Feb 23, 2021

Maybe this log will be helpful to found issue
Tue, 23 Feb 2021 09:57:31 GMT app:build:webpack-compiler Webpack compiler encountered errors. Tue, 23 Feb 2021 09:57:31 GMT app:build:webpack-compiler vendor.min.js from UglifyJs In strict mode code, functions can only be declared at top level or immediately within another function. [C://workspace/Admin/admin - i/src/frontend/node_modules/jspdf/dist/jspdf.es.min.js:77,3372][vendor.min.js:107770,3372] Tue, 23 Feb 2021 09:57:31 GMT app:bin:compile Compiler encountered an error. Error: Webpack compiler encountered errors

Update: looks this fails in strict mode https://github.com/MrRio/jsPDF/blob/master/src/libs/md5.js#L214

@HackbrettXXX
Copy link
Collaborator

@mititch thanks, this helped. We probably just need to rewrite this code a little. E.g.

var add32 = function add32(a, b) { // make it a "var"
  return (a + b) & 0xffffffff;
}

if (md5("hello") != "5d41402abc4b2a76b9719d911017c592") {
  add32 = function add32(x, y) {
    var lsw = (x & 0xffff) + (y & 0xffff),
      msw = (x >> 16) + (y >> 16) + (lsw >> 16);
    return (msw << 16) | (lsw & 0xffff);
  }
}

However, we need to move it to the top of the file then probably.

@mititch could you prepare a PR for this?

@JKMarkowski
Copy link

Hello @HackbrettXXX, @mititch are there any plans to introduce changes resolving this issue?

@HackbrettXXX
Copy link
Collaborator

I currently don't have the time to do it myself, but I will merge a pull request.

@ThomasStubbe
Copy link
Contributor

I made a pull request: #3173
However, I noticed that git claims that all lines have changed. I changed the line endings from CRLF to LF, but no avail.
Feel free to rewrite or change it

@github-actions
Copy link

This issue is stale because it has been open 90 days with no activity. It will be closed soon. Please comment/reopen if this issue is still relevant.

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

No branches or pull requests

7 participants