fix: use real package version in errortemplate

This commit is contained in:
Percs 2024-10-11 12:00:29 -05:00
parent 9305fdd96a
commit b9d60e96e2
2 changed files with 15 additions and 14 deletions

View file

@ -1,12 +1,14 @@
import { defineConfig } from "@rspack/cli"; import { defineConfig } from "@rspack/cli";
import { rspack } from "@rspack/core"; import { rspack } from "@rspack/core";
import { RsdoctorRspackPlugin } from "@rsdoctor/rspack-plugin"; import { RsdoctorRspackPlugin } from "@rsdoctor/rspack-plugin";
import { join } from "path";
import { fileURLToPath } from "url";
import obfuscator from "javascript-obfuscator"; import obfuscator from "javascript-obfuscator";
const { obfuscate } = obfuscator; const { obfuscate } = obfuscator;
import { readFile } from "node:fs/promises";
import { join } from "path";
import { fileURLToPath } from "url";
const __dirname = fileURLToPath(new URL(".", import.meta.url)); const __dirname = fileURLToPath(new URL(".", import.meta.url));
const packagemeta = JSON.parse(await readFile("package.json"));
export default defineConfig({ export default defineConfig({
// change to production when needed // change to production when needed
@ -66,6 +68,9 @@ export default defineConfig({
new rspack.ProvidePlugin({ new rspack.ProvidePlugin({
dbg: [join(__dirname, "src/log.ts"), "default"], dbg: [join(__dirname, "src/log.ts"), "default"],
}), }),
new rspack.DefinePlugin({
VERSION: JSON.stringify(packagemeta.version),
}),
process.env.OBFUSCATE === "true" && { process.env.OBFUSCATE === "true" && {
apply(compiler) { apply(compiler) {
compiler.hooks.compilation.tap("GyatPlugin", (compilation) => { compiler.hooks.compilation.tap("GyatPlugin", (compilation) => {

View file

@ -3,18 +3,16 @@ export function errorTemplate(trace: string, fetchedURL: string) {
const script = ` const script = `
errorTrace.value = ${JSON.stringify(trace)}; errorTrace.value = ${JSON.stringify(trace)};
fetchedURL.textContent = ${JSON.stringify(fetchedURL)}; fetchedURL.textContent = ${JSON.stringify(fetchedURL)};
for (const node of document.querySelectorAll("#hostname")) node.textContent = ${JSON.stringify( for (const node of document.querySelectorAll("#hostname")) node.textContent = ${JSON.stringify(location.hostname)};
location.hostname
)};
reload.addEventListener("click", () => location.reload()); reload.addEventListener("click", () => location.reload());
version.textContent = "0.0.1"; version.textContent = ${JSON.stringify(VERSION)};
`; `;
return `<!DOCTYPE html> return `<!DOCTYPE html>
<html> <html>
<head> <head>
<meta charset="utf-8" /> <meta charset="utf-8" />
<title>Error</title> <title>Scramjet</title>
<style> <style>
* { background-color: white } * { background-color: white }
</style> </style>
@ -42,9 +40,7 @@ export function errorTemplate(trace: string, fetchedURL: string) {
<button id="reload">Reload</button> <button id="reload">Reload</button>
<hr /> <hr />
<p><i>Scramjet v<span id="version"></span></i></p> <p><i>Scramjet v<span id="version"></span></i></p>
<script src="${ <script src="${"data:application/javascript," + encodeURIComponent(script)}"></script>
"data:application/javascript," + encodeURIComponent(script)
}"></script>
</body> </body>
</html> </html>
`; `;