Skip to content

Commit

Permalink
Split up wasm loading based on compilation target.
Browse files Browse the repository at this point in the history
  • Loading branch information
loganfsmyth committed Oct 11, 2018
1 parent d69af14 commit 440d984
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 44 deletions.
2 changes: 1 addition & 1 deletion dist/source-map.js

Large diffs are not rendered by default.

22 changes: 22 additions & 0 deletions lib/read-wasm-browser.js
@@ -0,0 +1,22 @@
"use strict";

let mappingsWasm = null;

module.exports = function readWasm() {
if (typeof mappingsWasm === "string") {
return fetch(mappingsWasm)
.then(response => response.arrayBuffer());
}
if (mappingsWasm instanceof ArrayBuffer) {
return Promise.resolve(mappingsWasm);
}

throw new Error("You must provide the string URL or ArrayBuffer contents " +
"of lib/mappings.wasm by calling " +
"SourceMapConsumer.initialize({ 'lib/mappings.wasm': ... }) " +
"before using SourceMapConsumer");
};

module.exports.initialize = input => {
mappingsWasm = input;
};
62 changes: 19 additions & 43 deletions lib/read-wasm.js
@@ -1,49 +1,25 @@
/* Determine browser vs node environment by testing the default top level context. Solution courtesy of: https://stackoverflow.com/questions/17575790/environment-detection-node-js-or-browser */
const isBrowserEnvironment = (function() {
// eslint-disable-next-line no-undef
return (typeof window !== "undefined") && (this === window);
}).call();
"use strict";

if (isBrowserEnvironment) {
// Web version of reading a wasm file into an array buffer.
// Note: This file is replaced with "read-wasm-browser.js" when this module is
// bundled with a packager that takes package.json#browser fields into account.

let mappingsWasm = null;
const fs = require("fs");
const path = require("path");

module.exports = function readWasm() {
if (typeof mappingsWasm === "string") {
return fetch(mappingsWasm)
.then(response => response.arrayBuffer());
}
if (mappingsWasm instanceof ArrayBuffer) {
return Promise.resolve(mappingsWasm);
}
throw new Error("You must provide the string URL or ArrayBuffer contents " +
"of lib/mappings.wasm by calling " +
"SourceMapConsumer.initialize({ 'lib/mappings.wasm': ... }) " +
"before using SourceMapConsumer");
};
module.exports = function readWasm() {
return new Promise((resolve, reject) => {
const wasmPath = path.join(__dirname, "mappings.wasm");
fs.readFile(wasmPath, null, (error, data) => {
if (error) {
reject(error);
return;
}

module.exports.initialize = input => mappingsWasm = input;
} else {
// Node version of reading a wasm file into an array buffer.
const fs = require("fs");
const path = require("path");

module.exports = function readWasm() {
return new Promise((resolve, reject) => {
const wasmPath = path.join(__dirname, "mappings.wasm");
fs.readFile(wasmPath, null, (error, data) => {
if (error) {
reject(error);
return;
}

resolve(data.buffer);
});
resolve(data.buffer);
});
};
});
};

module.exports.initialize = _ => {
console.debug("SourceMapConsumer.initialize is a no-op when running in node.js");
};
}
module.exports.initialize = _ => {
console.debug("SourceMapConsumer.initialize is a no-op when running in node.js");
};
3 changes: 3 additions & 0 deletions package.json
Expand Up @@ -48,6 +48,9 @@
},
"main": "./source-map.js",
"types": "./source-map.d.ts",
"browser": {
"./lib/read-wasm.js": "./lib/read-wasm-browser.js"
},
"files": [
"source-map.js",
"source-map.d.ts",
Expand Down

0 comments on commit 440d984

Please sign in to comment.