Skip to content

Autodetect ESM supports during asinit #1685

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
May 19, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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