Skip to content

Commit

Permalink
feat(cli): more default config search & TS support
Browse files Browse the repository at this point in the history
  • Loading branch information
PaperStrike committed Oct 22, 2023
1 parent f78d7cd commit 0b247f2
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 22 deletions.
20 changes: 9 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ Path to config file. The CLI checks these files by default:

```ts
[
'package.json',
'package.json', // "wrightplay" property
'.wrightplayrc',
'.wrightplayrc.json',
'.wrightplayrc.ts',
Expand All @@ -265,6 +265,14 @@ Path to config file. The CLI checks these files by default:
'.wrightplayrc.js',
'.wrightplayrc.mjs',
'.wrightplayrc.cjs',
'.config/wrightplayrc',
'.config/wrightplayrc.json',
'.config/wrightplayrc.ts',
'.config/wrightplayrc.mts',
'.config/wrightplayrc.cts',
'.config/wrightplayrc.js',
'.config/wrightplayrc.mjs',
'.config/wrightplayrc.cjs',
'wrightplay.config.ts',
'wrightplay.config.mts',
'wrightplay.config.cts',
Expand All @@ -274,16 +282,6 @@ Path to config file. The CLI checks these files by default:
]
```

#### TypeScript Config File Notice

Although the CLI checks TypeScript config files, it doesn't handle the load process differently. In most cases, using TypeScript config file will result in an `ERR_UNKNOWN_FILE_EXTENSION` error. However, you can use a custom [Node.js module loader](https://nodejs.org/api/cli.html#--experimental-loadermodule) to parse them properly:

```shell
cross-env NODE_OPTIONS="--loader ts-node/esm" wrightplay
```

Loader is not required for TypeScript [test files](#tests) and [entry points](#entrypoints). It is only required for TypeScript config files.

### setup

```shell
Expand Down
14 changes: 14 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
"esbuild": "~0.19.5",
"get-port": "^7.0.0",
"globby": "^13.2.2",
"jiti": "^1.20.0",
"lilconfig": "^2.1.0",
"mrmime": "^1.0.1",
"playwright": "^1.39.0",
Expand Down
38 changes: 28 additions & 10 deletions src/configSearcher.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
import { pathToFileURL } from 'node:url';
import { lilconfig } from 'lilconfig';
import jiti from 'jiti';

const moduleName = 'wrightplay';

const load = async (specifier: string) => (
(await import(pathToFileURL(specifier).href) as { default: unknown }).default
);
const tsLoader = jiti('', {
interopDefault: true,
});

const configLoader = (filepath: string) => {
try {
return tsLoader(filepath) as unknown;
} catch (error) {
throw new Error(`Failed to load TS config ${filepath}`, {
cause: error,
});
}
};

const searcher = lilconfig(moduleName, {
searchPlaces: [
Expand All @@ -18,6 +28,14 @@ const searcher = lilconfig(moduleName, {
`.${moduleName}rc.js`,
`.${moduleName}rc.mjs`,
`.${moduleName}rc.cjs`,
`.config/${moduleName}rc`,
`.config/${moduleName}rc.json`,
`.config/${moduleName}rc.ts`,
`.config/${moduleName}rc.mts`,
`.config/${moduleName}rc.cts`,
`.config/${moduleName}rc.js`,
`.config/${moduleName}rc.mjs`,
`.config/${moduleName}rc.cjs`,
`${moduleName}.config.ts`,
`${moduleName}.config.mts`,
`${moduleName}.config.cts`,
Expand All @@ -26,12 +44,12 @@ const searcher = lilconfig(moduleName, {
`${moduleName}.config.cjs`,
],
loaders: {
'.js': load,
'.mjs': load,
'.cjs': load,
'.ts': load,
'.mts': load,
'.cts': load,
'.js': configLoader,
'.mjs': configLoader,
'.cjs': configLoader,
'.ts': configLoader,
'.mts': configLoader,
'.cts': configLoader,
},
});

Expand Down
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"exactOptionalPropertyTypes": true,
"forceConsistentCasingInFileNames": true,
"lib": ["ESNext", "DOM"],
"module": "NodeNext",
"module": "ESNext",
"moduleResolution": "Bundler",
"outDir": "./build",
"rootDir": "./src",
"skipLibCheck": true,
Expand Down

0 comments on commit 0b247f2

Please sign in to comment.