Skip to content

Commit 2dd7540

Browse files
authored
fix: lazy import file-saver
1 parent 9c8e8d8 commit 2dd7540

File tree

5 files changed

+8
-4
lines changed

5 files changed

+8
-4
lines changed

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,4 +142,4 @@ const excel = useExcelJS({
142142

143143

144144
## Optimization
145-
The hook lazily imports the rather large `exceljs` package only once the `download` function is executed.
145+
The hook lazily imports `file-saver` and the rather large `exceljs` package only once the `download` function is executed.

src/deps.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { saveAs } from "file-saver"
2+
import ExcelJS from 'exceljs'
3+
4+
export default { saveAs , ExcelJS}

src/makeBuffer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export default async function makeBuffer<T extends Array<Sheet>>({
99
data: Array<any> | Record<T[number]["name"], Array<any>>
1010
intercept?: InterceptFn
1111
}) {
12-
const ExcelJS = await import("exceljs")
12+
const { default: {ExcelJS} } = await import("./deps")
1313
let workbook = new ExcelJS.Workbook()
1414
for (const worksheet of worksheets) {
1515
const sheet = workbook.addWorksheet(worksheet.name)

src/react-use-exceljs.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import React from "react"
2-
import { saveAs } from "file-saver"
32
import { type Data, type Filename, type InterceptFn, type Sheet } from "./types"
43
import makeBuffer from "./makeBuffer"
54

@@ -18,6 +17,7 @@ export default function useExcelJS<T extends Array<Sheet>>({
1817
const buffer = await makeBuffer({ worksheets, data, intercept })
1918
const fileType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
2019
const blob = new Blob([buffer], { type: fileType })
20+
const { default: {saveAs} } = await import("./deps")
2121
saveAs(blob, filename ?? "workbook.xlsx")
2222
},
2323
[filename, worksheets, intercept]

test/makeBuffer.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ test("can create two sheets", async () => {
6262
{ city: 'Dallas' },
6363
{ city: 'New York' },
6464
{ city: 'Miami' },
65-
]
65+
],
6666
},
6767
worksheets: [worksheetA, worksheetB],
6868
})

0 commit comments

Comments
 (0)