Skip to content

Commit 76be1e6

Browse files
authored
Split up wasm loading based on compilation target. (#363)
1 parent 968e0e3 commit 76be1e6

File tree

3 files changed

+44
-43
lines changed

3 files changed

+44
-43
lines changed

lib/read-wasm-browser.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
"use strict";
2+
3+
let mappingsWasm = null;
4+
5+
module.exports = function readWasm() {
6+
if (typeof mappingsWasm === "string") {
7+
return fetch(mappingsWasm)
8+
.then(response => response.arrayBuffer());
9+
}
10+
if (mappingsWasm instanceof ArrayBuffer) {
11+
return Promise.resolve(mappingsWasm);
12+
}
13+
14+
throw new Error("You must provide the string URL or ArrayBuffer contents " +
15+
"of lib/mappings.wasm by calling " +
16+
"SourceMapConsumer.initialize({ 'lib/mappings.wasm': ... }) " +
17+
"before using SourceMapConsumer");
18+
};
19+
20+
module.exports.initialize = input => {
21+
mappingsWasm = input;
22+
};

lib/read-wasm.js

Lines changed: 19 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,25 @@
1-
/* 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 */
2-
const isBrowserEnvironment = (function() {
3-
// eslint-disable-next-line no-undef
4-
return (typeof window !== "undefined") && (this === window);
5-
}).call();
1+
"use strict";
62

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

10-
let mappingsWasm = null;
6+
const fs = require("fs");
7+
const path = require("path");
118

12-
module.exports = function readWasm() {
13-
if (typeof mappingsWasm === "string") {
14-
return fetch(mappingsWasm)
15-
.then(response => response.arrayBuffer());
16-
}
17-
if (mappingsWasm instanceof ArrayBuffer) {
18-
return Promise.resolve(mappingsWasm);
19-
}
20-
throw new Error("You must provide the string URL or ArrayBuffer contents " +
21-
"of lib/mappings.wasm by calling " +
22-
"SourceMapConsumer.initialize({ 'lib/mappings.wasm': ... }) " +
23-
"before using SourceMapConsumer");
24-
};
9+
module.exports = function readWasm() {
10+
return new Promise((resolve, reject) => {
11+
const wasmPath = path.join(__dirname, "mappings.wasm");
12+
fs.readFile(wasmPath, null, (error, data) => {
13+
if (error) {
14+
reject(error);
15+
return;
16+
}
2517

26-
module.exports.initialize = input => mappingsWasm = input;
27-
} else {
28-
// Node version of reading a wasm file into an array buffer.
29-
const fs = require("fs");
30-
const path = require("path");
31-
32-
module.exports = function readWasm() {
33-
return new Promise((resolve, reject) => {
34-
const wasmPath = path.join(__dirname, "mappings.wasm");
35-
fs.readFile(wasmPath, null, (error, data) => {
36-
if (error) {
37-
reject(error);
38-
return;
39-
}
40-
41-
resolve(data.buffer);
42-
});
18+
resolve(data.buffer);
4319
});
44-
};
20+
});
21+
};
4522

46-
module.exports.initialize = _ => {
47-
console.debug("SourceMapConsumer.initialize is a no-op when running in node.js");
48-
};
49-
}
23+
module.exports.initialize = _ => {
24+
console.debug("SourceMapConsumer.initialize is a no-op when running in node.js");
25+
};

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@
4848
},
4949
"main": "./source-map.js",
5050
"types": "./source-map.d.ts",
51+
"browser": {
52+
"./lib/read-wasm.js": "./lib/read-wasm-browser.js"
53+
},
5154
"files": [
5255
"source-map.js",
5356
"source-map.d.ts",

0 commit comments

Comments
 (0)