Skip to content

Commit

Permalink
feat: support --tsconfig cli flag (#51)
Browse files Browse the repository at this point in the history
* Add support for `--tsconfig=tsconfig.custom.json`

* Test for `--tsconfig=tsconfig.custom.json` too

* refactor

* style

* chore: use stable loaders

* chore: use stable type-flag

* revert: irrelevant changes

* revert: irrelevant changes

Co-authored-by: Hiroki Osame <hiroki.osame@gmail.com>
  • Loading branch information
amitdahan and privatenumber committed Jul 2, 2022
1 parent 9c427e1 commit 5f3c954
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 22 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,15 @@ Pass in a file to run:
tsx ./file.ts
```

#### Custom `tsconfig.json` path
By default, `tsconfig.json` will be detected from the current working directory.

To set a custom path, use the `--tsconfig` flag:

```sh
tsx --tsconfig ./path/to/tsconfig.custom.json ./file.ts
```

### Watch mode
Run file and automatically rerun on changes:

Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@
"prepublishOnly": "npm test"
},
"dependencies": {
"@esbuild-kit/cjs-loader": "^2.2.1",
"@esbuild-kit/core-utils": "^2.0.1",
"@esbuild-kit/esm-loader": "^2.3.1"
"@esbuild-kit/cjs-loader": "^2.3.0",
"@esbuild-kit/core-utils": "^2.0.2",
"@esbuild-kit/esm-loader": "^2.4.0"
},
"optionalDependencies": {
"fsevents": "~2.3.2"
Expand Down
36 changes: 18 additions & 18 deletions pnpm-lock.yaml

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

5 changes: 5 additions & 0 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ const tsxFlags = {
type: Boolean,
description: 'Disable caching',
},
tsconfig: {
type: String,
description: 'Custom tsconfig.json path',
},
};

const flags = {
Expand Down Expand Up @@ -60,6 +64,7 @@ cli({

run(args, {
noCache: Boolean(argv.flags.noCache),
tsconfigPath: argv.flags.tsconfig,
}).on(
'close',
code => process.exit(code!),
Expand Down
5 changes: 5 additions & 0 deletions src/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export function run(
argv: string[],
options?: {
noCache?: boolean;
tsconfigPath?: string;
ipc?: boolean;
},
) {
Expand All @@ -21,6 +22,10 @@ export function run(
environment.ESBK_DISABLE_CACHE = '1';
}

if (options.tsconfigPath) {
environment.ESBK_TSCONFIG_PATH = options.tsconfigPath;
}

if (options.ipc) {
// To communicate with parent process
stdio.push('ipc');
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": "../tsconfig.json",
"compilerOptions": {
"jsxFactory": "console.error"
}
}
9 changes: 9 additions & 0 deletions tests/specs/typescript/tsconfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@ export default testSuite(async ({ describe }, node: NodeApis) => {
expect(nodeProcess.stdout).toBe('div null hello world\nnull null goodbye world');
});

test('Custom tsconfig.json path', async () => {
const nodeProcess = await node.load('./src/tsx.tsx', {
cwd: './tsconfig',
args: ['--tsconfig', './tsconfig-custom/tsconfig.custom-name.json'],
});
expect(nodeProcess.stdout).toBe('');
expect(nodeProcess.stderr).toBe('div null hello world\nnull null goodbye world');
});

describe('paths', ({ test, describe }) => {
test('resolves baseUrl', async () => {
const nodeProcess = await node.load('./src/base-url.ts', {
Expand Down
6 changes: 5 additions & 1 deletion tests/utils/tsx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,15 @@ export async function createNode(
filePath: string,
options?: {
cwd?: string;
args?: string[];
},
) {
return tsx(
{
args: [filePath],
args: [
...(options?.args ?? []),
filePath,
],
nodePath: node.path,
cwd: path.join(fixturePath, options?.cwd ?? ''),
},
Expand Down

0 comments on commit 5f3c954

Please sign in to comment.