Skip to content

Commit 3b50720

Browse files
committed
Also create an 'index.js' to load the module when running asinit
1 parent 59a22c1 commit 3b50720

1 file changed

Lines changed: 24 additions & 0 deletions

File tree

bin/asinit

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ const entryFile = path.join(assemblyDir, "index.ts");
3535
const buildDir = path.join(projectDir, "build");
3636
const gitignoreFile = path.join(buildDir, ".gitignore");
3737
const packageFile = path.join(projectDir, "package.json");
38+
const indexFile = path.join(projectDir, "index.js");
3839

3940
console.log([
4041
"Version: " + version,
@@ -59,6 +60,9 @@ console.log([
5960
chalk.cyan(" ./build/.gitignore"),
6061
" Git configuration that excludes compiled binaries from source control.",
6162
"",
63+
chalk.cyan(" ./index.js"),
64+
" Main file loading the WebAssembly module and exporting its exports.",
65+
"",
6266
chalk.cyan(" ./package.json"),
6367
" Package info containing the necessary commands to compile to WebAssembly.",
6468
"",
@@ -80,6 +84,7 @@ rl.question(chalk.white.bold("Do you want to proceed?") + " [Y/n] ", answer => {
8084
ensureBuildDirectory();
8185
ensureGitignore();
8286
ensurePackageJson();
87+
ensureIndexJs();
8388
console.log([
8489
chalk.green("Done!"),
8590
"",
@@ -192,6 +197,7 @@ function ensureGitignore() {
192197
if (!fs.existsSync(gitignoreFile)) {
193198
fs.writeFileSync(gitignoreFile, [
194199
"*.wasm",
200+
"*.wasm.map",
195201
"*.asm.js"
196202
].join("\n") + "\n");
197203
console.log(chalk.green(" Created: ") + gitignoreFile);
@@ -233,3 +239,21 @@ function ensurePackageJson() {
233239
}
234240
console.log();
235241
}
242+
243+
function ensureIndexJs() {
244+
console.log("- Making sure that 'index.js' exists...");
245+
if (!fs.existsSync(indexFile)) {
246+
fs.writeFileSync(indexFile, [
247+
"const fs = require(\"fs\");",
248+
"const compiled = new WebAssembly.Module(fs.readFileSync(__dirname + \"/build/optimized.wasm\"));",
249+
"const imports = {};",
250+
"Object.defineProperty(module, \"exports\", {",
251+
" get: () => new WebAssembly.Instance(compiled, imports).exports",
252+
"});",
253+
].join("\n") + "\n");
254+
console.log(chalk.green(" Created: ") + indexFile);
255+
} else {
256+
console.log(chalk.yellow(" Exists: ") + indexFile);
257+
}
258+
console.log();
259+
}

0 commit comments

Comments
 (0)