Skip to content

Commit 9580d29

Browse files
authored
feat(config): import mud config with tsx (#3290)
1 parent ef67355 commit 9580d29

File tree

4 files changed

+40
-261
lines changed

4 files changed

+40
-261
lines changed

.changeset/calm-dodos-care.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@latticexyz/config": patch
3+
---
4+
5+
Replaced esbuild with tsx to load MUD config in an effort to resolve issues with loading multiple MUD configs in parallel.

packages/config/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@
4343
"@ark/util": "0.2.2",
4444
"@latticexyz/common": "workspace:*",
4545
"@latticexyz/schema-type": "workspace:*",
46-
"esbuild": "^0.17.15",
47-
"find-up": "^6.3.0"
46+
"find-up": "^6.3.0",
47+
"tsx": "^4.19.1"
4848
},
4949
"devDependencies": {
5050
"tsup": "^6.7.0",

packages/config/src/deprecated/node/loadConfig.ts

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,31 @@
11
import { findUp } from "find-up";
22
import path from "path";
3-
import esbuild from "esbuild";
4-
import { rmSync } from "fs";
53
import { pathToFileURL } from "url";
64
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";
98

109
// In order of preference files are checked
1110
/** @deprecated */
1211
const configFiles = ["mud.config.js", "mud.config.mjs", "mud.config.ts", "mud.config.mts"];
13-
/** @deprecated */
14-
const TEMP_CONFIG = "mud.config.temp.mjs";
1512

1613
/** @deprecated */
1714
export async function loadConfig(configPath?: string): Promise<unknown> {
1815
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;
3825
}
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;
3929
}
4030

4131
/** @deprecated */

0 commit comments

Comments
 (0)