@intlayer/config
Version:
Retrieve Intlayer configurations and manage environment variables for both server-side and client-side environments.
106 lines (104 loc) • 3.4 kB
JavaScript
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
const require_runtime = require('../_virtual/_rolldown/runtime.cjs');
const require_utils_ESMxCJSHelpers = require('../utils/ESMxCJSHelpers.cjs');
const require_loadEnvFile = require('../loadEnvFile.cjs');
let node_vm = require("node:vm");
let esbuild = require("esbuild");
esbuild = require_runtime.__toESM(esbuild);
//#region src/loadExternalFile/parseFileContent.ts
const NODE_GLOBALS = [
"Buffer",
"setTimeout",
"clearTimeout",
"setInterval",
"clearInterval",
"setImmediate",
"clearImmediate",
"queueMicrotask",
"URL",
"URLSearchParams",
"TextEncoder",
"TextDecoder",
"AbortController",
"AbortSignal",
"performance",
"fetch",
"crypto",
"structuredClone"
];
const getSandBoxContext = (options) => {
const { envVarOptions, projectRequire, additionalEnvVars, mocks, aliases } = options ?? {};
let additionalGlobalVar = {};
const baseRequire = typeof projectRequire === "function" ? projectRequire : require_utils_ESMxCJSHelpers.getProjectRequire();
const mockedRequire = (() => {
const mockTable = Object.assign({ esbuild }, mocks);
const aliasTable = Object.assign({}, aliases);
const wrappedRequire = function mockableRequire(id) {
const target = aliasTable?.[id] ? aliasTable[id] : id;
if (mockTable && Object.hasOwn(mockTable, target)) return mockTable[target];
if (target !== id && mockTable && Object.hasOwn(mockTable, id)) return mockTable[id];
return baseRequire(target);
};
wrappedRequire.resolve = baseRequire.resolve.bind(baseRequire);
wrappedRequire.main = baseRequire.main;
wrappedRequire.extensions = baseRequire.extensions;
wrappedRequire.cache = baseRequire.cache;
return wrappedRequire;
})();
try {
additionalGlobalVar = { React: baseRequire("react") };
} catch (_err) {
additionalGlobalVar = { React: {
createElement: (type, props, ...children) => ({
type,
props: {
...props,
children: children.length <= 1 ? children[0] : children
}
}),
Fragment: Symbol.for("react.fragment")
} };
}
const sandboxContext = {
exports: { default: {} },
module: { exports: {} },
process: {
...process,
env: {
...process.env,
...require_loadEnvFile.loadEnvFile(envVarOptions),
...additionalEnvVars
}
},
console,
require: mockedRequire,
...additionalGlobalVar
};
for (const key of NODE_GLOBALS) if (!(key in sandboxContext) && key in globalThis) sandboxContext[key] = globalThis[key];
return sandboxContext;
};
const parseFileContent = (fileContentString, options) => {
const sandboxContext = getSandBoxContext(options);
(0, node_vm.runInNewContext)(`"use strict";\n${fileContentString}`, sandboxContext);
const candidates = [
sandboxContext.exports?.default,
sandboxContext.module?.exports?.defaults,
sandboxContext.module?.exports?.default,
sandboxContext.module?.exports
];
let result;
for (const candidate of candidates) if (candidate && typeof candidate === "object" && Object.keys(candidate).length > 0) {
result = candidate;
break;
}
sandboxContext.require = void 0;
sandboxContext.process = void 0;
sandboxContext.React = void 0;
sandboxContext.module = void 0;
sandboxContext.exports = void 0;
return result;
};
//#endregion
exports.getSandBoxContext = getSandBoxContext;
exports.parseFileContent = parseFileContent;
//# sourceMappingURL=parseFileContent.cjs.map