Skip to content

Commit 3d52206

Browse files
MaxGraeydcodeIO
andauthored
Autodetect ESM supports during asinit (#1685)
Co-authored-by: Daniel Wirtz <[email protected]>
1 parent 4e83617 commit 3d52206

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

bin/asinit

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -377,9 +377,14 @@ function ensurePackageJson() {
377377
function ensureIndexJs() {
378378
console.log("- Making sure that 'index.js' exists...");
379379
if (!fs.existsSync(indexFile)) {
380+
// since node.js v13.2.0 or v12.17.0 we can use ESM without flags
381+
const ver = process.versions.node.split('.');
382+
const maj = parseInt(ver[0]);
383+
const min = parseInt(ver[1]);
384+
const supportESM = maj >= 14 || (maj == 13 && min >= 2) || (maj == 12 && min >= 17);
380385
fs.writeFileSync(indexFile, [
381386
"const fs = require(\"fs\");",
382-
"const loader = require(\"@assemblyscript/loader\");",
387+
"const loader = require(\"@assemblyscript/loader" + (supportESM ? "" : "/umd") + "\");",
383388
"const imports = { /* imports go here */ };",
384389
"const wasmModule = loader.instantiateSync(fs.readFileSync(__dirname + \"/build/optimized.wasm\"), imports);",
385390
"module.exports = wasmModule.exports;"

0 commit comments

Comments
 (0)