@intlayer/config
Version:
Retrieve Intlayer configurations and manage environment variables for both server-side and client-side environments.
101 lines • 3.96 kB
JavaScript
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var loadExternalFile_exports = {};
__export(loadExternalFile_exports, {
loadExternalFile: () => loadExternalFile
});
module.exports = __toCommonJS(loadExternalFile_exports);
var import_esbuild = require("esbuild");
var import_path = require("path");
var import_vm = require("vm");
var import_getSandboxContext = require('./getSandboxContext.cjs');
var import_logger = require('./logger.cjs');
var import_ESMxCJSHelpers = require('./utils/ESMxCJSHelpers.cjs');
const getTransformationOptions = (filePath) => ({
loader: {
".js": "js",
".jsx": "jsx",
".mjs": "js",
".ts": "ts",
".tsx": "tsx",
".cjs": "js",
".json": "json",
".md": "text",
".mdx": "text"
},
format: "cjs",
// Output format as commonjs
target: "es2017",
packages: "external",
write: false,
bundle: true,
banner: {
js: [
`globalThis.intlayer_file_path = ${JSON.stringify(filePath)};`,
`globalThis.intlayer_file_dir = ${JSON.stringify((0, import_path.dirname)(filePath))};`
].join("\n")
}
});
const loadExternalFile = (filePath, envVarOptions, projectRequire = import_ESMxCJSHelpers.ESMxCJSRequire) => {
let fileContent = void 0;
const fileExtension = filePath.split(".").pop() ?? "";
try {
if (fileExtension === "json") {
delete import_ESMxCJSHelpers.ESMxCJSRequire.cache[import_ESMxCJSHelpers.ESMxCJSRequire.resolve(filePath)];
return (0, import_ESMxCJSHelpers.ESMxCJSRequire)(filePath);
}
const moduleResult = (0, import_esbuild.buildSync)({
entryPoints: [filePath],
...getTransformationOptions(filePath)
});
const moduleResultString = moduleResult.outputFiles?.[0].text;
if (!moduleResultString) {
(0, import_logger.logger)("File could not be loaded.", { level: "error" });
return void 0;
}
const sandboxContext = (0, import_getSandboxContext.getSandBoxContext)(envVarOptions, projectRequire);
(0, import_vm.runInNewContext)(moduleResultString, sandboxContext);
if (sandboxContext.exports.default && Object.keys(sandboxContext.exports.default).length > 0) {
fileContent = sandboxContext.exports.default;
} else if (sandboxContext.module.exports.defaults && Object.keys(sandboxContext.module.exports.defaults).length > 0) {
fileContent = sandboxContext.module.exports.default;
} else if (sandboxContext.module.exports.default && Object.keys(sandboxContext.module.exports.default).length > 0) {
fileContent = sandboxContext.module.exports.default;
} else if (sandboxContext.module.exports && Object.keys(sandboxContext.module.exports).length > 0) {
fileContent = sandboxContext.module.exports;
}
if (typeof fileContent === "undefined") {
(0, import_logger.logger)(`File file could not be loaded. Path : ${filePath}`);
return void 0;
}
return fileContent;
} catch (error) {
(0, import_logger.logger)(
`Error: ${error} ${JSON.stringify(error.stack, null, 2)}`,
{
level: "error"
}
);
}
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
loadExternalFile
});
//# sourceMappingURL=loadExternalFile.cjs.map