Skip to content

Commit

Permalink
Changed importing .json as text instead for performance reasons (#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
LennardF1989 authored and RDIL committed Dec 3, 2022
1 parent cebb555 commit 0d4aa3c
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 49 deletions.
3 changes: 3 additions & 0 deletions components/configSwizzleManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,9 @@ const configs: Record<string, unknown> = {
}

Object.keys(configs).forEach((cfg) => {
// Parse the string into an object
configs[cfg] = JSON.parse(configs[cfg])

const overridePath = join("overrides", `${cfg}.json`)

if (existsSync(overridePath)) {
Expand Down
7 changes: 5 additions & 2 deletions components/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@ import { MenuSystemDatabase, menuSystemDatabase } from "./menus/menuSystem"
import { escalationMappings } from "./contracts/escalationMappings"
import { parse } from "json5"
import { userAuths } from "./officialServerAuth"
// @ts-expect-error Ignore JSON import
import LASTYARDBIRDSCPC from "../contractdata/SNIPER/THELASTYARDBIRD_SCPC.json"
// @ts-expect-error Ignore JSON import
import LEGACYFF from "../contractdata/COLORADO/FREEDOMFIGHTERSLEGACY.json"
import { missionsInLocations } from "./contracts/missionsInLocation"
import { createContext, Script } from "vm"
Expand Down Expand Up @@ -263,9 +265,10 @@ function createPeacockRequire(pluginName: string): NodeRequire {
/**
* Freedom Fighters for Hitman 2016 (objectives are different).
*/
export const _legacyBull: MissionManifest = LEGACYFF
export const _legacyBull: MissionManifest = JSON.parse(LEGACYFF)

export const _theLastYardbirdScpc: MissionManifest = LASTYARDBIRDSCPC
export const _theLastYardbirdScpc: MissionManifest =
JSON.parse(LASTYARDBIRDSCPC)

export const peacockRecentEscalations: readonly string[] = [
"35f1f534-ae2d-42be-8472-dd55e96625ea",
Expand Down
23 changes: 0 additions & 23 deletions contractdata/COLORADO/FREEDOMFIGHTERSLEGACY.json.d.ts

This file was deleted.

23 changes: 0 additions & 23 deletions contractdata/SNIPER/THELASTYARDBIRD_SCPC.json.d.ts

This file was deleted.

18 changes: 18 additions & 0 deletions packaging/build.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,23 @@ await packContractsAndChallenges()

const { version, revisionIdent } = require("../package.json")

const jsonAsCompressedTextPlugin = {
name: "jsonAsCompressedTextPlugin",

setup(build) {
const fs = require("fs")

build.onLoad({ filter: /\.json$/ }, async (args) => {
const text = await fs.promises.readFile(args.path)

return {
contents: JSON.stringify(JSON.parse(text)),
loader: "text",
}
})
},
}

await e.build({
entryPoints: ["components/index.ts"],
bundle: true,
Expand Down Expand Up @@ -59,6 +76,7 @@ await e.build({
},
sourcemap: "external",
plugins: [
jsonAsCompressedTextPlugin,
esbuildPluginLicense({
thirdParty: {
output: {
Expand Down
11 changes: 10 additions & 1 deletion packaging/devLoader.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@

import picocolors from "picocolors"
import { packContractsAndChallenges } from "./buildTasks.mjs"
import { createRequire } from "module"
import { createRequire, Module } from "module"
import { readFileSync } from "fs"

// this `require` instance will be hijacked by `esbuild-register` so we can load
// TS files as if they were JS in a CommonJS environment
Expand Down Expand Up @@ -46,4 +47,12 @@ const { register } = require("esbuild-register/dist/node")

register()

const resolveTextFile = function (module, path) {
const content = readFileSync(path).toString()

module.exports = content
}

Module._extensions[".json"] = resolveTextFile

require("../components/index.ts")

0 comments on commit 0d4aa3c

Please sign in to comment.