|
1 | 1 | import { findUp } from "find-up"; |
2 | 2 | import path from "path"; |
3 | | -import esbuild from "esbuild"; |
4 | | -import { rmSync } from "fs"; |
5 | 3 | import { pathToFileURL } from "url"; |
6 | 4 | import os from "os"; |
7 | | - |
8 | | -// TODO: explore using https://www.npmjs.com/package/ts-import instead |
| 5 | +import fs from "fs/promises"; |
| 6 | +import { tsImport } from "tsx/esm/api"; |
| 7 | +import { require as tsRequire } from "tsx/cjs/api"; |
9 | 8 |
|
10 | 9 | // In order of preference files are checked |
11 | 10 | /** @deprecated */ |
12 | 11 | const configFiles = ["mud.config.js", "mud.config.mjs", "mud.config.ts", "mud.config.mts"]; |
13 | | -/** @deprecated */ |
14 | | -const TEMP_CONFIG = "mud.config.temp.mjs"; |
15 | 12 |
|
16 | 13 | /** @deprecated */ |
17 | 14 | export async function loadConfig(configPath?: string): Promise<unknown> { |
18 | 15 | configPath = await resolveConfigPath(configPath); |
19 | | - try { |
20 | | - await esbuild.build({ |
21 | | - entryPoints: [configPath], |
22 | | - format: "esm", |
23 | | - outfile: TEMP_CONFIG, |
24 | | - // https://esbuild.github.io/getting-started/#bundling-for-node |
25 | | - platform: "node", |
26 | | - // bundle local imports (otherwise it may error, js can't import ts) |
27 | | - bundle: true, |
28 | | - // avoid bundling external imports (it's unnecessary and esbuild can't handle all node features) |
29 | | - packages: "external", |
30 | | - }); |
31 | | - configPath = await resolveConfigPath(TEMP_CONFIG, true); |
32 | | - // Node.js caches dynamic imports, so without appending a cache breaking |
33 | | - // param like `?update={Date.now()}` this import always returns the same config |
34 | | - // if called multiple times in a single process, like the `dev-contracts` cli |
35 | | - return (await import(configPath + `?update=${Date.now()}`)).default; |
36 | | - } finally { |
37 | | - rmSync(TEMP_CONFIG, { force: true }); |
| 16 | + // load nearest package.json to figure out if we need to import with ESM or CJS |
| 17 | + const packageJsonPath = await findUp("package.json", { cwd: path.dirname(configPath) }); |
| 18 | + if (!packageJsonPath) throw new Error(`Could not find package.json for config at "${configPath}".`); |
| 19 | + const packageJson = JSON.parse(await fs.readFile(packageJsonPath, "utf8")); |
| 20 | + // use require if cjs |
| 21 | + if (!packageJson.type || packageJson.type === "commonjs") { |
| 22 | + // tsRequire has an internal cache, so we need to append data to reload the config |
| 23 | + // this helps with things like the mud dev runner that reevalutes the config on changes |
| 24 | + return tsRequire(`${configPath}?update=${Date.now()}`, import.meta.url).default; |
38 | 25 | } |
| 26 | + // otherwise default to esm |
| 27 | + // this is not cached, so we don't need to append anything to the config path |
| 28 | + return (await tsImport(configPath, import.meta.url)).default; |
39 | 29 | } |
40 | 30 |
|
41 | 31 | /** @deprecated */ |
|
0 commit comments