Skip to content

Commit c8a8a54

Browse files
authored
Revert "Support ESM and .mts/.cts config extensions (#1281)" (#1285)
This reverts commit f6cfa2c.
1 parent 9e4f453 commit c8a8a54

File tree

1 file changed

+4
-69
lines changed

1 file changed

+4
-69
lines changed

src/helpers/cosmiconfig.ts

+4-69
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
1-
import { execSync } from 'child_process';
21
import { cosmiconfig, cosmiconfigSync, Loader, defaultLoaders } from 'cosmiconfig';
3-
import { createHash } from 'crypto';
4-
import { promises } from 'fs';
5-
import { tmpdir } from 'os';
6-
import { basename, join } from 'path';
72
import { env } from 'string-env-interpolation';
83

94
export interface ConfigSearchResult {
@@ -44,70 +39,12 @@ export function createCosmiConfigSync(moduleName: string, legacy: boolean) {
4439
return cosmiconfigSync(moduleName, options);
4540
}
4641

47-
const loadTypeScript: Loader = async (filepath, content) => {
48-
try {
49-
// eslint-disable-next-line @typescript-eslint/no-var-requires
50-
const { TypeScriptLoader } = require('cosmiconfig-typescript-loader');
51-
return TypeScriptLoader({ transpileOnly: true })(filepath, content);
52-
} catch (err) {
53-
if (isRequireESMError(err)) {
54-
const hash = createHash('sha256').update(content).digest('base64url');
55-
56-
const tempDir = join(tmpdir(), `graphql-config`);
57-
58-
let inTempDir: string[] = [];
59-
try {
60-
inTempDir = await promises.readdir(tempDir);
61-
} catch (err) {
62-
if (err.code === 'ENOENT') {
63-
// tsc will create the directory if it doesn't exist.
64-
} else {
65-
throw err;
66-
}
67-
}
68-
69-
let outDir = join(tempDir, new Date().getTime() + '-' + hash);
70-
const previousOutDir = inTempDir.find((s) => s.endsWith(hash));
71-
72-
if (previousOutDir) {
73-
outDir = join(tempDir, previousOutDir);
74-
} else {
75-
// We're compiling the file, because ts-node doesn't work perfectly with ESM.
76-
execSync(`tsc ${filepath} --module commonjs --outDir ${outDir} --skipLibCheck`);
77-
}
78-
79-
const newPath = join(outDir, basename(filepath).replace(/\.(m|c)?ts$/, '.js'));
80-
const config = import(newPath).then((m) => {
81-
const config = m.default;
82-
return 'default' in config ? config.default : config;
83-
});
84-
85-
// If the cache has more than 10 files, we delete the oldest one.
86-
await removeOldestDirInCache(inTempDir, tempDir, 10);
87-
88-
return config;
89-
}
90-
throw err;
91-
}
42+
const loadTypeScript: Loader = (...args) => {
43+
// eslint-disable-next-line @typescript-eslint/no-var-requires
44+
const { TypeScriptLoader } = require('cosmiconfig-typescript-loader');
45+
return TypeScriptLoader({ transpileOnly: true })(...args);
9246
};
9347

94-
async function removeOldestDirInCache(inTempDir: string[], tempDir: string, cacheLimit: number) {
95-
if (inTempDir.length > cacheLimit) {
96-
const oldest = inTempDir.sort((a, b) => {
97-
const aTime = Number(a.split('-')[0]);
98-
const bTime = Number(b.split('-')[0]);
99-
100-
return aTime - bTime;
101-
})[0];
102-
103-
await promises.rm(join(tempDir, oldest), { recursive: true, force: true });
104-
}
105-
}
106-
107-
function isRequireESMError(err: any) {
108-
return typeof err.stack === 'string' && err.stack.startsWith('Error [ERR_REQUIRE_ESM]:');
109-
}
110-
11148
const loadToml: Loader = (...args) => {
11249
// eslint-disable-next-line @typescript-eslint/no-var-requires
11350
const { loadToml } = require('cosmiconfig-toml-loader');
@@ -146,8 +83,6 @@ function prepareCosmiconfig(moduleName: string, legacy: boolean) {
14683
searchPlaces: searchPlaces.map((place) => place.replace('#', moduleName)),
14784
loaders: {
14885
'.ts': loadTypeScript,
149-
'.mts': loadTypeScript,
150-
'.cts': loadTypeScript,
15186
'.js': defaultLoaders['.js'],
15287
'.json': createCustomLoader(defaultLoaders['.json']),
15388
'.yaml': loadYaml,

0 commit comments

Comments
 (0)