Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletions VITE_COMPATIBILITY_FIX.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# jsPDF + Vite Compatibility Fix

## Problem
jsPDF was not compatible with Vite due to missing `exports` field in package.json, causing the error:
```
Pre-transform error: failed to resolve import 'jspdf'
```

## Root Cause
Modern bundlers like Vite require explicit module exports configuration in package.json to resolve imports correctly. The missing `exports` field prevented Vite from understanding how to import jsPDF.

## Solution
Added a comprehensive `exports` field to package.json:

```json
{
"exports": {
".": {
"import": "./dist/jspdf.es.js",
"require": "./dist/jspdf.node.js",
"browser": "./dist/jspdf.umd.js",
"types": "./types/index.d.ts"
},
"./dist/*": "./dist/*",
"./package.json": "./package.json"
}
}
```

### Additional Configuration
For Vite projects using jsPDF, add this to your `vite.config.js` to handle the external dependencies gracefully:

```javascript
import { defineConfig } from 'vite'

export default defineConfig({
build: {
rollupOptions: {
onwarn(warning, warn) {
// Ignore warnings about external modules
if (warning.code === 'UNRESOLVED_IMPORT') {
return
}
warn(warning)
}
}
},
optimizeDeps: {
include: [
'jspdf',
'@babel/runtime/helpers/typeof',
'@babel/runtime/helpers/slicedToArray',
'fflate',
'fast-png'
]
}
})
```

## Verification
- βœ… ES module imports work: `import { jsPDF } from "jspdf"`
- βœ… Vite development server runs without errors
- βœ… Vite production builds complete successfully
- βœ… Generated PDF files work correctly

## Dependencies
The following peer dependencies are required when using jsPDF with Vite:
- `@babel/runtime`
- `fflate`
- `fast-png`

## Testing
A comprehensive test project was created in `/test-vite/` that demonstrates:
1. Importing jsPDF in a Vite project
2. Creating and downloading PDF files
3. Both development and production builds working correctly

This fix resolves GitHub issue #3851 and ensures jsPDF is compatible with modern ES module bundlers like Vite.
10 changes: 10 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@
"main": "dist/jspdf.node.min.js",
"module": "dist/jspdf.es.min.js",
"browser": "dist/jspdf.es.min.js",
"exports": {
".": {
"import": "./dist/jspdf.es.js",
"require": "./dist/jspdf.node.js",
"browser": "./dist/jspdf.umd.js",
"types": "./types/index.d.ts"
},
"./dist/*": "./dist/*",
"./package.json": "./package.json"
},
"files": [
"dist",
"types/index.d.ts",
Expand Down
29 changes: 29 additions & 0 deletions test-vite/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# jsPDF + Vite Test Project

This test project demonstrates that jsPDF now works correctly with Vite after the compatibility fix.

## Setup
```bash
npm install
```

## Development
```bash
npm run dev
```
Open http://localhost:5173 and click "Generate PDF" to test.

## Production Build
```bash
npm run build
npm run preview
```
Open http://localhost:4173 and test the production build.

## What's Tested
- βœ… ES module import: `import { jsPDF } from "jspdf"`
- βœ… PDF generation and download
- βœ… Development server compatibility
- βœ… Production build success

This confirms that GitHub issue #3851 has been resolved.
375 changes: 375 additions & 0 deletions test-vite/dist/assets/index-VJj9Glvr.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions test-vite/dist/assets/index-VJj9Glvr.js.map

Large diffs are not rendered by default.

17 changes: 17 additions & 0 deletions test-vite/dist/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Vite + jsPDF Test</title>
<script type="module" crossorigin src="/assets/index-VJj9Glvr.js"></script>
</head>
<body>
<div id="app">
<h1>jsPDF + Vite Test</h1>
<button id="generate-pdf">Generate PDF</button>
<div id="status"></div>
</div>
</body>
</html>
17 changes: 17 additions & 0 deletions test-vite/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Vite + jsPDF Test</title>
</head>
<body>
<div id="app">
<h1>jsPDF + Vite Test</h1>
<button id="generate-pdf">Generate PDF</button>
<div id="status"></div>
</div>
<script type="module" src="/main.js"></script>
</body>
</html>
33 changes: 33 additions & 0 deletions test-vite/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { jsPDF } from "jspdf";

document.addEventListener('DOMContentLoaded', () => {
const button = document.getElementById('generate-pdf');
const status = document.getElementById('status');

button.addEventListener('click', () => {
try {
// Test that jsPDF import works
const doc = new jsPDF();

doc.text('Hello, this is a test PDF generated with jsPDF and Vite!', 10, 10);
doc.text('If you can see this, the import issue has been fixed.', 10, 20);

// Save the PDF
doc.save('test-vite-jspdf.pdf');

status.innerHTML = '<span style="color: green;">βœ… Success! PDF generated successfully.</span>';
status.innerHTML += '<br>jsPDF version: ' + (doc.version || 'unknown');
} catch (error) {
status.innerHTML = '<span style="color: red;">❌ Error: ' + error.message + '</span>';
console.error('jsPDF Error:', error);
}
});

// Test that the import worked
try {
const testDoc = new jsPDF();
status.innerHTML = '<span style="color: blue;">πŸ“¦ jsPDF imported successfully! Click the button to test PDF generation.</span>';
} catch (error) {
status.innerHTML = '<span style="color: red;">❌ Import Error: ' + error.message + '</span>';
}
});
Loading