Attempt quicker loading

This commit is contained in:
Adria Navarro 2024-02-13 23:37:30 +01:00
parent 0902854bb0
commit 2d2d88f988
1 changed files with 33 additions and 14 deletions

View File

@ -7,23 +7,42 @@ export const enum BundleType {
BSON = "bson",
}
const bundleSourceCode = {
[BundleType.HELPERS]: "./index-helpers.ivm.bundle.js",
[BundleType.BSON]: "./bson.ivm.bundle.js",
}
const bundleSourceCode: Partial<Record<BundleType, string>> = {}
export function loadBundle(type: BundleType) {
if (!environment.isBundled) {
return fs.readFileSync(require.resolve(bundleSourceCode[type]), "utf-8")
let sourceCode = bundleSourceCode[type]
if (sourceCode) {
return sourceCode
}
// If we are running from a built version, esbuild is configured to inject .ivm.bundle.js files as text
switch (type) {
case BundleType.HELPERS:
return require("./index-helpers.ivm.bundle.js")
case BundleType.BSON:
return require("./bson.ivm.bundle.js")
default:
utils.unreachable(type)
if (!environment.isBundled) {
let filePath
switch (type) {
case BundleType.HELPERS:
filePath = "./index-helpers.ivm.bundle.js"
break
case BundleType.BSON:
filePath = "./bson.ivm.bundle.js"
break
default:
throw utils.unreachable(type)
}
sourceCode = fs.readFileSync(require.resolve(filePath), "utf-8")
} else {
// If we are running from a built version, esbuild is configured to inject .ivm.bundle.js files as text
switch (type) {
case BundleType.HELPERS:
sourceCode = require("./index-helpers.ivm.bundle.js")
break
case BundleType.BSON:
sourceCode = require("./bson.ivm.bundle.js")
break
default:
throw utils.unreachable(type)
}
}
bundleSourceCode[type] = sourceCode
return sourceCode
}