docs.sheetjs.com/docz/docs/03-demos/01-frontend/19-bundler/index.md

655 lines
17 KiB
Markdown
Raw Normal View History

2022-07-17 03:47:27 +00:00
---
title: Bundlers
2023-01-10 00:31:37 +00:00
pagination_prev: demos/index
2023-02-28 11:40:44 +00:00
pagination_next: demos/grid/index
2023-10-18 02:07:06 +00:00
sidebar_position: 19
2023-01-09 05:08:30 +00:00
sidebar_custom_props:
skip: 1
2022-07-17 03:47:27 +00:00
---
import current from '/version.js';
import Tabs from '@theme/Tabs';
import TabItem from '@theme/TabItem';
2023-04-29 11:21:37 +00:00
import CodeBlock from '@theme/CodeBlock';
2023-10-19 05:23:55 +00:00
import {useCurrentSidebarCategory} from '@docusaurus/theme-common';
2022-07-17 03:47:27 +00:00
2023-10-18 02:07:06 +00:00
SheetJS predates ECMAScript modules and most bundler tools. As best practices
have evolved, stress testing SheetJS libraries have revealed bugs in bundlers
and other tools. This demo collects various notes and provides basic examples.
2022-07-20 08:58:29 +00:00
2023-09-22 06:32:55 +00:00
:::note pass
2022-07-20 08:58:29 +00:00
Issues should be reported to the respective bundler projects. Typically it is
considered a bundler bug if the tool cannot properly handle JS libraries.
:::
2023-10-19 05:23:55 +00:00
The following tools are covered in separate pages:
<ul>{useCurrentSidebarCategory().items.filter(item => !item?.customProps?.skip).map((item, index) => {
const listyle = (item.customProps?.icon) ? {
listStyleImage: `url("${item.customProps.icon}")`
} : {};
return (<li style={listyle} {...(item.customProps?.class ? {className: item.customProps.class}: {})}>
<a href={item.href}>{item.label}</a>{item.customProps?.summary && (" - " + item.customProps.summary)}
</li>);
})}</ul>
2022-07-20 08:58:29 +00:00
2023-10-21 11:15:28 +00:00
#### Browserify
2022-07-22 03:26:52 +00:00
2023-10-21 11:15:28 +00:00
**[The exposition has been moved to a separate page.](/docs/demos/frontend/bundler/browserify)**
2022-07-22 03:26:52 +00:00
2022-07-20 08:58:29 +00:00
## Bun
`bun bun` is capable of optimizing imported libraries in `node_modules`. In
local testing, a bundled script can save tens of milliseconds per run.
<details><summary><b>Complete Example</b> (click to show)</summary>
2023-05-09 08:08:01 +00:00
:::note
This demo was last tested on 2023 May 07 against Bun `0.5.9`
:::
2022-07-20 08:58:29 +00:00
1) Install the tarball using a package manager:
2023-05-07 13:58:36 +00:00
<Tabs groupId="pm">
2022-07-20 08:58:29 +00:00
<TabItem value="npm" label="npm">
2023-05-07 13:58:36 +00:00
<CodeBlock language="bash">{`\
2022-08-07 07:48:40 +00:00
npm i --save https://cdn.sheetjs.com/xlsx-${current}/xlsx-${current}.tgz`}
2023-05-07 13:58:36 +00:00
</CodeBlock>
2022-07-20 08:58:29 +00:00
</TabItem>
<TabItem value="pnpm" label="pnpm">
2023-05-07 13:58:36 +00:00
<CodeBlock language="bash">{`\
2022-08-07 07:48:40 +00:00
pnpm install https://cdn.sheetjs.com/xlsx-${current}/xlsx-${current}.tgz`}
2023-05-07 13:58:36 +00:00
</CodeBlock>
2022-07-20 08:58:29 +00:00
</TabItem>
<TabItem value="yarn" label="Yarn" default>
2023-05-07 13:58:36 +00:00
<CodeBlock language="bash">{`\
2022-08-07 07:48:40 +00:00
yarn add https://cdn.sheetjs.com/xlsx-${current}/xlsx-${current}.tgz`}
2023-05-07 13:58:36 +00:00
</CodeBlock>
2022-07-20 08:58:29 +00:00
</TabItem>
</Tabs>
2) Save the following script to `bun.js`:
```js title="bun.js"
// highlight-next-line
2023-01-09 05:08:30 +00:00
import * as XLSX from 'xlsx';
2022-07-20 08:58:29 +00:00
// highlight-next-line
import * as fs from 'fs';
// highlight-next-line
XLSX.set_fs(fs);
/* fetch JSON data and parse */
2022-08-21 19:43:30 +00:00
const url = "https://sheetjs.com/data/executive.json";
2022-07-20 08:58:29 +00:00
const raw_data = await (await fetch(url)).json();
/* filter for the Presidents */
const prez = raw_data.filter((row) => row.terms.some((term) => term.type === "prez"));
/* flatten objects */
const rows = prez.map((row) => ({
name: row.name.first + " " + row.name.last,
birthday: row.bio.birthday
}));
/* generate worksheet and workbook */
const worksheet = XLSX.utils.json_to_sheet(rows);
const workbook = XLSX.utils.book_new();
XLSX.utils.book_append_sheet(workbook, worksheet, "Dates");
/* fix headers */
XLSX.utils.sheet_add_aoa(worksheet, [["Name", "Birthday"]], { origin: "A1" });
/* calculate column width */
const max_width = rows.reduce((w, r) => Math.max(w, r.name.length), 10);
worksheet["!cols"] = [ { wch: max_width } ];
/* create an XLSX file and try to save to Presidents.xlsx */
XLSX.writeFile(workbook, "Presidents.xlsx");
```
3) Bundle the script with `bun bun`:
```bash
bun bun bun.js
```
This procedure will generate `node_modules.bun`.
4) Run the script
```bash
bun bun.js
```
</details>
2022-07-22 03:26:52 +00:00
2022-08-30 22:12:52 +00:00
## Dojo
2022-10-30 05:45:37 +00:00
Integration details are included [in the "AMD" installation](/docs/getting-started/installation/amd#dojo-toolkit)
2022-08-30 22:12:52 +00:00
2023-01-09 17:25:32 +00:00
Complete Examples are included [in the "Dojo" demo](/docs/demos/frontend/legacy#dojo-toolkit)
2022-08-30 22:12:52 +00:00
2023-10-20 09:35:44 +00:00
#### esbuild
2022-08-30 22:12:52 +00:00
2023-10-20 09:35:44 +00:00
**[The exposition has been moved to a separate page.](/docs/demos/frontend/bundler/esbuild)**
2023-05-29 02:52:54 +00:00
2022-07-17 03:47:27 +00:00
## Parcel
2022-08-25 08:22:28 +00:00
Parcel should play nice with SheetJS out of the box.
2022-07-17 03:47:27 +00:00
2022-07-20 08:58:29 +00:00
:::warning Parcel Bug
2022-10-19 10:05:59 +00:00
Errors of the form `Could not statically evaluate fs call` stem from a Parcel
bug. Upgrade to Parcel version 1.5.0 or later.
2022-07-17 03:47:27 +00:00
2022-07-20 08:58:29 +00:00
:::
2022-07-17 03:47:27 +00:00
<details><summary><b>Complete Example</b> (click to show)</summary>
2023-05-09 08:08:01 +00:00
:::note
This demo was last tested on 2023 May 07 against parcel `2.8.3`
:::
2022-10-30 05:45:37 +00:00
This demo follows the [Presidents Example](/docs/getting-started/example).
2022-07-17 03:47:27 +00:00
1) Save the following to `index.html`:
```html title="index.html"
<body>
<h3>SheetJS <span id="vers"></span> export demo</h3>
<button id="xport">Click to Export!</button>
<!-- the script tag must be marked as `type="module"` -->
<!-- highlight-next-line -->
<script type="module">
// ESM-style import from "xlsx"
// highlight-next-line
2022-07-26 10:49:14 +00:00
import { utils, version, writeFileXLSX } from 'xlsx';
2022-07-17 03:47:27 +00:00
document.getElementById("vers").innerText = version;
document.getElementById("xport").onclick = async() => {
/* fetch JSON data and parse */
2022-08-21 19:43:30 +00:00
const url = "https://sheetjs.com/data/executive.json";
2022-07-17 03:47:27 +00:00
const raw_data = await (await fetch(url)).json();
/* filter for the Presidents */
const prez = raw_data.filter(row => row.terms.some(term => term.type === "prez"));
/* flatten objects */
const rows = prez.map(row => ({
name: row.name.first + " " + row.name.last,
birthday: row.bio.birthday
}));
/* generate worksheet and workbook */
const worksheet = utils.json_to_sheet(rows);
const workbook = utils.book_new();
utils.book_append_sheet(workbook, worksheet, "Dates");
/* fix headers */
utils.sheet_add_aoa(worksheet, [["Name", "Birthday"]], { origin: "A1" });
/* calculate column width */
const max_width = rows.reduce((w, r) => Math.max(w, r.name.length), 10);
worksheet["!cols"] = [ { wch: max_width } ];
/* create an XLSX file and try to save to Presidents.xlsx */
2022-07-26 10:49:14 +00:00
writeFileXLSX(workbook, "Presidents.xlsx");
2022-07-17 03:47:27 +00:00
};
</script>
<body>
```
2) Install the SheetJS node module:
2023-05-07 13:58:36 +00:00
<Tabs groupId="pm">
2022-07-17 03:47:27 +00:00
<TabItem value="npm" label="npm">
2023-05-07 13:58:36 +00:00
<CodeBlock language="bash">{`\
2022-08-07 07:48:40 +00:00
npm i --save https://cdn.sheetjs.com/xlsx-${current}/xlsx-${current}.tgz`}
2023-05-07 13:58:36 +00:00
</CodeBlock>
2022-07-17 03:47:27 +00:00
</TabItem>
<TabItem value="pnpm" label="pnpm">
2023-05-07 13:58:36 +00:00
<CodeBlock language="bash">{`\
2022-08-07 07:48:40 +00:00
pnpm install https://cdn.sheetjs.com/xlsx-${current}/xlsx-${current}.tgz`}
2023-05-07 13:58:36 +00:00
</CodeBlock>
2022-07-17 03:47:27 +00:00
</TabItem>
<TabItem value="yarn" label="Yarn" default>
2023-05-07 13:58:36 +00:00
<CodeBlock language="bash">{`\
2022-08-07 07:48:40 +00:00
yarn add https://cdn.sheetjs.com/xlsx-${current}/xlsx-${current}.tgz`}
2023-05-07 13:58:36 +00:00
</CodeBlock>
2022-07-17 03:47:27 +00:00
</TabItem>
</Tabs>
3) Run the Parcel CLI tool:
```bash
2023-05-09 08:08:01 +00:00
npx -y parcel@2.8.3 index.html
2022-07-17 03:47:27 +00:00
```
4) Access the page listed in the output (typically `http://localhost:1234`) and
click the "Click to Export!" button to generate a file.
2022-07-25 20:48:10 +00:00
</details>
2023-10-21 11:15:28 +00:00
#### RequireJS
2022-07-28 05:36:09 +00:00
2023-10-19 05:23:55 +00:00
**[The exposition has been moved to a separate page.](/docs/demos/frontend/bundler/requirejs)**
2023-05-09 08:08:01 +00:00
2022-07-31 18:48:02 +00:00
## Rollup
Rollup requires `@rollup/plugin-node-resolve` to support NodeJS modules:
<details><summary><b>Complete Example</b> (click to show)</summary>
2023-05-09 08:08:01 +00:00
:::note
This demo was last tested on 2023 May 07 against Rollup 3.21.5
:::
2022-07-31 18:48:02 +00:00
1) Install the tarball using a package manager:
2023-05-07 13:58:36 +00:00
<Tabs groupId="pm">
2022-07-31 18:48:02 +00:00
<TabItem value="npm" label="npm">
2023-05-07 13:58:36 +00:00
<CodeBlock language="bash">{`\
2023-05-09 08:08:01 +00:00
npm i --save https://cdn.sheetjs.com/xlsx-${current}/xlsx-${current}.tgz`} rollup@3.21.5 @rollup/plugin-node-resolve
2023-05-07 13:58:36 +00:00
</CodeBlock>
2022-07-31 18:48:02 +00:00
</TabItem>
<TabItem value="pnpm" label="pnpm">
2023-05-07 13:58:36 +00:00
<CodeBlock language="bash">{`\
2023-05-09 08:08:01 +00:00
pnpm install https://cdn.sheetjs.com/xlsx-${current}/xlsx-${current}.tgz`} rollup@3.21.5 @rollup/plugin-node-resolve
2023-05-07 13:58:36 +00:00
</CodeBlock>
2022-07-31 18:48:02 +00:00
</TabItem>
<TabItem value="yarn" label="Yarn" default>
2023-05-07 13:58:36 +00:00
<CodeBlock language="bash">{`\
2023-05-09 08:08:01 +00:00
yarn add https://cdn.sheetjs.com/xlsx-${current}/xlsx-${current}.tgz`} rollup@3.21.5 @rollup/plugin-node-resolve
2023-05-07 13:58:36 +00:00
</CodeBlock>
2022-07-31 18:48:02 +00:00
</TabItem>
</Tabs>
2) Save the following to `index.js`:
```js title="index.js"
// highlight-next-line
2023-01-09 05:08:30 +00:00
import { utils, version, writeFileXLSX } from 'xlsx';
2022-07-31 18:48:02 +00:00
document.getElementById("xport").addEventListener("click", async() => {
/* fetch JSON data and parse */
2022-08-21 19:43:30 +00:00
const url = "https://sheetjs.com/data/executive.json";
2022-07-31 18:48:02 +00:00
const raw_data = await (await fetch(url)).json();
/* filter for the Presidents */
const prez = raw_data.filter(row => row.terms.some(term => term.type === "prez"));
/* flatten objects */
const rows = prez.map(row => ({
name: row.name.first + " " + row.name.last,
birthday: row.bio.birthday
}));
/* generate worksheet and workbook */
const worksheet = utils.json_to_sheet(rows);
const workbook = utils.book_new();
utils.book_append_sheet(workbook, worksheet, "Dates");
/* fix headers */
utils.sheet_add_aoa(worksheet, [["Name", "Birthday"]], { origin: "A1" });
/* calculate column width */
const max_width = rows.reduce((w, r) => Math.max(w, r.name.length), 10);
worksheet["!cols"] = [ { wch: max_width } ];
/* create an XLSX file and try to save to Presidents.xlsx */
writeFileXLSX(workbook, "Presidents.xlsx");
});
```
3) Bundle the script:
```bash
npx rollup index.js --plugin @rollup/plugin-node-resolve --file bundle.js --format iife
```
4) Create a small HTML page that loads the script. Save to `index.html`:
```html title="index.html"
<!DOCTYPE html>
<html lang="en">
<head></head>
<body>
<h1>SheetJS Presidents Demo</h1>
<button id="xport">Click here to export</button>
<script type="module" src="./bundle.js"></script>
</body>
</html>
```
2023-05-03 03:40:40 +00:00
5) Start a local HTTP server, then go to `http://localhost:8080/`
2022-07-31 18:48:02 +00:00
```bash
npx http-server .
```
Click on "Click here to export" to generate a file.
</details>
2022-07-25 20:48:10 +00:00
## Snowpack
Snowpack works with no caveats.
<details><summary><b>Complete Example</b> (click to show)</summary>
2023-05-09 08:08:01 +00:00
:::note
This demo was last tested on 2023 May 07 against Snowpack `3.8.8`
:::
2022-07-25 20:48:10 +00:00
1) Install the tarball using a package manager:
2023-05-07 13:58:36 +00:00
<Tabs groupId="pm">
2022-07-25 20:48:10 +00:00
<TabItem value="npm" label="npm">
2023-05-07 13:58:36 +00:00
<CodeBlock language="bash">{`\
2022-08-07 07:48:40 +00:00
npm i --save https://cdn.sheetjs.com/xlsx-${current}/xlsx-${current}.tgz`}
2023-05-07 13:58:36 +00:00
</CodeBlock>
2022-07-25 20:48:10 +00:00
</TabItem>
<TabItem value="pnpm" label="pnpm">
2023-05-07 13:58:36 +00:00
<CodeBlock language="bash">{`\
2022-08-07 07:48:40 +00:00
pnpm install https://cdn.sheetjs.com/xlsx-${current}/xlsx-${current}.tgz`}
2023-05-07 13:58:36 +00:00
</CodeBlock>
2022-07-25 20:48:10 +00:00
</TabItem>
<TabItem value="yarn" label="Yarn" default>
2023-05-07 13:58:36 +00:00
<CodeBlock language="bash">{`\
2022-08-07 07:48:40 +00:00
yarn add https://cdn.sheetjs.com/xlsx-${current}/xlsx-${current}.tgz`}
2023-05-07 13:58:36 +00:00
</CodeBlock>
2022-07-25 20:48:10 +00:00
</TabItem>
</Tabs>
2) Save the following to `index.js`:
```js title="index.js"
// highlight-next-line
2023-01-09 05:08:30 +00:00
import { utils, version, writeFileXLSX } from 'xlsx';
2022-07-25 20:48:10 +00:00
document.getElementById("xport").addEventListener("click", async() => {
/* fetch JSON data and parse */
2022-08-21 19:43:30 +00:00
const url = "https://sheetjs.com/data/executive.json";
2022-07-25 20:48:10 +00:00
const raw_data = await (await fetch(url)).json();
/* filter for the Presidents */
const prez = raw_data.filter(row => row.terms.some(term => term.type === "prez"));
/* flatten objects */
const rows = prez.map(row => ({
name: row.name.first + " " + row.name.last,
birthday: row.bio.birthday
}));
/* generate worksheet and workbook */
const worksheet = utils.json_to_sheet(rows);
const workbook = utils.book_new();
utils.book_append_sheet(workbook, worksheet, "Dates");
/* fix headers */
utils.sheet_add_aoa(worksheet, [["Name", "Birthday"]], { origin: "A1" });
/* calculate column width */
const max_width = rows.reduce((w, r) => Math.max(w, r.name.length), 10);
worksheet["!cols"] = [ { wch: max_width } ];
/* create an XLSX file and try to save to Presidents.xlsx */
2022-07-26 10:49:14 +00:00
writeFileXLSX(workbook, "Presidents.xlsx");
2022-07-25 20:48:10 +00:00
});
```
3) Create a small HTML page that loads the script. Save to `index.html`:
```html title="index.html"
<!DOCTYPE html>
<html lang="en">
<head></head>
<body>
<h1>SheetJS Presidents Demo</h1>
<button id="xport">Click here to export</button>
<script type="module" src="./index.js"></script>
</body>
</html>
```
2023-09-22 06:32:55 +00:00
:::note pass
2022-07-25 20:48:10 +00:00
Unlike other bundlers, Snowpack requires a full page including `HEAD` element.
:::
4) Build for production:
```bash
2023-05-09 08:08:01 +00:00
npx snowpack@3.8.8 build
2022-07-25 20:48:10 +00:00
```
2023-05-03 03:40:40 +00:00
5) Start a local HTTP server, then go to `http://localhost:8080/`
2022-07-25 20:48:10 +00:00
```bash
npx http-server build/
```
Click on "Click here to export" to generate a file.
</details>
2022-07-26 10:49:14 +00:00
2023-10-21 11:15:28 +00:00
#### SWC
2023-05-09 08:08:01 +00:00
2023-10-21 11:15:28 +00:00
**[The exposition has been moved to a separate page.](/docs/demos/frontend/bundler/swcpack)**
2022-07-31 09:22:30 +00:00
2023-10-19 05:23:55 +00:00
#### SystemJS
2022-08-07 01:57:38 +00:00
2023-10-19 05:23:55 +00:00
**[The exposition has been moved to a separate page.](/docs/demos/frontend/bundler/systemjs)**
2022-08-07 01:57:38 +00:00
2022-07-26 10:49:14 +00:00
## Vite
2023-04-14 08:13:40 +00:00
ViteJS is compatible with SheetJS versions starting from 0.18.10.
2022-07-26 10:49:14 +00:00
<details><summary><b>Complete Example</b> (click to show)</summary>
2023-05-09 08:08:01 +00:00
:::note
This demo was last tested on 2023 May 07 against ViteJS `4.3.5`
:::
2022-07-26 10:49:14 +00:00
1) Create a new ViteJS project:
2023-08-20 20:39:35 +00:00
```bash
2023-05-09 08:08:01 +00:00
npm create vite@latest sheetjs-vite -- --template vue-ts
cd sheetjs-vite
2022-07-26 10:49:14 +00:00
npm i
```
2) Add the SheetJS dependency:
2023-05-07 13:58:36 +00:00
<CodeBlock language="bash">{`\
2022-08-07 07:48:40 +00:00
npm i --save https://cdn.sheetjs.com/xlsx-${current}/xlsx-${current}.tgz`}
2023-05-07 13:58:36 +00:00
</CodeBlock>
2022-07-26 10:49:14 +00:00
3) Replace `src\components\HelloWorld.vue` with:
```html title="src\components\HelloWorld.vue"
<script setup lang="ts">
import { version, utils, writeFileXLSX } from 'xlsx';
interface President {
terms: { "type": "prez" | "viceprez"; }[];
name: { first: string; last: string; }
bio: { birthday: string; }
}
async function xport() {
/* fetch JSON data and parse */
2022-08-21 19:43:30 +00:00
const url = "https://sheetjs.com/data/executive.json";
2022-07-26 10:49:14 +00:00
const raw_data: President[] = await (await fetch(url)).json();
/* filter for the Presidents */
const prez = raw_data.filter(row => row.terms.some(term => term.type === "prez"));
/* flatten objects */
const rows = prez.map(row => ({
name: row.name.first + " " + row.name.last,
birthday: row.bio.birthday
}));
/* generate worksheet and workbook */
const worksheet = utils.json_to_sheet(rows);
const workbook = utils.book_new();
utils.book_append_sheet(workbook, worksheet, "Dates");
/* fix headers */
utils.sheet_add_aoa(worksheet, [["Name", "Birthday"]], { origin: "A1" });
/* calculate column width */
const max_width = rows.reduce((w, r) => Math.max(w, r.name.length), 10);
worksheet["!cols"] = [ { wch: max_width } ];
/* create an XLSX file and try to save to Presidents.xlsx */
writeFileXLSX(workbook, "Presidents.xlsx");
}
</script>
<template>
<button type="button" @click="xport">Export with SheetJS version {{ version }}</button>
</template>
```
4) Run `npm run dev` and test functionality by opening a web browser to
2023-05-09 08:08:01 +00:00
`http://localhost:5173/` and clicking the button
2022-07-26 10:49:14 +00:00
5) Run `npx vite build` and verify the generated pages work by running a local
web server in the `dist` folder:
2023-08-20 20:39:35 +00:00
```bash
2022-07-26 10:49:14 +00:00
npx http-server dist/
```
2023-05-09 08:08:01 +00:00
Access `http://localhost:8080` in your web browser and click the export button.
2022-07-26 10:49:14 +00:00
</details>
2023-09-11 04:44:15 +00:00
:::note pass
2022-10-18 22:33:42 +00:00
2023-01-15 03:36:13 +00:00
The [Vite section of the Content demo](/docs/demos/static/vitejs) covers asset
2022-10-31 00:58:49 +00:00
loaders. They are ideal for static sites pulling data from sheets at build time.
2022-10-18 22:33:42 +00:00
:::
2023-10-18 02:07:06 +00:00
#### Webpack
2022-08-07 07:48:40 +00:00
2023-10-18 02:07:06 +00:00
**[The exposition has been moved to a separate page.](/docs/demos/frontend/bundler/webpack)**
2023-05-25 01:36:15 +00:00
2022-07-31 18:48:02 +00:00
## WMR
2023-05-09 08:08:01 +00:00
WMR works with no caveats.
2022-07-31 18:48:02 +00:00
<details><summary><b>Complete Example</b> (click to show)</summary>
2023-05-09 08:08:01 +00:00
:::note
This demo was last tested on 2023 May 07 against WMR `3.8.0`
:::
2022-07-31 18:48:02 +00:00
1) Install the tarball using a package manager:
2023-05-07 13:58:36 +00:00
<Tabs groupId="pm">
2022-07-31 18:48:02 +00:00
<TabItem value="npm" label="npm">
2023-05-07 13:58:36 +00:00
<CodeBlock language="bash">{`\
2022-08-07 07:48:40 +00:00
npm i --save https://cdn.sheetjs.com/xlsx-${current}/xlsx-${current}.tgz`}
2023-05-07 13:58:36 +00:00
</CodeBlock>
2022-07-31 18:48:02 +00:00
</TabItem>
<TabItem value="pnpm" label="pnpm">
2023-05-07 13:58:36 +00:00
<CodeBlock language="bash">{`\
2022-08-07 07:48:40 +00:00
pnpm install https://cdn.sheetjs.com/xlsx-${current}/xlsx-${current}.tgz`}
2023-05-07 13:58:36 +00:00
</CodeBlock>
2022-07-31 18:48:02 +00:00
</TabItem>
<TabItem value="yarn" label="Yarn" default>
2023-05-07 13:58:36 +00:00
<CodeBlock language="bash">{`\
2022-08-07 07:48:40 +00:00
yarn add https://cdn.sheetjs.com/xlsx-${current}/xlsx-${current}.tgz`}
2023-05-07 13:58:36 +00:00
</CodeBlock>
2022-07-31 18:48:02 +00:00
</TabItem>
</Tabs>
2) Save the following to `index.js`:
```js title="index.js"
// highlight-next-line
2023-01-09 05:08:30 +00:00
import { utils, version, writeFileXLSX } from 'xlsx';
2022-07-31 18:48:02 +00:00
document.getElementById("xport").addEventListener("click", async() => {
/* fetch JSON data and parse */
2022-08-21 19:43:30 +00:00
const url = "https://sheetjs.com/data/executive.json";
2022-07-31 18:48:02 +00:00
const raw_data = await (await fetch(url)).json();
/* filter for the Presidents */
const prez = raw_data.filter(row => row.terms.some(term => term.type === "prez"));
/* flatten objects */
const rows = prez.map(row => ({
name: row.name.first + " " + row.name.last,
birthday: row.bio.birthday
}));
/* generate worksheet and workbook */
const worksheet = utils.json_to_sheet(rows);
const workbook = utils.book_new();
utils.book_append_sheet(workbook, worksheet, "Dates");
/* fix headers */
utils.sheet_add_aoa(worksheet, [["Name", "Birthday"]], { origin: "A1" });
/* calculate column width */
const max_width = rows.reduce((w, r) => Math.max(w, r.name.length), 10);
worksheet["!cols"] = [ { wch: max_width } ];
/* create an XLSX file and try to save to Presidents.xlsx */
writeFileXLSX(workbook, "Presidents.xlsx");
});
```
3) Create a small HTML page that loads the script. Save to `index.html`:
```html title="index.html"
<!DOCTYPE html>
<html lang="en">
<head></head>
<body>
<h1>SheetJS Presidents Demo</h1>
<button id="xport">Click here to export</button>
<script type="module" src="./index.js"></script>
</body>
</html>
```
4) Build for production:
```bash
2023-05-09 08:08:01 +00:00
npx wmr@3.8.0 build
2022-07-31 18:48:02 +00:00
```
2023-05-03 03:40:40 +00:00
5) Start a local HTTP server in `dist` folder and go to `http://localhost:8080/`
2022-07-31 18:48:02 +00:00
```bash
npx http-server dist/
```
Click on "Click here to export" to generate a file.
</details>