Skip to content

Commit

Permalink
Support ESM and .mts/.cts extensions with jiti loader (#1286)
Browse files Browse the repository at this point in the history
* Support ESM and .mts/.cts extensions with jiti loader

* Add more tests

* Get rid of cosmiconfig-typescript-loader

* some fixes

---------

Co-authored-by: Dimitri POSTOLOV <dmytropostolov@gmail.com>
  • Loading branch information
beerose and dimaMachina authored Mar 6, 2023
1 parent 87ba188 commit 85bc332
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 18 deletions.
6 changes: 1 addition & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,9 @@
},
"peerDependencies": {
"cosmiconfig-toml-loader": "^1.0.0",
"cosmiconfig-typescript-loader": "^4.0.0",
"graphql": "^0.11.0 || ^0.12.0 || ^0.13.0 || ^14.0.0 || ^15.0.0 || ^16.0.0"
},
"peerDependenciesMeta": {
"cosmiconfig-typescript-loader": {
"optional": true
},
"cosmiconfig-toml-loader": {
"optional": true
}
Expand All @@ -68,6 +64,7 @@
"@graphql-tools/url-loader": "^7.9.7",
"@graphql-tools/utils": "^9.0.0",
"cosmiconfig": "8.0.0",
"jiti": "1.17.1",
"minimatch": "4.2.3",
"string-env-interpolation": "1.0.1",
"tslib": "^2.4.0"
Expand All @@ -80,7 +77,6 @@
"@typescript-eslint/parser": "5.54.0",
"bob-the-bundler": "5.0.1",
"cosmiconfig-toml-loader": "1.0.0",
"cosmiconfig-typescript-loader": "4.3.0",
"del": "6.1.1",
"eslint": "8.35.0",
"eslint-config-prettier": "8.6.0",
Expand Down
2 changes: 2 additions & 0 deletions scripts/postbuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ await writeFile(
`
import { createRequire } from 'module';
const require = createRequire(import.meta.url);
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
${content}`.trimStart(),
);
console.timeEnd('done');
16 changes: 12 additions & 4 deletions src/helpers/cosmiconfig.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { cosmiconfig, cosmiconfigSync, Loader, defaultLoaders } from 'cosmiconfig';
import { env } from 'string-env-interpolation';
import jiti from 'jiti';

export interface ConfigSearchResult {
config: any;
Expand Down Expand Up @@ -39,10 +40,11 @@ export function createCosmiConfigSync(moduleName: string, legacy: boolean) {
return cosmiconfigSync(moduleName, options);
}

const loadTypeScript: Loader = (...args) => {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const { TypeScriptLoader } = require('cosmiconfig-typescript-loader');
return TypeScriptLoader({ transpileOnly: true })(...args);
const loadTypeScript: Loader = (filepath) => {
const jitiLoader = jiti(__filename, {
interopDefault: true,
});
return jitiLoader(filepath);
};

const loadToml: Loader = (...args) => {
Expand All @@ -56,6 +58,8 @@ function prepareCosmiconfig(moduleName: string, legacy: boolean) {

const searchPlaces = [
'#.config.ts',
'#.config.cts',
'#.config.mts',
'#.config.js',
'#.config.cjs',
'#.config.json',
Expand All @@ -64,6 +68,8 @@ function prepareCosmiconfig(moduleName: string, legacy: boolean) {
'#.config.toml',
'.#rc',
'.#rc.ts',
'.#rc.cts',
'.#rc.mts',
'.#rc.js',
'.#rc.cjs',
'.#rc.json',
Expand All @@ -83,6 +89,8 @@ function prepareCosmiconfig(moduleName: string, legacy: boolean) {
searchPlaces: searchPlaces.map((place) => place.replace('#', moduleName)),
loaders: {
'.ts': loadTypeScript,
'.mts': loadTypeScript,
'.cts': loadTypeScript,
'.js': defaultLoaders['.js'],
'.json': createCustomLoader(defaultLoaders['.json']),
'.yaml': loadYaml,
Expand Down
40 changes: 40 additions & 0 deletions test/config.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ runTests({ async: loadConfig, sync: loadConfigSync })((load, mode) => {
let configFiles = [
// #.config files
[`${moduleName}.config.ts`, tsConfig],
[`${moduleName}.config.cts`, tsConfig],
[`${moduleName}.config.mts`, tsConfig],
[`${moduleName}.config.js`, jsConfig],
[`${moduleName}.config.cjs`, jsConfig],
[`${moduleName}.config.json`, jsonConfig],
Expand All @@ -133,6 +135,8 @@ runTests({ async: loadConfig, sync: loadConfigSync })((load, mode) => {
// .#rc files
[`.${moduleName}rc`, yamlConfig],
[`.${moduleName}rc.ts`, tsConfig],
[`.${moduleName}rc.cts`, tsConfig],
[`.${moduleName}rc.mts`, tsConfig],
[`.${moduleName}rc.js`, jsConfig],
[`.${moduleName}rc.cjs`, jsConfig],
[`.${moduleName}rc.json`, jsonConfig],
Expand Down Expand Up @@ -173,6 +177,42 @@ runTests({ async: loadConfig, sync: loadConfigSync })((load, mode) => {
});
});

describe('"type": "module" in package.json', () => {
const schemaFile = 'schema.graphql';
const tsConfig = `export default { schema: '${schemaFile}' };`;

beforeEach(() => {
temp.clean();
temp.createFile(
schemaFile,
/* GraphQL */ `
type Query {
foo: String
}
`,
);
temp.createFile(
'package.json',
JSON.stringify({
type: 'module',
}),
);
});

const extensions = ['ts', 'cts', 'mts'];

test.each(extensions)('load a %s config', async (ext) => {
temp.createFile(`graphql.config.${ext}`, tsConfig);

const config = await load({ rootDir: temp.dir });

const loadedSchema = config.getDefault().schema;

expect(config).toBeDefined();
expect(loadedSchema).toEqual(schemaFile);
});
});

describe('environment variables', () => {
test('not defined but with a default value', async () => {
temp.createFile(
Expand Down
4 changes: 0 additions & 4 deletions website/src/pages/docs/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,6 @@ const config: IGraphQLConfig = {
export default config
```

<Callout type="warning">
**Note**: This config requires `cosmiconfig-typescript-loader`, `typescript` and `ts-node` to be installed.
</Callout>

### Custom Paths

Custom extension paths with `.mycustomrc.js`, `mycustom.config.yml`, etc. - any filename listed in [usage docs](usage) with `graphql` replaced by the `loadConfig(){:ts}` parameter [`configName`](load-config#configname).
Expand Down
10 changes: 5 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1298,11 +1298,6 @@ cosmiconfig-toml-loader@1.0.0:
dependencies:
"@iarna/toml" "^2.2.5"

cosmiconfig-typescript-loader@4.3.0:
version "4.3.0"
resolved "https://registry.yarnpkg.com/cosmiconfig-typescript-loader/-/cosmiconfig-typescript-loader-4.3.0.tgz#c4259ce474c9df0f32274ed162c0447c951ef073"
integrity sha512-NTxV1MFfZDLPiBMjxbHRwSh5LaLcPMwNdCutmnHJCKoVnlvldPWlllonKwrsRJ5pYZBIBGRWWU2tfvzxgeSW5Q==

cosmiconfig@8.0.0:
version "8.0.0"
resolved "https://registry.yarnpkg.com/cosmiconfig/-/cosmiconfig-8.0.0.tgz#e9feae014eab580f858f8a0288f38997a7bebe97"
Expand Down Expand Up @@ -2521,6 +2516,11 @@ iterall@^1.2.1:
resolved "https://registry.yarnpkg.com/iterall/-/iterall-1.3.0.tgz#afcb08492e2915cbd8a0884eb93a8c94d0d72fea"
integrity sha512-QZ9qOMdF+QLHxy1QIpUHUU1D5pS2CG2P69LF6L6CPjPYA/XMOmKV3PZpawHoAjHNyB0swdVTRxdYT4tbBbxqwg==

jiti@1.17.1:
version "1.17.1"
resolved "https://registry.yarnpkg.com/jiti/-/jiti-1.17.1.tgz#264daa43ee89a03e8be28c3d712ccc4eb9f1e8ed"
integrity sha512-NZIITw8uZQFuzQimqjUxIrIcEdxYDFIe/0xYfIlVXTkiBjjyBEvgasj5bb0/cHtPRD/NziPbT312sFrkI5ALpw==

joycon@^3.0.1:
version "3.1.1"
resolved "https://registry.yarnpkg.com/joycon/-/joycon-3.1.1.tgz#bce8596d6ae808f8b68168f5fc69280996894f03"
Expand Down

0 comments on commit 85bc332

Please sign in to comment.