From 12df0fc920b1d4cb7263a3f84496fa5e9b55f950 Mon Sep 17 00:00:00 2001 From: dcode Date: Tue, 21 Apr 2020 21:34:15 +0200 Subject: [PATCH 1/2] Fix asc build --- cli/asc.js | 50 ++++++++++++++++++++++++++++------------------- webpack.config.js | 4 +++- 2 files changed, 33 insertions(+), 21 deletions(-) diff --git a/cli/asc.js b/cli/asc.js index 7c2baf3ae1..f21597c875 100644 --- a/cli/asc.js +++ b/cli/asc.js @@ -40,7 +40,7 @@ const mkdirp = require("./util/mkdirp"); const find = require("./util/find"); const EOL = process.platform === "win32" ? "\r\n" : "\n"; const SEP = process.platform === "win32" ? "\\" : "/"; -const binaryen = global.Binaryen || (global.Binaryen = require("binaryen")); +const binaryen = global.binaryen || (global.binaryen = require("binaryen")); // Proxy Binaryen's ready event Object.defineProperty(exports, "ready", { @@ -51,28 +51,38 @@ Object.defineProperty(exports, "ready", { // useless code fragment on top of an actual error. suppress this: if (process.removeAllListeners) process.removeAllListeners("uncaughtException"); -// Use distribution files if present, otherwise run the sources directly -var assemblyscript, isDev = false; -try { // `asc` on the command line - assemblyscript = require("../dist/assemblyscript.js"); -} catch (e) { - try { // `asc` on the command line without dist files - require("ts-node").register({ - project: path.join(__dirname, "..", "src", "tsconfig.json"), - skipIgnore: true, - compilerOptions: { target: "ES2016" } - }); - require("../src/glue/js"); - assemblyscript = require("../src"); - isDev = true; - } catch (e_ts) { - try { // `require("dist/asc.js")` in explicit browser tests - assemblyscript = eval("require('./assemblyscript')"); +// Use distribution files if present, otherwise run the sources directly. +var assemblyscript; +var isDev = false; +(function loadAssemblyScript() { + try { + assemblyscript = require("assemblyscript"); + } catch (e) { + function dynRequire(...args) { + return eval("require")(...args); + } + try { // `asc` on the command line + assemblyscript = dynRequire("../dist/assemblyscript.js"); } catch (e) { - throw Error(e_ts.stack + "\n---\n" + e.stack); + try { // `asc` on the command line without dist files + dynRequire("ts-node").register({ + project: path.join(__dirname, "..", "src", "tsconfig.json"), + skipIgnore: true, + compilerOptions: { target: "ES2016" } + }); + dynRequire("../src/glue/js"); + assemblyscript = dynRequire("../src"); + isDev = true; + } catch (e_ts) { + try { // `require("dist/asc.js")` in explicit browser tests + assemblyscript = dynRequire("./assemblyscript"); + } catch (e) { + throw Error(e_ts.stack + "\n---\n" + e.stack); + } + } } } -} +})(); /** Whether this is a webpack bundle or not. */ exports.isBundle = typeof BUNDLE_VERSION === "string"; diff --git a/webpack.config.js b/webpack.config.js index 038f53050f..ed6bb8cec2 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -64,7 +64,9 @@ const bin = { context: path.join(__dirname, "cli"), entry: [ "./asc.js" ], externals: [ - { "../dist/assemblyscript.js": "assemblyscript" } + "binaryen", + "assemblyscript", + "ts-node" ], node: { "buffer": false, From 8d475bb9f5a247323ef383a5edcdd9776c5935d9 Mon Sep 17 00:00:00 2001 From: dcode Date: Tue, 21 Apr 2020 21:42:10 +0200 Subject: [PATCH 2/2] lowercase global.binaryen everywhere --- src/glue/binaryen.js | 2 +- tests/decompiler.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/glue/binaryen.js b/src/glue/binaryen.js index 0db53bbb64..f173b76539 100644 --- a/src/glue/binaryen.js +++ b/src/glue/binaryen.js @@ -3,7 +3,7 @@ * @license Apache-2.0 */ -const binaryen = global.Binaryen || (global.Binaryen = require("binaryen")); +const binaryen = global.binaryen || (global.binaryen = require("binaryen")); module.exports = binaryen; diff --git a/tests/decompiler.js b/tests/decompiler.js index 47e78ba5eb..e8ca8922bf 100644 --- a/tests/decompiler.js +++ b/tests/decompiler.js @@ -1,4 +1,4 @@ -var binaryen = global.Binaryen = require("../lib/binaryen"); +var binaryen = global.binaryen = require("../lib/binaryen"); require("ts-node").register({ project: require("path").join(__dirname, "..", "src", "tsconfig.json") }); require("../src/glue/js");