Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split up wasm loading based on compilation target. #363

Merged
merged 1 commit into from
Nov 13, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions lib/read-wasm-browser.js
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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