In an Angular 8 app, my component has a table that can be exported to an excel file. I'm using the library xlsx for that. The webpack-bundle-analyzer shows that the xlsx library uses 65% of my total chunk size.
As the export feature is used quite rarely, I'd like to exclude it from the initial bundle to increase performance for the majority of the users and load it when the user clicks on "export" and they need to wait for the download anyway.
This is what I currently have:
import * as xlsx from 'xlsx';
...
exportToExcel(filename, worksheet) {
const ws: xlsx.WorkSheet = xlsx.utils.json_to_sheet(this.tableData);
const wb: xlsx.WorkBook = xlsx.utils.book_new();
xlsx.utils.book_append_sheet(wb, ws, worksheet);
xlsx.writeFile(wb, filename+'.xlsx');
}
I would imagine something like this:
exportToExcel(filename, worksheet) {
loadScript('/scripts/xlsx.js').then(xlsx => {
const ws: xlsx.WorkSheet = xlsx.utils.json_to_sheet(this.tableData);
const wb: xlsx.WorkBook = xlsx.utils.book_new();
xlsx.utils.book_append_sheet(wb, ws, worksheet);
xlsx.writeFile(wb, filename+'.xlsx');
})
}
Is there a way to do that? Or do you have any other suggestions?
Something like this may help:
From PrimeNg - Export