Skip to content

Commit

Permalink
another attempt
Browse files Browse the repository at this point in the history
  • Loading branch information
holic committed Aug 29, 2024
1 parent d6b1770 commit 9b9cdcc
Showing 1 changed file with 18 additions and 14 deletions.
32 changes: 18 additions & 14 deletions packages/config/src/deprecated/node/loadConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,36 +14,40 @@ const configFiles = ["mud.config.js", "mud.config.mjs", "mud.config.ts", "mud.co
/** @deprecated */
const TEMP_CONFIG = "mud.config.temp.mjs";

function prepareWindowsPath(fullPath: string): string {
if (os.platform() === "win32") {
// Add `file:///` for Windows support
// (see https://github.com/nodejs/node/issues/31710)
return pathToFileURL(
// undo unc prefix we added in `resolveConfigPath`
fullPath.replace(/^\/\/\?\//, ""),
).href;
}
return fullPath;
}

/** @deprecated */
export async function loadConfig(configPath?: string): Promise<unknown> {
configPath = await resolveConfigPath(configPath);
const inputPath = await resolveConfigPath(configPath);
const outputPath = path.join(path.dirname(inputPath), TEMP_CONFIG);
try {
await esbuild.build({
entryPoints: [configPath],
entryPoints: [prepareWindowsPath(inputPath)],
format: "esm",
outfile: TEMP_CONFIG,
outfile: prepareWindowsPath(outputPath),
// https://esbuild.github.io/getting-started/#bundling-for-node
platform: "node",
// bundle local imports (otherwise it may error, js can't import ts)
bundle: true,
// avoid bundling external imports (it's unnecessary and esbuild can't handle all node features)
packages: "external",
});
let importPath = await resolveConfigPath(TEMP_CONFIG);
// Add `file:///` for Windows support
// (see https://github.com/nodejs/node/issues/31710)
if (os.platform() === "win32") {
importPath = pathToFileURL(
// undo unc prefix we added in `resolveConfigPath`
importPath.replace(/^\/\/\?\//, ""),
).href;
}
// Node.js caches dynamic imports, so without appending a cache breaking
// param like `?update={Date.now()}` this import always returns the same config
// if called multiple times in a single process, like the `dev-contracts` cli
return (await import(`${importPath}?update=${Date.now()}`)).default;
return (await import(`${prepareWindowsPath(outputPath)}?update=${Date.now()}`)).default;
} finally {
rmSync(TEMP_CONFIG, { force: true });
rmSync(prepareWindowsPath(outputPath), { force: true });
}
}

Expand Down

0 comments on commit 9b9cdcc

Please sign in to comment.