Skip to content

Commit 82c9029

Browse files
Only auto apply plugin in browser environment
1 parent cd7b967 commit 82c9029

File tree

4 files changed

+14
-28
lines changed

4 files changed

+14
-28
lines changed

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/applyPlugin.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { parseInput } from './inputParser'
66
import { createTable } from './tableCalculator'
77
import { drawTable } from './tableDrawer'
88

9-
export default function (jsPDF: jsPDFConstructor) {
9+
export function applyPlugin(jsPDF: jsPDFConstructor) {
1010
// eslint-disable-next-line @typescript-eslint/no-explicit-any
1111
jsPDF.API.autoTable = function (this: jsPDFDocument, ...args: any[]) {
1212
const options: UserOptions = args[0]

src/main.ts

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,16 @@
11
'use strict'
22

3-
import _applyPlugin from './applyPlugin'
3+
import { applyPlugin } from './applyPlugin'
44
import { UserOptions } from './config'
5-
import { jsPDFConstructor, jsPDFDocument } from './documentHandler'
5+
import { jsPDFDocument } from './documentHandler'
66
import { CellHookData } from './HookData'
77
import { parseInput } from './inputParser'
88
import { Cell, Column, Row, Table } from './models'
99
import { createTable as _createTable } from './tableCalculator'
1010
import { drawTable as _drawTable } from './tableDrawer'
1111

1212
export type autoTableInstanceType = (options: UserOptions) => void
13-
14-
// export { applyPlugin } didn't export applyPlugin
15-
// to index.d.ts for some reason
16-
export function applyPlugin(jsPDF: jsPDFConstructor) {
17-
_applyPlugin(jsPDF)
18-
}
13+
export { applyPlugin }
1914

2015
export function autoTable(d: jsPDFDocument, options: UserOptions) {
2116
const input = parseInput(d, options)
@@ -34,17 +29,16 @@ export function __drawTable(d: jsPDFDocument, table: Table) {
3429
}
3530

3631
try {
37-
// eslint-disable-next-line @typescript-eslint/no-require-imports
38-
let jsPDF = require('jspdf')
39-
// Webpack imported jspdf instead of jsPDF for some reason
40-
// while it seemed to work everywhere else.
41-
if (jsPDF.jsPDF) jsPDF = jsPDF.jsPDF
42-
applyPlugin(jsPDF)
43-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
32+
if (typeof window !== 'undefined' && window) {
33+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
34+
const anyWindow = window as any
35+
const jsPDF = anyWindow.jsPDF || anyWindow.jspdf?.jsPDF
36+
if (jsPDF) {
37+
applyPlugin(jsPDF)
38+
}
39+
}
4440
} catch (error) {
45-
// Importing jspdf in nodejs environments does not work as of jspdf
46-
// 1.5.3 so we need to silence potential errors to support using for example
47-
// the nodejs jspdf dist files with the exported applyPlugin
41+
console.error('Could not apply autoTable plugin', error)
4842
}
4943

5044
export default autoTable

webpack.config.mjs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,6 @@ export default (env) => {
2626
"typeof globalThis !== 'undefined' ? globalThis : typeof this !== 'undefined' ? this : typeof window !== 'undefined' ? window : typeof self !== 'undefined' ? self : global ",
2727
},
2828
module: { rules: [{ test: /\.ts$/, use: [{ loader: 'ts-loader' }] }] },
29-
externals: {
30-
jspdf: {
31-
commonjs: 'jspdf',
32-
commonjs2: 'jspdf',
33-
amd: 'jspdf',
34-
root: 'jspdf',
35-
},
36-
},
3729
performance: { hints: false },
3830
devServer: {
3931
static: { directory: '.' },

0 commit comments

Comments
 (0)