@intlayer/config
Version:
Retrieve Intlayer configurations and manage environment variables for both server-side and client-side environments.
68 lines (66 loc) • 1.64 kB
JavaScript
import { dirname, extname } from "node:path";
import { build, buildSync } from "esbuild";
import { pathToFileURL } from "node:url";
//#region src/loadExternalFile/bundleFile.ts
const getLoader = (extension) => {
switch (extension) {
case ".js": return "js";
case ".jsx": return "jsx";
case ".mjs": return "js";
case ".ts": return "ts";
case ".tsx": return "tsx";
case ".cjs": return "js";
case ".json": return "json";
case ".md": return "text";
case ".mdx": return "text";
default: return "js";
}
};
const getTransformationOptions = (filePath) => ({
loader: {
".js": "js",
".jsx": "jsx",
".mjs": "js",
".ts": "ts",
".tsx": "tsx",
".cjs": "js",
".json": "json",
".md": "text",
".mdx": "text"
},
format: "cjs",
target: "node16",
platform: "neutral",
write: false,
packages: "bundle",
external: ["esbuild"],
bundle: true,
define: { "import.meta.url": JSON.stringify(pathToFileURL(filePath).href) }
});
const bundleFileSync = (code, filePath, options) => {
return buildSync({
stdin: {
contents: code,
loader: getLoader(extname(filePath)),
resolveDir: dirname(filePath),
sourcefile: filePath
},
...getTransformationOptions(filePath),
...options
}).outputFiles?.[0].text;
};
const bundleFile = async (code, filePath, options) => {
return (await build({
stdin: {
contents: code,
loader: getLoader(extname(filePath)),
resolveDir: dirname(filePath),
sourcefile: filePath
},
...getTransformationOptions(filePath),
...options
})).outputFiles?.[0].text;
};
//#endregion
export { bundleFile, bundleFileSync, getLoader };
//# sourceMappingURL=bundleFile.mjs.map