|
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"; |
6 | 2 |
|
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. |
9 | 5 |
|
10 | | - let mappingsWasm = null; |
| 6 | +const fs = require("fs"); |
| 7 | +const path = require("path"); |
11 | 8 |
|
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 | + } |
25 | 17 |
|
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); |
43 | 19 | }); |
44 | | - }; |
| 20 | + }); |
| 21 | +}; |
45 | 22 |
|
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 | +}; |
0 commit comments