Skip to content
This repository was archived by the owner on Oct 18, 2023. It is now read-only.

Commit 834583b

Browse files
feat: custom tsconfig path via ESBK_TSCONFIG_PATH (#31)
Co-authored-by: Hiroki Osame <[email protected]>
1 parent 5787f15 commit 834583b

File tree

7 files changed

+48
-7
lines changed

7 files changed

+48
-7
lines changed

README.md

+9
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,15 @@ The following properties are used from `tsconfig.json` in the working directory:
3434
- `jsxFactory`
3535
- `jsxFragmentFactory`
3636

37+
#### Custom `tsconfig.json` path
38+
By default, `tsconfig.json` will be detected from the current working directory.
39+
40+
To set a custom path, use the `ESBK_TSCONFIG_PATH` environment variable:
41+
42+
```sh
43+
ESBK_TSCONFIG_PATH=./path/to/tsconfig.custom.json node --loader @esbuild/esm-loader ./file.ts
44+
```
45+
3746
### Cache
3847
Modules transformations are cached in the system cache directory ([`TMPDIR`](https://en.wikipedia.org/wiki/TMPDIR)). Transforms are cached by content hash so duplicate dependencies are not re-transformed.
3948

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
},
3333
"dependencies": {
3434
"@esbuild-kit/core-utils": "^2.0.0",
35-
"get-tsconfig": "^4.0.5"
35+
"get-tsconfig": "^4.1.0"
3636
},
3737
"devDependencies": {
3838
"@pvtnbr/eslint-config": "^0.22.0",

pnpm-lock.yaml

+4-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/utils.ts

+13-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,21 @@
11
import path from 'path';
22
import { installSourceMapSupport } from '@esbuild-kit/core-utils';
3-
import { getTsconfig, createPathsMatcher } from 'get-tsconfig';
3+
import {
4+
getTsconfig,
5+
parseTsconfig,
6+
createPathsMatcher,
7+
} from 'get-tsconfig';
48

59
export const sourcemaps = installSourceMapSupport();
610

7-
const tsconfig = getTsconfig();
11+
const tsconfig = (
12+
process.env.ESBK_TSCONFIG_PATH
13+
? {
14+
path: process.env.ESBK_TSCONFIG_PATH,
15+
config: parseTsconfig(process.env.ESBK_TSCONFIG_PATH),
16+
}
17+
: getTsconfig()
18+
);
819

920
export const tsconfigRaw = tsconfig?.config;
1021
export const tsconfigPathsMatcher = tsconfig && createPathsMatcher(tsconfig);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"extends": "../tsconfig.json",
3+
"compilerOptions": {
4+
"jsxFactory": "console.error"
5+
}
6+
}

tests/specs/typescript/tsconfig.ts

+11
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,17 @@ export default testSuite(async ({ describe }, node: NodeApis) => {
1010
expect(nodeProcess.stdout).toBe('div null hello world\nnull null goodbye world');
1111
});
1212

13+
test('Custom tsconfig.json path', async () => {
14+
const nodeProcess = await node.load('./src/tsx.tsx', {
15+
cwd: './tsconfig',
16+
env: {
17+
ESBK_TSCONFIG_PATH: './tsconfig-custom/tsconfig.custom-name.json',
18+
},
19+
});
20+
expect(nodeProcess.stdout).toBe('');
21+
expect(nodeProcess.stderr).toMatch('div null hello world\nnull null goodbye world');
22+
});
23+
1324
describe('paths', ({ test, describe }) => {
1425
test('resolves baseUrl', async () => {
1526
const nodeProcess = await node.load('./src/base-url.ts', {

tests/utils/node-with-loader.ts

+4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ type Options = {
77
args: string[];
88
nodePath: string;
99
cwd?: string;
10+
env?: NodeJS.ProcessEnv;
1011
nodeOptions?: string[];
1112
};
1213

@@ -20,6 +21,7 @@ export const nodeWithLoader = (
2021
{
2122
env: {
2223
ESBK_DISABLE_CACHE: '1',
24+
...options.env,
2325
},
2426
nodeOptions: [
2527
...(options.nodeOptions ?? []),
@@ -47,6 +49,7 @@ export async function createNode(
4749
filePath: string,
4850
options?: {
4951
cwd?: string;
52+
env?: typeof process.env;
5053
nodeOptions?: string[];
5154
},
5255
) {
@@ -55,6 +58,7 @@ export async function createNode(
5558
args: [filePath],
5659
nodePath: node.path,
5760
cwd: path.join(fixturePath, options?.cwd ?? ''),
61+
env: options?.env,
5862
nodeOptions: options?.nodeOptions,
5963
},
6064
);

0 commit comments

Comments
 (0)