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

Commit b643049

Browse files
feat: custom tsconfig path via ESBK_TSCONFIG_PATH (#14)
Co-authored-by: Hiroki Osame <[email protected]>
1 parent e6e1fce commit b643049

File tree

7 files changed

+50
-7
lines changed

7 files changed

+50
-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 -r @esbuild/cjs-loader ./file.js
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
@@ -33,7 +33,7 @@
3333
},
3434
"dependencies": {
3535
"@esbuild-kit/core-utils": "^2.0.0",
36-
"get-tsconfig": "^4.0.5"
36+
"get-tsconfig": "^4.1.0"
3737
},
3838
"devDependencies": {
3939
"@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/index.ts

+14-2
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,25 @@ import {
88
transformDynamicImport,
99
applySourceMap,
1010
} from '@esbuild-kit/core-utils';
11-
import { getTsconfig, createPathsMatcher } from 'get-tsconfig';
11+
import {
12+
getTsconfig,
13+
parseTsconfig,
14+
createPathsMatcher,
15+
} from 'get-tsconfig';
1216

1317
const isPathPattern = /^\.{0,2}\//;
1418
const isTsFilePatten = /\.[cm]?tsx?$/;
1519
const nodeModulesPath = `${path.sep}node_modules${path.sep}`;
1620

17-
const tsconfig = getTsconfig();
21+
const tsconfig = (
22+
process.env.ESBK_TSCONFIG_PATH
23+
? {
24+
path: process.env.ESBK_TSCONFIG_PATH,
25+
config: parseTsconfig(process.env.ESBK_TSCONFIG_PATH),
26+
}
27+
: getTsconfig()
28+
);
29+
1830
const tsconfigRaw = tsconfig?.config;
1931
const tsconfigPathsMatcher = tsconfig && createPathsMatcher(tsconfig);
2032

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).toBe('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

+5
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

@@ -18,6 +19,7 @@ export const nodeWithLoader = async (
1819
{
1920
env: {
2021
ESBK_DISABLE_CACHE: '1',
22+
...options.env,
2123
},
2224
nodeOptions: [
2325
...(options.nodeOptions ?? []),
@@ -44,14 +46,17 @@ export async function createNode(
4446
filePath: string,
4547
options?: {
4648
cwd?: string;
49+
env?: typeof process.env;
4750
nodeOptions?: string[];
4851
},
4952
) {
5053
return nodeWithLoader(
5154
{
55+
5256
args: [filePath],
5357
nodePath: node.path,
5458
cwd: path.join(fixturePath, options?.cwd ?? ''),
59+
env: options?.env,
5560
nodeOptions: options?.nodeOptions,
5661
},
5762
);

0 commit comments

Comments
 (0)