mirror of
https://github.com/SquidDev-CC/CC-Tweaked
synced 2025-10-22 01:17:38 +00:00
Build a web-based emulator for the documentation site (#1597)
Historically we've used copy-cat to provide a web-based emulator for running example code on our documentation site. However, copy-cat is often out-of-date with CC:T, which means example snippets fail when you try to run them! This commit vendors in copy-cat (or rather an updated version of it) into CC:T itself, allowing us to ensure the emulator is always in sync with the mod. While the ARCHITECTURE.md documentation goes into a little bit more detail here, the general implementation is as follows - In project/src/main we implement the core of the emulator. This includes a basic reimplementation of some of CC's classes to work on the web (mostly the HTTP API and ComputerThread), and some additional code to expose the computers to Javascript. - This is all then compiled to Javascript using [TeaVM][1] (we actually use a [personal fork of it][2] as there's a couple of changes I've not upstreamed yet). - The Javascript side then pulls in the these compiled classes (and the CC ROM) and hooks them up to [cc-web-term][3] to display the actual computer. - As we're no longer pulling in copy-cat, we can simplify our bundling system a little - we now just compile to ESM modules directly. [1]: https://github.com/konsoletyper/teavm [2]: https://github.com/SquidDev/teavm/tree/squid-patches [3]: https://github.com/squiddev-cc/cc-web-term
This commit is contained in:
@@ -2,52 +2,48 @@
|
||||
//
|
||||
// SPDX-License-Identifier: MPL-2.0
|
||||
|
||||
import { readFileSync } from "fs";
|
||||
import path from "path";
|
||||
|
||||
import terser from "@rollup/plugin-terser";
|
||||
import resolve from "@rollup/plugin-node-resolve";
|
||||
import typescript from "@rollup/plugin-typescript";
|
||||
import url from "@rollup/plugin-url";
|
||||
import postcss from "rollup-plugin-postcss";
|
||||
|
||||
const input = "src/frontend";
|
||||
const requirejs = readFileSync("../../node_modules/requirejs/require.js");
|
||||
|
||||
/** @type import("rollup").RollupOptions */
|
||||
export default {
|
||||
const minify = args => !args.configDebug;
|
||||
|
||||
/** @type import("rollup").RollupOptionsFunction */
|
||||
export default args => ({
|
||||
input: [`${input}/index.tsx`],
|
||||
output: {
|
||||
// Also defined in build.gradle.kts
|
||||
dir: "build/rollup/",
|
||||
|
||||
// We bundle requirejs (and config) into the header. It's rather gross
|
||||
// but also works reasonably well.
|
||||
// Also suffix a ?v=${date} onto the end in the event we need to require a specific copy-cat version.
|
||||
banner: `
|
||||
${requirejs}
|
||||
require.config({
|
||||
paths: { copycat: "https://copy-cat.squiddev.cc" },
|
||||
urlArgs: function(id) { return id == "copycat/embed" ? "?v=20211221" : ""; }
|
||||
});
|
||||
`,
|
||||
format: "amd",
|
||||
format: "esm",
|
||||
generatedCode: {
|
||||
preset: "es2015",
|
||||
constBindings: true,
|
||||
},
|
||||
amd: {
|
||||
define: "require",
|
||||
}
|
||||
},
|
||||
context: "window",
|
||||
external: ["copycat/embed"],
|
||||
|
||||
plugins: [
|
||||
typescript(),
|
||||
resolve({ browser: true }),
|
||||
|
||||
url({
|
||||
include: "**/*.dfpwm",
|
||||
include: ["**/*.dfpwm", "**/*.worker.js", "**/*.png"],
|
||||
fileName: "[name]-[hash][extname]",
|
||||
publicPath: "/",
|
||||
limit: 0,
|
||||
}),
|
||||
|
||||
postcss({
|
||||
namedExports: true,
|
||||
minimize: minify(args),
|
||||
extract: true,
|
||||
}),
|
||||
|
||||
{
|
||||
@@ -59,8 +55,14 @@ export default {
|
||||
? `export default ${JSON.stringify(code)};\n`
|
||||
: null;
|
||||
},
|
||||
|
||||
async resolveId(source) {
|
||||
if (source === "cct/classes") return path.resolve("build/teaVM/classes.js");
|
||||
if (source === "cct/resources") return path.resolve("build/teaVM/resources.js");
|
||||
return null;
|
||||
},
|
||||
},
|
||||
|
||||
terser(),
|
||||
minify(args) && terser(),
|
||||
],
|
||||
};
|
||||
});
|
||||
|
Reference in New Issue
Block a user