-
Notifications
You must be signed in to change notification settings - Fork 361
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split up wasm loading based on compilation target.
- Loading branch information
1 parent
d69af14
commit 5c2ab93
Showing
3 changed files
with
44 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters