sheetjs_sheetjs/demos/systemjs
2019-11-14 20:46:49 -05:00
..
.gitignore systemjs node/browser examples [ci skip] 2017-05-19 12:46:08 -04:00
app.node.js Math.LOG2E precision issue + new demos [ci skip] 2017-09-05 01:34:30 -04:00
main.js version bump 0.12.4: zip cleanup 2018-03-05 19:34:04 -05:00
main.simple.js version bump 0.12.5: ancillary utility update 2018-03-12 22:51:54 -04:00
Makefile version bump 0.12.5: ancillary utility update 2018-03-12 22:51:54 -04:00
README.md version bump 0.15.2: actually pass lint tests 2019-11-14 20:46:49 -05:00
simple.html version bump 0.12.5: ancillary utility update 2018-03-12 22:51:54 -04:00
systemjs.html version bump 0.12.4: zip cleanup 2018-03-05 19:34:04 -05:00
test.node.js demo refresh [ci skip] 2017-09-24 19:40:09 -04:00
worker.js demo refresh [ci skip] 2017-09-24 19:40:09 -04:00
xlsxworker.js demo refresh [ci skip] 2017-09-24 19:40:09 -04:00

SystemJS Demos

SystemJS supports both browser and nodejs deployments. It does not recognize browser environments and automatically suppress node core modules, but with some configuration magic SystemJS can load the library.

Browser

SystemJS fails by default because the library does not export anything in the web browser. The meta configuration option can be used to expose XLSX:

SystemJS.config({
	meta: {
		'xlsx': {
			exports: 'XLSX' // <-- tell SystemJS to expose the XLSX variable
		}
	},
	map: {
		'xlsx': 'xlsx.full.min.js', // <-- make sure xlsx.full.min.js is in same dir
		'fs': '',     // <--|
		'crypto': '', // <--| suppress native node modules
		'stream': ''  // <--|
	}
});
SystemJS.import('main.js')

With the new configuration, require('xlsx') will load the library:

var XLSX = require('xlsx');
var w = XLSX.read('abc,def\nghi,jkl', {type:'binary'});
var j = XLSX.utils.sheet_to_json(w.Sheets[w.SheetNames[0]], {header:1});
console.log(j);

Note: The readFile and writeFile functions are not available in the browser.

Web Workers

Web Workers can load the SystemJS library with importScripts, but the imported code cannot assign the original worker's onmessage callback. This demo works around the limitation by exposing the desired function as a global:

/* main worker script */
importScripts('system.js');

SystemJS.config({ /* ... browser config ... */ });

onmessage = function(evt) {
	SystemJS.import('xlsxworker.js').then(function() { _cb(evt); });
};

/* xlsxworker.js */
var XLSX = require('xlsx');

_cb = function(evt) { /* ... do work here ... */ };

Node

The node core modules should be mapped to their @node equivalents:

var SystemJS = require('systemjs');
SystemJS.config({
	map: {
		'xlsx': 'node_modules/xlsx/xlsx.js',
		'fs': '@node/fs',
		'crypto': '@node/crypto',
		'stream': '@node/stream'
	}
});

And use is pretty straightforward:

SystemJS.import('xlsx').then(function(XLSX) {
	/* XLSX is available here */
	var w = XLSX.readFile('test.xlsx');
	var j = XLSX.utils.sheet_to_json(w.Sheets[w.SheetNames[0]], {header:1});
	console.log(j);
});

Analytics