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

Commit

Permalink
feat: custom tsconfig path via ESBK_TSCONFIG_PATH (#14)
Browse files Browse the repository at this point in the history
Co-authored-by: Hiroki Osame <hiroki.osame@gmail.com>
  • Loading branch information
amitdahan and privatenumber authored Jun 30, 2022
1 parent e6e1fce commit b643049
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 7 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,15 @@ The following properties are used from `tsconfig.json` in the working directory:
- `jsxFactory`
- `jsxFragmentFactory`

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

To set a custom path, use the `ESBK_TSCONFIG_PATH` environment variable:

```sh
ESBK_TSCONFIG_PATH=./path/to/tsconfig.custom.json node -r @esbuild/cjs-loader ./file.js
```

### Cache
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.

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
},
"dependencies": {
"@esbuild-kit/core-utils": "^2.0.0",
"get-tsconfig": "^4.0.5"
"get-tsconfig": "^4.1.0"
},
"devDependencies": {
"@pvtnbr/eslint-config": "^0.22.0",
Expand Down
8 changes: 4 additions & 4 deletions pnpm-lock.yaml

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

16 changes: 14 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,25 @@ import {
transformDynamicImport,
applySourceMap,
} from '@esbuild-kit/core-utils';
import { getTsconfig, createPathsMatcher } from 'get-tsconfig';
import {
getTsconfig,
parseTsconfig,
createPathsMatcher,
} from 'get-tsconfig';

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

const tsconfig = getTsconfig();
const tsconfig = (
process.env.ESBK_TSCONFIG_PATH
? {
path: process.env.ESBK_TSCONFIG_PATH,
config: parseTsconfig(process.env.ESBK_TSCONFIG_PATH),
}
: getTsconfig()
);

const tsconfigRaw = tsconfig?.config;
const tsconfigPathsMatcher = tsconfig && createPathsMatcher(tsconfig);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": "../tsconfig.json",
"compilerOptions": {
"jsxFactory": "console.error"
}
}
11 changes: 11 additions & 0 deletions tests/specs/typescript/tsconfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,17 @@ 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',
env: {
ESBK_TSCONFIG_PATH: './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
5 changes: 5 additions & 0 deletions tests/utils/node-with-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ type Options = {
args: string[];
nodePath: string;
cwd?: string;
env?: NodeJS.ProcessEnv;
nodeOptions?: string[];
};

Expand All @@ -18,6 +19,7 @@ export const nodeWithLoader = async (
{
env: {
ESBK_DISABLE_CACHE: '1',
...options.env,
},
nodeOptions: [
...(options.nodeOptions ?? []),
Expand All @@ -44,14 +46,17 @@ export async function createNode(
filePath: string,
options?: {
cwd?: string;
env?: typeof process.env;
nodeOptions?: string[];
},
) {
return nodeWithLoader(
{

args: [filePath],
nodePath: node.path,
cwd: path.join(fixturePath, options?.cwd ?? ''),
env: options?.env,
nodeOptions: options?.nodeOptions,
},
);
Expand Down

0 comments on commit b643049

Please sign in to comment.