Skip to content

Commit

Permalink
Clean up the web build
Browse files Browse the repository at this point in the history
Just like the DTS build, the web build now exports functions instead of
being spawned as a child process. Also, the web build no longer fails
silently upon error.
  • Loading branch information
CountBleck committed Oct 7, 2023
1 parent 12a2867 commit 5fc8a8f
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 51 deletions.
73 changes: 37 additions & 36 deletions scripts/build-web.js
@@ -1,42 +1,43 @@
import path from "path";
import {dirname, join} from "path";
import fs from "fs";
import { createRequire } from "module";
import { fileURLToPath } from "url";

const dirname = path.dirname(fileURLToPath(import.meta.url));
const require = createRequire(import.meta.url);
const pkg = require("../package-lock.json");
const __dirname = dirname(fileURLToPath(import.meta.url));

const mainVersion = pkg.version;
const binaryenVersion = pkg.dependencies.binaryen.version;
const longVersion = pkg.dependencies.long.version;
export function buildWeb() {
const pkg = JSON.parse(fs.readFileSync(join(__dirname, "../package-lock.json")));

const distUrl = mainVersion === "0.0.0" ? `./` : `https://cdn.jsdelivr.net/npm/assemblyscript@${mainVersion}/dist/`;
const binaryenUrl = `https://cdn.jsdelivr.net/npm/binaryen@${binaryenVersion}/index.js`;
const longUrl = `https://cdn.jsdelivr.net/npm/long@${longVersion}/index.js`;
const importmap = {
"imports": {
"assemblyscript": `${distUrl}assemblyscript.js`,
"assemblyscript/asc": `${distUrl}asc.js`,
"binaryen": binaryenUrl,
"long": longUrl
}
};
const mainVersion = pkg.version;
const binaryenVersion = pkg.dependencies.binaryen.version;
const longVersion = pkg.dependencies.long.version;

const distUrl = mainVersion === "0.0.0" ? `./` : `https://cdn.jsdelivr.net/npm/assemblyscript@${mainVersion}/dist/`;
const binaryenUrl = `https://cdn.jsdelivr.net/npm/binaryen@${binaryenVersion}/index.js`;
const longUrl = `https://cdn.jsdelivr.net/npm/long@${longVersion}/index.js`;
const importmap = {
"imports": {
"assemblyscript": `${distUrl}assemblyscript.js`,
"assemblyscript/asc": `${distUrl}asc.js`,
"binaryen": binaryenUrl,
"long": longUrl
}
};

fs.writeFileSync(path.join(dirname, "..", "dist", "importmap.json"), `${JSON.stringify(importmap, null, 2)}
`);
fs.writeFileSync(path.join(dirname, "..", "dist", "web.js"), `var ASSEMBLYSCRIPT_VERSION = ${JSON.stringify(mainVersion)};
var ASSEMBLYSCRIPT_IMPORTMAP = ${JSON.stringify(importmap, null, 2)};
if (!document.currentScript.src.includes("noinstall")) {
let elem = document.createElement("script");
elem.type = "importmap";
elem.text = JSON.stringify(ASSEMBLYSCRIPT_IMPORTMAP);
document.head.appendChild(elem);
}
if (!document.currentScript.src.includes("noshim")) {
let elem = document.createElement("script");
elem.async = true;
elem.src = "https://cdn.jsdelivr.net/npm/es-module-shims@1/dist/es-module-shims.wasm.min.js";
document.head.appendChild(elem);
}
`);
fs.writeFileSync(join(__dirname, "..", "dist", "importmap.json"), `${JSON.stringify(importmap, null, 2)}
`);
fs.writeFileSync(join(__dirname, "..", "dist", "web.js"), `var ASSEMBLYSCRIPT_VERSION = ${JSON.stringify(mainVersion)};
var ASSEMBLYSCRIPT_IMPORTMAP = ${JSON.stringify(importmap, null, 2)};
if (!document.currentScript.src.includes("noinstall")) {
let elem = document.createElement("script");
elem.type = "importmap";
elem.text = JSON.stringify(ASSEMBLYSCRIPT_IMPORTMAP);
document.head.appendChild(elem);
}
if (!document.currentScript.src.includes("noshim")) {
let elem = document.createElement("script");
elem.async = true;
elem.src = "https://cdn.jsdelivr.net/npm/es-module-shims@1/dist/es-module-shims.wasm.min.js";
document.head.appendChild(elem);
}
`);
}
30 changes: 15 additions & 15 deletions scripts/build.js
@@ -1,12 +1,12 @@
import fs from "fs";
import path from "path";
import { fileURLToPath } from "url";
import childProcess from "child_process";
import esbuild from "esbuild";
import { globSync } from "glob";
import { createRequire } from "module";
import { stdoutColors } from "../util/terminal.js";

import {buildWeb} from "./build-web.js";
import * as dts from "./build-dts.js";

const require = createRequire(import.meta.url);
Expand Down Expand Up @@ -175,22 +175,22 @@ const webPlugin = {
setup(build) {
build.onEnd(() => {
const startTime = Date.now();
const stdout = [];
console.log(`${time()} - ${"web"} - Starting new build ...`);
childProcess.spawn("node", [ "./build-web.js" ], {
cwd: dirname,
stdio: "pipe"
}).on("data", data => {
stdout.push(data.toString());
}).on("error", err => {
console.log(`${time()} - web - Starting new build ...`);

try {
buildWeb();

const duration = Date.now() - startTime;
console.log(stdout.join(""));
console.log(`${time()} - ${"web"} - ${stdoutColors.red("ERROR")} (had errors, ${duration} ms)`);
}).on("close", code => {
if (code) return;
console.log(`${time()} - web - ${stdoutColors.green("SUCCESS")} (no errors, ${duration} ms)`);
process.exitCode = 0;
} catch (e) {
const duration = Date.now() - startTime;
console.log(`${time()} - ${"web"} - ${stdoutColors.green("SUCCESS")} (no errors, ${duration} ms)`);
});
console.error(e);
console.log(`${time()} - web - ${stdoutColors.red("ERROR")} (had errors, ${duration} ms)`);
process.exitCode = 1;
} finally {
buildingDefinitions = false;
}
});
}
};
Expand Down

0 comments on commit 5fc8a8f

Please sign in to comment.