Skip to content

Commit f2a1916

Browse files
authored
Fix asc build (#1236)
1 parent 387c698 commit f2a1916

File tree

4 files changed

+35
-23
lines changed

4 files changed

+35
-23
lines changed

cli/asc.js

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ const mkdirp = require("./util/mkdirp");
4040
const find = require("./util/find");
4141
const EOL = process.platform === "win32" ? "\r\n" : "\n";
4242
const SEP = process.platform === "win32" ? "\\" : "/";
43-
const binaryen = global.Binaryen || (global.Binaryen = require("binaryen"));
43+
const binaryen = global.binaryen || (global.binaryen = require("binaryen"));
4444

4545
// Proxy Binaryen's ready event
4646
Object.defineProperty(exports, "ready", {
@@ -51,28 +51,38 @@ Object.defineProperty(exports, "ready", {
5151
// useless code fragment on top of an actual error. suppress this:
5252
if (process.removeAllListeners) process.removeAllListeners("uncaughtException");
5353

54-
// Use distribution files if present, otherwise run the sources directly
55-
var assemblyscript, isDev = false;
56-
try { // `asc` on the command line
57-
assemblyscript = require("../dist/assemblyscript.js");
58-
} catch (e) {
59-
try { // `asc` on the command line without dist files
60-
require("ts-node").register({
61-
project: path.join(__dirname, "..", "src", "tsconfig.json"),
62-
skipIgnore: true,
63-
compilerOptions: { target: "ES2016" }
64-
});
65-
require("../src/glue/js");
66-
assemblyscript = require("../src");
67-
isDev = true;
68-
} catch (e_ts) {
69-
try { // `require("dist/asc.js")` in explicit browser tests
70-
assemblyscript = eval("require('./assemblyscript')");
54+
// Use distribution files if present, otherwise run the sources directly.
55+
var assemblyscript;
56+
var isDev = false;
57+
(function loadAssemblyScript() {
58+
try {
59+
assemblyscript = require("assemblyscript");
60+
} catch (e) {
61+
function dynRequire(...args) {
62+
return eval("require")(...args);
63+
}
64+
try { // `asc` on the command line
65+
assemblyscript = dynRequire("../dist/assemblyscript.js");
7166
} catch (e) {
72-
throw Error(e_ts.stack + "\n---\n" + e.stack);
67+
try { // `asc` on the command line without dist files
68+
dynRequire("ts-node").register({
69+
project: path.join(__dirname, "..", "src", "tsconfig.json"),
70+
skipIgnore: true,
71+
compilerOptions: { target: "ES2016" }
72+
});
73+
dynRequire("../src/glue/js");
74+
assemblyscript = dynRequire("../src");
75+
isDev = true;
76+
} catch (e_ts) {
77+
try { // `require("dist/asc.js")` in explicit browser tests
78+
assemblyscript = dynRequire("./assemblyscript");
79+
} catch (e) {
80+
throw Error(e_ts.stack + "\n---\n" + e.stack);
81+
}
82+
}
7383
}
7484
}
75-
}
85+
})();
7686

7787
/** Whether this is a webpack bundle or not. */
7888
exports.isBundle = typeof BUNDLE_VERSION === "string";

src/glue/binaryen.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* @license Apache-2.0
44
*/
55

6-
const binaryen = global.Binaryen || (global.Binaryen = require("binaryen"));
6+
const binaryen = global.binaryen || (global.binaryen = require("binaryen"));
77

88
module.exports = binaryen;
99

tests/decompiler.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
var binaryen = global.Binaryen = require("../lib/binaryen");
1+
var binaryen = global.binaryen = require("../lib/binaryen");
22

33
require("ts-node").register({ project: require("path").join(__dirname, "..", "src", "tsconfig.json") });
44
require("../src/glue/js");

webpack.config.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,9 @@ const bin = {
6464
context: path.join(__dirname, "cli"),
6565
entry: [ "./asc.js" ],
6666
externals: [
67-
{ "../dist/assemblyscript.js": "assemblyscript" }
67+
"binaryen",
68+
"assemblyscript",
69+
"ts-node"
6870
],
6971
node: {
7072
"buffer": false,

0 commit comments

Comments
 (0)