Simplify loading

This commit is contained in:
Adria Navarro 2024-02-13 23:44:24 +01:00
parent 2d2d88f988
commit f733d293da
3 changed files with 6 additions and 34 deletions

View File

@ -98,7 +98,6 @@ const environment = {
JS_RUNNER_MEMORY_LIMIT: JS_RUNNER_MEMORY_LIMIT:
parseIntSafe(process.env.JS_RUNNER_MEMORY_LIMIT) || 64, parseIntSafe(process.env.JS_RUNNER_MEMORY_LIMIT) || 64,
LOG_JS_ERRORS: process.env.LOG_JS_ERRORS, LOG_JS_ERRORS: process.env.LOG_JS_ERRORS,
isBundled: process.env.BUNDLED,
} }
// clean up any environment variable edge cases // clean up any environment variable edge cases

View File

@ -7,6 +7,10 @@ export const enum BundleType {
BSON = "bson", BSON = "bson",
} }
const bundleSourceFile: Record<BundleType, string> = {
[BundleType.HELPERS]: "./index-helpers.ivm.bundle.js",
[BundleType.BSON]: "./bson.ivm.bundle.js",
}
const bundleSourceCode: Partial<Record<BundleType, string>> = {} const bundleSourceCode: Partial<Record<BundleType, string>> = {}
export function loadBundle(type: BundleType) { export function loadBundle(type: BundleType) {
@ -15,34 +19,7 @@ export function loadBundle(type: BundleType) {
return sourceCode return sourceCode
} }
if (!environment.isBundled) { sourceCode = fs.readFileSync(require.resolve(bundleSourceFile[type]), "utf-8")
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 bundleSourceCode[type] = sourceCode
return sourceCode return sourceCode
} }

View File

@ -49,7 +49,6 @@ function runBuild(entry, outfile) {
preserveSymlinks: true, preserveSymlinks: true,
loader: { loader: {
".svelte": "copy", ".svelte": "copy",
".ivm.bundle.js": "text",
}, },
metafile: true, metafile: true,
external: [ external: [
@ -63,9 +62,6 @@ function runBuild(entry, outfile) {
"graphql/*", "graphql/*",
"bson", "bson",
], ],
define: {
"process.env.BUNDLED": '"true"',
},
} }
build({ build({
@ -73,7 +69,7 @@ function runBuild(entry, outfile) {
platform: "node", platform: "node",
outfile, outfile,
}).then(result => { }).then(result => {
glob(`${process.cwd()}/src/**/*.hbs`, {}, (err, files) => { glob(`${process.cwd()}/src/**/*.{hbs,ivm.bundle.js}`, {}, (err, files) => {
for (const file of files) { for (const file of files) {
fs.copyFileSync(file, `${process.cwd()}/dist/${path.basename(file)}`) fs.copyFileSync(file, `${process.cwd()}/dist/${path.basename(file)}`)
} }