From d3058dc45a10a3b8c9a33fc0c9b212fc16d412fb Mon Sep 17 00:00:00 2001 From: Austin Turner Date: Sun, 8 Jan 2023 15:25:15 -0700 Subject: [PATCH 1/2] Improvements to javascript and build process Added prettier for consistent code formatting and applied formatting. (This accounts for most of the code changes) Updated javascript to simplify some of the environment detection Moved import of pako to top of file Ensure that performance object in node is not fully replaced and instead just `now` is replaced Added scripts to package.json for building and testing. Added source maps - this is nice for library consumers that need to debug Updated compatibility chart. globalThis bumped node from 8 to 12. Updated build process to automatically include type definition --- .prettierrc | 5 + README.md | 168 +++++++------- package.json | 20 +- rollup.config.js | 100 ++++----- src/index.d.ts | 535 +++++++++++++++++++++++--------------------- src/index.js | 261 +++++++++++++-------- test/test-nodejs.js | 51 +++++ 7 files changed, 651 insertions(+), 489 deletions(-) create mode 100644 .prettierrc create mode 100644 test/test-nodejs.js diff --git a/.prettierrc b/.prettierrc new file mode 100644 index 0000000..abc3313 --- /dev/null +++ b/.prettierrc @@ -0,0 +1,5 @@ +{ + "printWidth": 120, + "singleQuote": false, + "trailingComma": "all" +} diff --git a/README.md b/README.md index a1d774d..d0abca5 100644 --- a/README.md +++ b/README.md @@ -16,33 +16,33 @@ Excelize-wasm is a pure WebAssembly / Javascript port of Go [Excelize](https://g ## Environment Compatibility -Browser | Version ----|--- -Chrome | ≥57 -Chrome for Android and Android Browser | ≥105 -Edge | ≥16 -Safari on macOS and iOS | ≥11 -Firefox | ≥52 -Firefox for Android | ≥104 -Opera | ≥44 -Opera Mobile | ≥64 -Samsung Internet | ≥7.2 -UC Browser for Android | ≥13.4 -QQ Browser | ≥10.4 -Node.js | ≥8.0.0 -Deno | ≥1.0 +| Browser | Version | +| -------------------------------------- | ---------- | +| Chrome | ≥71 | +| Chrome for Android and Android Browser | ≥108 | +| Edge | ≥79 | +| Safari on macOS and iOS | ≥12.2 | +| Firefox | ≥65 | +| Firefox for Android | ≥65 | +| Opera | ≥58 | +| Opera Mobile | ≥50 | +| Samsung Internet | ≥10.1 | +| UC Browser for Android | ≥13.4 | +| QQ Browser | ≥13.1 | +| Node.js | ≥12.0.0 | +| Deno | ≥1.0 | ## Basic Usage ### Installation -#### Node.js +#### Node.js or browser ```bash npm install --save excelize-wasm ``` -#### Browser +#### Browser using script tag ```html @@ -59,17 +59,17 @@ const fs = require("fs"); init("./node_modules/excelize-wasm/excelize.wasm.gz").then((excelize) => { const f = excelize.NewFile(); // Create a new sheet. - const { index } = f.NewSheet("Sheet2") + const { index } = f.NewSheet("Sheet2"); // Set value of a cell. - f.SetCellValue("Sheet2", "A2", "Hello world.") - f.SetCellValue("Sheet1", "B2", 100) + f.SetCellValue("Sheet2", "A2", "Hello world."); + f.SetCellValue("Sheet1", "B2", 100); // Set active sheet of the workbook. - f.SetActiveSheet(index) + f.SetActiveSheet(index); // Save spreadsheet by the given path. const { buffer, error } = f.WriteToBuffer(); if (error) { console.log(error); - return + return; } fs.writeFile("Book1.xlsx", buffer, "binary", (error) => { if (error) { @@ -86,42 +86,42 @@ Create spreadsheet in browser: ```html - - - - - -
- -
- + + +
+ +
+ - + + + ``` @@ -137,22 +137,22 @@ const fs = require("fs"); init("./node_modules/excelize-wasm/excelize.wasm.gz").then((excelize) => { const f = excelize.OpenReader(fs.readFileSync("Book1.xlsx")); // Set value of a cell. - var { value, error } = f.GetCellValue("Sheet1", "B2") + var { value, error } = f.GetCellValue("Sheet1", "B2"); if (error) { console.log(error); return; } - console.log(value) + console.log(value); // Get all the rows in the Sheet1. var { result, error } = f.GetRows("Sheet1"); if (error) { console.log(error); return; } - result.forEach(row => { - row.forEach(colCell => { - process.stdout.write(`${colCell}\t`) - }) + result.forEach((row) => { + row.forEach((colCell) => { + process.stdout.write(`${colCell}\t`); + }); console.log(); }); }); @@ -238,40 +238,40 @@ init("./node_modules/excelize-wasm/excelize.wasm.gz").then((excelize) => { const f = excelize.OpenReader(fs.readFileSync("Book1.xlsx")); if (f.error) { console.log(f.error); - return + return; } // Insert a picture. - var { error } = f.AddPictureFromBytes("Sheet1", "A2", - "Picture 1", ".png", fs.readFileSync("image.png"), {}) + var { error } = f.AddPictureFromBytes("Sheet1", "A2", "Picture 1", ".png", fs.readFileSync("image.png"), {}); if (error) { console.log(error); - return + return; } // Insert a picture to worksheet with scaling. - var { error } = f.AddPictureFromBytes("Sheet1", "D2", "Picture 2", ".png", - fs.readFileSync("image.jpg"), {ScaleX: 0.5, ScaleY: 0.5}); + var { error } = f.AddPictureFromBytes("Sheet1", "D2", "Picture 2", ".png", fs.readFileSync("image.jpg"), { + ScaleX: 0.5, + ScaleY: 0.5, + }); if (error) { console.log(error); - return + return; } // Insert a picture offset in the cell with printing support. - var { error } = f.AddPictureFromBytes("Sheet1", "H2", "Picture 3", ".png", - fs.readFileSync("image.gif"), { - OffsetX: 15, - OffsetY: 10, - PrintObject: true, - LockAspectRatio: false, - Locked: false + var { error } = f.AddPictureFromBytes("Sheet1", "H2", "Picture 3", ".png", fs.readFileSync("image.gif"), { + OffsetX: 15, + OffsetY: 10, + PrintObject: true, + LockAspectRatio: false, + Locked: false, }); if (error) { console.log(error); - return + return; } // Save spreadsheet by the given path. var { buffer, error } = f.WriteToBuffer(); if (error) { console.log(error); - return + return; } fs.writeFile("Book1.xlsx", buffer, "binary", (error) => { if (error) { @@ -285,6 +285,16 @@ init("./node_modules/excelize-wasm/excelize.wasm.gz").then((excelize) => { Contributions are welcome! Open a pull request to fix a bug, or open an issue to discuss a new feature or change. +### Build locally + +Ensure you have node and go installed. + +Install all dependencies with `npm install` and build with `npm run build`. This will compile the javascript to three different build targets and will build and generate the wasm gzip file. + +To build just the JS, you can run `npm run build:js` + +Run tests by running `npm run test` + ## Licenses This program is under the terms of the BSD 3-Clause License. See [https://opensource.org/licenses/BSD-3-Clause](https://opensource.org/licenses/BSD-3-Clause). diff --git a/package.json b/package.json index c7cb68f..d9b2651 100644 --- a/package.json +++ b/package.json @@ -17,12 +17,22 @@ "script": "./dist/index.js", "default": "./dist/main.js" }, + "scripts": { + "build": "npm-run-all clean build:js build:go", + "build:js": "rollup -c", + "build:go": "cd cmd && GOOS=js GOARCH=wasm go build -o ../dist/excelize.wasm && cd ../dist && gzip -9 -v -c excelize.wasm > excelize.wasm.gz && rm excelize.wasm", + "clean": "rm -rf dist", + "test": "node test/test-nodejs.js" + }, "files": [ "excelize.wasm.gz", "index.d.ts", "index.js", + "index.js.map", "main.cjs", - "main.js" + "main.cjs.map", + "main.js", + "main.js.map" ], "types": "index.d.ts", "typings": "index.d.ts", @@ -71,7 +81,11 @@ "@rollup/plugin-node-resolve": "15.0.1", "@rollup/plugin-terser": "0.2.1", "pako": "2.1.0", - "rollup-plugin-polyfill-node": "0.11.0", - "rollup": "3.8.0" + "rollup": "3.8.0", + "rollup-plugin-polyfill-node": "0.11.0" + }, + "dependencies": { + "npm-run-all": "^4.1.5", + "rollup-plugin-dts": "^5.1.1" } } diff --git a/rollup.config.js b/rollup.config.js index 95fd706..421a038 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -1,58 +1,52 @@ -import commonjs from '@rollup/plugin-commonjs'; -import nodePolyfills from 'rollup-plugin-polyfill-node'; -import { nodeResolve } from '@rollup/plugin-node-resolve'; -import terser from '@rollup/plugin-terser'; -import pkg from './package.json' assert {type: 'json'}; -const input = 'src/index.js'; +import commonjs from "@rollup/plugin-commonjs"; +import { nodeResolve } from "@rollup/plugin-node-resolve"; +import terser from "@rollup/plugin-terser"; +import dts from "rollup-plugin-dts"; +import nodePolyfills from "rollup-plugin-polyfill-node"; +import pkg from "./package.json" assert { type: "json" }; + +const input = "src/index.js"; export default [ - { - // Plain browser