Browser and Node ESM support

This commit is contained in:
SheetJS 2021-09-12 07:11:48 -04:00
parent 326b7c0ca1
commit 3d35cebe9e
14 changed files with 45497 additions and 4 deletions

@ -51,6 +51,7 @@ VueJS
WebSQL
iOS
nodejs
node.js
npm
unpkg
webpack
@ -73,12 +74,14 @@ XMLHttpRequest
bundler
bundlers
cleanroom
codepage
config
customizable
datagrid
deduplication
destructuring
embeddable
encodings
filesystem
globals
javascript
@ -93,7 +96,7 @@ runtime
serverless
submodule
transpiled
node.js
utils
commonjs
async
uncheck

@ -11,6 +11,12 @@ MINITGT=xlsx.mini.js
MINIFLOW=xlsx.mini.flow.js
MINIDEPS=$(shell cat mini.lst)
NODESMTGT=xlsx.esm.mjs
NODESMDEPS=$(shell cat misc/esm.lst)
ESMJSTGT=xlsx.mjs
ESMJSDEPS=$(shell cat misc/mjs.lst)
ULIB=$(shell echo $(LIB) | tr a-z A-Z)
DEPS=$(sort $(wildcard bits/*.js))
TARGET=$(LIB).js
@ -24,7 +30,7 @@ UGLIFYOPTS=--support-ie8 -m
## Main Targets
.PHONY: all
all: $(TARGET) $(AUXTARGETS) $(AUXSCPTS) $(MINITGT) ## Build library and auxiliary scripts
all: $(TARGET) $(AUXTARGETS) $(AUXSCPTS) $(MINITGT) $(NODESMTGT) $(ESMJSTGT) ## Build library and auxiliary scripts
$(FLOWTGTS): %.js : %.flow.js
node -e 'process.stdout.write(require("fs").readFileSync("$<","utf8").replace(/^[ \t]*\/\*[:#][^*]*\*\/\s*(\n)?/gm,"").replace(/\/\*[:#][^*]*\*\//gm,""))' > $@
@ -35,6 +41,12 @@ $(FLOWTARGET): $(DEPS)
$(MINIFLOW): $(MINIDEPS)
cat $^ | tr -d '\15\32' > $@
$(NODESMTGT): $(NODESMDEPS)
cat $^ | tr -d '\15\32' > $@
$(ESMJSTGT): $(ESMJSDEPS)
cat $^ | tr -d '\15\32' > $@
bits/01_version.js: package.json
echo "$(ULIB).version = '"`grep version package.json | awk '{gsub(/[^0-9a-z\.-]/,"",$$2); print $$2}'`"';" > $@

@ -213,7 +213,6 @@ function write_BrtFileVersion(data, o) {
write_XLWideString(XLSX.version, o);
write_XLWideString(XLSX.version, o);
write_XLWideString("7262", o);
o.length = o.l;
return o.length > o.l ? o.slice(0, o.l) : o;
}

@ -14,7 +14,7 @@ if "%1" == "help" (
npm install -g eslint eslint-plugin-html eslint-plugin-json
npm install -g mocha markdown-toc
) else if "%1" == "lint" (
eslint --ext .js,.njs,.json,.html,.htm xlsx.js xlsx.flow.js bin/xlsx.njs package.json bower.json
eslint --ext .js,.njs,.json,.html,.htm xlsx.js xlsx.flow.js bin\xlsx.njs package.json bower.json
) else if "%1" == "test" (
SET FMTS=
mocha -R spec -t 30000

5
misc/00_esmheader.js Normal file

@ -0,0 +1,5 @@
/*! xlsx.js (C) 2013-present SheetJS -- http://sheetjs.com */
/* vim: set ts=2: */
/*exported XLSX */
/*global global, exports, module, require:false, process:false, Buffer:false, ArrayBuffer:false */
var XLSX = {};

1483
misc/18_esmcfb.js Normal file

File diff suppressed because it is too large Load Diff

60
misc/19_esmfs.js Normal file

@ -0,0 +1,60 @@
import * as _fs from 'fs';
/* normalize data for blob ctor */
function blobify(data) {
if(typeof data === "string") return s2ab(data);
if(Array.isArray(data)) return a2u(data);
return data;
}
/* write or download file */
function write_dl(fname/*:string*/, payload/*:any*/, enc/*:?string*/) {
/*global IE_SaveFile, Blob, navigator, saveAs, document, File, chrome */
if(typeof _fs !== 'undefined' && _fs.writeFileSync) return enc ? _fs.writeFileSync(fname, payload, enc) : _fs.writeFileSync(fname, payload);
var data = (enc == "utf8") ? utf8write(payload) : payload;
/*:: declare var IE_SaveFile: any; */
if(typeof IE_SaveFile !== 'undefined') return IE_SaveFile(data, fname);
if(typeof Blob !== 'undefined') {
var blob = new Blob([blobify(data)], {type:"application/octet-stream"});
/*:: declare var navigator: any; */
if(typeof navigator !== 'undefined' && navigator.msSaveBlob) return navigator.msSaveBlob(blob, fname);
/*:: declare var saveAs: any; */
if(typeof saveAs !== 'undefined') return saveAs(blob, fname);
if(typeof URL !== 'undefined' && typeof document !== 'undefined' && document.createElement && URL.createObjectURL) {
var url = URL.createObjectURL(blob);
/*:: declare var chrome: any; */
if(typeof chrome === 'object' && typeof (chrome.downloads||{}).download == "function") {
if(URL.revokeObjectURL && typeof setTimeout !== 'undefined') setTimeout(function() { URL.revokeObjectURL(url); }, 60000);
return chrome.downloads.download({ url: url, filename: fname, saveAs: true});
}
var a = document.createElement("a");
if(a.download != null) {
/*:: if(document.body == null) throw new Error("unreachable"); */
a.download = fname; a.href = url; document.body.appendChild(a); a.click();
/*:: if(document.body == null) throw new Error("unreachable"); */ document.body.removeChild(a);
if(URL.revokeObjectURL && typeof setTimeout !== 'undefined') setTimeout(function() { URL.revokeObjectURL(url); }, 60000);
return url;
}
}
}
// $FlowIgnore
if(typeof $ !== 'undefined' && typeof File !== 'undefined' && typeof Folder !== 'undefined') try { // extendscript
// $FlowIgnore
var out = File(fname); out.open("w"); out.encoding = "binary";
if(Array.isArray(payload)) payload = a2s(payload);
out.write(payload); out.close(); return payload;
} catch(e) { if(!e.message || !e.message.match(/onstruct/)) throw e; }
throw new Error("cannot save file " + fname);
}
/* read binary data from file */
function read_binary(path/*:string*/) {
if(typeof _fs !== 'undefined') return _fs.readFileSync(path);
// $FlowIgnore
if(typeof $ !== 'undefined' && typeof File !== 'undefined' && typeof Folder !== 'undefined') try { // extendscript
// $FlowIgnore
var infile = File(path); infile.open("r"); infile.encoding = "binary";
var data = infile.read(); infile.close();
return data;
} catch(e) { if(!e.message || !e.message.match(/onstruct/)) throw e; }
throw new Error("Cannot access file " + path);
}

60
misc/19_mjsfs.js Normal file

@ -0,0 +1,60 @@
const _fs = void 0;
/* normalize data for blob ctor */
function blobify(data) {
if(typeof data === "string") return s2ab(data);
if(Array.isArray(data)) return a2u(data);
return data;
}
/* write or download file */
function write_dl(fname/*:string*/, payload/*:any*/, enc/*:?string*/) {
/*global IE_SaveFile, Blob, navigator, saveAs, document, File, chrome */
if(typeof _fs !== 'undefined' && _fs.writeFileSync) return enc ? _fs.writeFileSync(fname, payload, enc) : _fs.writeFileSync(fname, payload);
var data = (enc == "utf8") ? utf8write(payload) : payload;
/*:: declare var IE_SaveFile: any; */
if(typeof IE_SaveFile !== 'undefined') return IE_SaveFile(data, fname);
if(typeof Blob !== 'undefined') {
var blob = new Blob([blobify(data)], {type:"application/octet-stream"});
/*:: declare var navigator: any; */
if(typeof navigator !== 'undefined' && navigator.msSaveBlob) return navigator.msSaveBlob(blob, fname);
/*:: declare var saveAs: any; */
if(typeof saveAs !== 'undefined') return saveAs(blob, fname);
if(typeof URL !== 'undefined' && typeof document !== 'undefined' && document.createElement && URL.createObjectURL) {
var url = URL.createObjectURL(blob);
/*:: declare var chrome: any; */
if(typeof chrome === 'object' && typeof (chrome.downloads||{}).download == "function") {
if(URL.revokeObjectURL && typeof setTimeout !== 'undefined') setTimeout(function() { URL.revokeObjectURL(url); }, 60000);
return chrome.downloads.download({ url: url, filename: fname, saveAs: true});
}
var a = document.createElement("a");
if(a.download != null) {
/*:: if(document.body == null) throw new Error("unreachable"); */
a.download = fname; a.href = url; document.body.appendChild(a); a.click();
/*:: if(document.body == null) throw new Error("unreachable"); */ document.body.removeChild(a);
if(URL.revokeObjectURL && typeof setTimeout !== 'undefined') setTimeout(function() { URL.revokeObjectURL(url); }, 60000);
return url;
}
}
}
// $FlowIgnore
if(typeof $ !== 'undefined' && typeof File !== 'undefined' && typeof Folder !== 'undefined') try { // extendscript
// $FlowIgnore
var out = File(fname); out.open("w"); out.encoding = "binary";
if(Array.isArray(payload)) payload = a2s(payload);
out.write(payload); out.close(); return payload;
} catch(e) { if(!e.message || !e.message.match(/onstruct/)) throw e; }
throw new Error("cannot save file " + fname);
}
/* read binary data from file */
function read_binary(path/*:string*/) {
if(typeof _fs !== 'undefined') return _fs.readFileSync(path);
// $FlowIgnore
if(typeof $ !== 'undefined' && typeof File !== 'undefined' && typeof Folder !== 'undefined') try { // extendscript
// $FlowIgnore
var infile = File(path); infile.open("r"); infile.encoding = "binary";
var data = infile.read(); infile.close();
return data;
} catch(e) { if(!e.message || !e.message.match(/onstruct/)) throw e; }
throw new Error("Cannot access file " + path);
}

15
misc/98_esmxport.js Normal file

@ -0,0 +1,15 @@
export const version = XLSX.version;
export {
parse_xlscfb,
parse_zip,
readSync as read,
readFileSync as readFile,
readFileSync,
writeSync as write,
writeFileSync as writeFile,
writeFileSync,
writeFileAsync,
utils,
SSF,
CFB
};

79
misc/esm.lst Normal file

@ -0,0 +1,79 @@
misc/00_esmheader.js
bits/01_version.js
misc/02_codepage.js
bits/03_consts.js
bits/04_base64.js
bits/05_buf.js
bits/09_types.js
bits/10_ssf.js
bits/11_ssfutils.js
misc/18_esmcfb.js
misc/19_esmfs.js
bits/20_jsutils.js
misc/21_ziputils.js
bits/22_xmlutils.js
bits/23_binutils.js
bits/24_hoppers.js
bits/25_cellutils.js
bits/27_csfutils.js
bits/28_binstructs.js
bits/29_xlsenum.js
bits/30_ctype.js
bits/31_rels.js
bits/32_odmanrdf.js
bits/33_coreprops.js
bits/34_extprops.js
bits/35_custprops.js
bits/36_xlsprops.js
bits/38_xlstypes.js
bits/39_xlsbiff.js
bits/40_harb.js
bits/41_lotus.js
bits/42_sstxml.js
bits/43_sstbin.js
bits/44_offcrypto.js
bits/45_rtf.js
bits/46_stycommon.js
bits/47_styxml.js
bits/48_stybin.js
bits/49_theme.js
bits/50_styxls.js
bits/52_calcchain.js
bits/53_externlink.js
bits/54_drawing.js
bits/55_vml.js
bits/56_cmntcommon.js
bits/57_cmntxml.js
bits/58_cmntbin.js
bits/59_vba.js
bits/60_macrovba.js
bits/61_fcommon.js
bits/62_fxls.js
bits/63_fbin.js
bits/64_ftab.js
bits/65_fods.js
bits/66_wscommon.js
bits/67_wsxml.js
bits/68_wsbin.js
bits/69_chartxml.js
bits/70_csheet.js
bits/71_wbcommon.js
bits/72_wbxml.js
bits/73_wbbin.js
bits/74_xmlbin.js
bits/75_xlml.js
bits/76_xls.js
bits/77_parsetab.js
bits/78_writebiff.js
bits/79_html.js
bits/80_parseods.js
bits/81_writeods.js
bits/82_sheeter.js
bits/84_defaults.js
bits/85_parsezip.js
bits/86_writezip.js
bits/87_read.js
bits/88_write.js
bits/90_utils.js
bits/95_api.js
misc/98_esmxport.js

79
misc/mjs.lst Normal file

@ -0,0 +1,79 @@
misc/00_esmheader.js
bits/01_version.js
misc/02_codepage.js
bits/03_consts.js
bits/04_base64.js
bits/05_buf.js
bits/09_types.js
bits/10_ssf.js
bits/11_ssfutils.js
misc/18_esmcfb.js
misc/19_mjsfs.js
bits/20_jsutils.js
misc/21_ziputils.js
bits/22_xmlutils.js
bits/23_binutils.js
bits/24_hoppers.js
bits/25_cellutils.js
bits/27_csfutils.js
bits/28_binstructs.js
bits/29_xlsenum.js
bits/30_ctype.js
bits/31_rels.js
bits/32_odmanrdf.js
bits/33_coreprops.js
bits/34_extprops.js
bits/35_custprops.js
bits/36_xlsprops.js
bits/38_xlstypes.js
bits/39_xlsbiff.js
bits/40_harb.js
bits/41_lotus.js
bits/42_sstxml.js
bits/43_sstbin.js
bits/44_offcrypto.js
bits/45_rtf.js
bits/46_stycommon.js
bits/47_styxml.js
bits/48_stybin.js
bits/49_theme.js
bits/50_styxls.js
bits/52_calcchain.js
bits/53_externlink.js
bits/54_drawing.js
bits/55_vml.js
bits/56_cmntcommon.js
bits/57_cmntxml.js
bits/58_cmntbin.js
bits/59_vba.js
bits/60_macrovba.js
bits/61_fcommon.js
bits/62_fxls.js
bits/63_fbin.js
bits/64_ftab.js
bits/65_fods.js
bits/66_wscommon.js
bits/67_wsxml.js
bits/68_wsbin.js
bits/69_chartxml.js
bits/70_csheet.js
bits/71_wbcommon.js
bits/72_wbxml.js
bits/73_wbbin.js
bits/74_xmlbin.js
bits/75_xlml.js
bits/76_xls.js
bits/77_parsetab.js
bits/78_writebiff.js
bits/79_html.js
bits/80_parseods.js
bits/81_writeods.js
bits/82_sheeter.js
bits/84_defaults.js
bits/85_parsezip.js
bits/86_writezip.js
bits/87_read.js
bits/88_write.js
bits/90_utils.js
bits/95_api.js
misc/98_esmxport.js

@ -21,6 +21,7 @@
"xlsx": "./bin/xlsx.njs"
},
"main": "xlsx.js",
"module": "xlsx.mjs",
"unpkg": "dist/xlsx.min.js",
"jsdelivr": "dist/xlsx.min.js",
"types": "types/index.d.ts",
@ -78,6 +79,7 @@
},
"alex": {
"allow": [
"chinese",
"special",
"simple",
"just",

21848
xlsx.esm.mjs Normal file

File diff suppressed because it is too large Load Diff

21848
xlsx.mjs Normal file

File diff suppressed because it is too large Load Diff