@intlayer/config
Version:
Retrieve Intlayer configurations and manage environment variables for both server-side and client-side environments.
83 lines (81 loc) • 2.85 kB
JavaScript
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
const require_runtime = require('../_virtual/_rolldown/runtime.cjs');
const require_utils_getPackageJsonPath = require('../utils/getPackageJsonPath.cjs');
const require_loadExternalFile_bundleFile = require('./bundleFile.cjs');
let node_fs = require("node:fs");
let node_path = require("node:path");
let node_module = require("node:module");
let esbuild = require("esbuild");
let node_url = require("node:url");
//#region src/loadExternalFile/transpileTSToCJS.ts
const getTsConfigPath = (filePath) => {
const tsconfigPath = (0, node_path.join)(require_utils_getPackageJsonPath.getPackageJsonPath((0, node_path.dirname)(filePath)).baseDir, "tsconfig.json");
return (0, node_fs.existsSync)(tsconfigPath) ? tsconfigPath : void 0;
};
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: "node20",
platform: "node",
write: false,
packages: "external",
bundle: true,
tsconfig: getTsConfigPath(filePath),
define: {
"import.meta.url": JSON.stringify((0, node_url.pathToFileURL)(filePath).href),
"import.meta.env": "process.env"
}
});
const transpileTSToCJSSync = (code, filePath, options) => {
const loader = require_loadExternalFile_bundleFile.getLoader((0, node_path.extname)(filePath));
const { esbuildInstance, ...buildOptions } = options ?? {};
const esbuildBuildSync = esbuildInstance?.buildSync ?? esbuild.buildSync;
if (typeof globalThis.__filename !== "string") try {
const esbuildEntry = (0, node_module.createRequire)(require("url").pathToFileURL(__filename).href).resolve("esbuild");
globalThis.__filename = esbuildEntry;
globalThis.__dirname = (0, node_path.dirname)(esbuildEntry);
} catch {}
return esbuildBuildSync({
stdin: {
contents: code,
loader,
resolveDir: (0, node_path.dirname)(filePath),
sourcefile: filePath
},
...getTransformationOptions(filePath),
...buildOptions
}).outputFiles?.[0].text;
};
const transpileTSToCJS = async (code, filePath, options) => {
const loader = require_loadExternalFile_bundleFile.getLoader((0, node_path.extname)(filePath));
const { esbuildInstance, ...buildOptions } = options ?? {};
const ctx = await (esbuildInstance?.context ?? esbuild.context)({
stdin: {
contents: code,
loader,
resolveDir: (0, node_path.dirname)(filePath),
sourcefile: filePath
},
...getTransformationOptions(filePath),
...buildOptions
});
try {
return (await ctx.rebuild()).outputFiles?.[0].text;
} finally {
await ctx.dispose();
}
};
//#endregion
exports.transpileTSToCJS = transpileTSToCJS;
exports.transpileTSToCJSSync = transpileTSToCJSSync;
//# sourceMappingURL=transpileTSToCJS.cjs.map