Skip to content

Commit af138b2

Browse files
committed
refactor: remove file-saver dependency
1 parent 3c9d2de commit af138b2

File tree

3 files changed

+26
-20
lines changed

3 files changed

+26
-20
lines changed

package-lock.json

Lines changed: 0 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@
4646
"@nuxtify/core": "^0.1.8",
4747
"csv-stringify": "^6.5.2",
4848
"defu": "^6.1.4",
49-
"file-saver": "^2.0.5",
5049
"firebase": "^11.9.1",
5150
"nuxt-vuefire": "^1.0.5"
5251
},
@@ -58,7 +57,6 @@
5857
"@nuxt/module-builder": "^1.0.1",
5958
"@nuxt/schema": "^3.17.5",
6059
"@nuxt/test-utils": "^3.19.1",
61-
"@types/file-saver": "^2.0.7",
6260
"@types/node": "latest",
6361
"changelogen": "^0.6.1",
6462
"eslint": "^9.29.0",

src/runtime/utils/io.ts

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { saveAs } from 'file-saver'
21
import { stringify } from 'csv-stringify/browser/esm/sync'
32

43
/**
@@ -10,9 +9,33 @@ export function downloadCSV(data: unknown[], filename = 'export') {
109
header: true,
1110
})
1211

13-
// Download File
12+
// Create a Blob from the CSV string
1413
const blob = new Blob([csv], {
1514
type: 'text/plain;charset=utf-8',
1615
})
17-
saveAs(blob, `${filename}.csv`)
16+
17+
// Trigger the download
18+
saveBlobAsFile(blob, `${filename}.csv`)
19+
}
20+
21+
/**
22+
* Triggers a browser download for a Blob object.
23+
*/
24+
export function saveBlobAsFile(blob: Blob, filename: string) {
25+
// Create a temporary URL for the Blob
26+
const url = URL.createObjectURL(blob)
27+
28+
// Create a temporary <a> element to trigger the download
29+
const a = document.createElement('a')
30+
a.href = url
31+
a.download = filename
32+
document.body.appendChild(a)
33+
a.click()
34+
35+
// Clean up
36+
// The timeout ensures the download starts before the URL is revoked
37+
setTimeout(() => {
38+
document.body.removeChild(a)
39+
URL.revokeObjectURL(url)
40+
}, 100)
1841
}

0 commit comments

Comments
 (0)