Skip to content

Commit

Permalink
Autodetect ESM supports during asinit (#1685)
Browse files Browse the repository at this point in the history
Co-authored-by: Daniel Wirtz <dcode@dcode.io>
  • Loading branch information
MaxGraey and dcodeIO committed May 19, 2021
1 parent 4e83617 commit 3d52206
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion bin/asinit
Original file line number Diff line number Diff line change
Expand Up @@ -377,9 +377,14 @@ function ensurePackageJson() {
function ensureIndexJs() {
console.log("- Making sure that 'index.js' exists...");
if (!fs.existsSync(indexFile)) {
// since node.js v13.2.0 or v12.17.0 we can use ESM without flags
const ver = process.versions.node.split('.');
const maj = parseInt(ver[0]);
const min = parseInt(ver[1]);
const supportESM = maj >= 14 || (maj == 13 && min >= 2) || (maj == 12 && min >= 17);
fs.writeFileSync(indexFile, [
"const fs = require(\"fs\");",
"const loader = require(\"@assemblyscript/loader\");",
"const loader = require(\"@assemblyscript/loader" + (supportESM ? "" : "/umd") + "\");",
"const imports = { /* imports go here */ };",
"const wasmModule = loader.instantiateSync(fs.readFileSync(__dirname + \"/build/optimized.wasm\"), imports);",
"module.exports = wasmModule.exports;"
Expand Down

0 comments on commit 3d52206

Please sign in to comment.