From b64304970c93f22cbe11bb04464ef7b617e1d037 Mon Sep 17 00:00:00 2001 From: Amit Dahan Date: Thu, 30 Jun 2022 21:17:11 +0300 Subject: [PATCH] feat: custom tsconfig path via `ESBK_TSCONFIG_PATH` (#14) Co-authored-by: Hiroki Osame --- README.md | 9 +++++++++ package.json | 2 +- pnpm-lock.yaml | 8 ++++---- src/index.ts | 16 ++++++++++++++-- .../tsconfig-custom/tsconfig.custom-name.json | 6 ++++++ tests/specs/typescript/tsconfig.ts | 11 +++++++++++ tests/utils/node-with-loader.ts | 5 +++++ 7 files changed, 50 insertions(+), 7 deletions(-) create mode 100644 tests/fixtures/tsconfig/tsconfig-custom/tsconfig.custom-name.json diff --git a/README.md b/README.md index d460ac9..e46b47d 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/package.json b/package.json index 838f80d..7f80469 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 476fe90..8eacb70 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -8,7 +8,7 @@ specifiers: eslint: ^8.15.0 execa: ^6.1.0 get-node: ^12.1.0 - get-tsconfig: ^4.0.5 + get-tsconfig: ^4.1.0 manten: ^0.1.0 pkgroll: ^1.3.0 semver: ^7.3.7 @@ -17,7 +17,7 @@ specifiers: 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_4hx5bygx4rxgd7xwyndf6ymwce @@ -2018,8 +2018,8 @@ packages: get-intrinsic: 1.1.1 dev: true - /get-tsconfig/4.0.5: - resolution: {integrity: sha512-UiOHySG2zoM0krlrfJOMQoY5UR1Z57/HjMUJdi7lJHCLKiES9zZsDXtU0BPo2GUI5EqJmNRnqU3FdMjDmf2XaA==} + /get-tsconfig/4.1.0: + resolution: {integrity: sha512-bhshxJhpfmeQ8x4fAvDqJV2VfGp5TfHdLpmBpNZZhMoVyfIrOippBW4mayC3DT9Sxuhcyl56Efw61qL28hG4EQ==} dev: false /glob-parent/5.1.2: diff --git a/src/index.ts b/src/index.ts index 9964f7c..85c2cc0 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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); diff --git a/tests/fixtures/tsconfig/tsconfig-custom/tsconfig.custom-name.json b/tests/fixtures/tsconfig/tsconfig-custom/tsconfig.custom-name.json new file mode 100644 index 0000000..b437804 --- /dev/null +++ b/tests/fixtures/tsconfig/tsconfig-custom/tsconfig.custom-name.json @@ -0,0 +1,6 @@ +{ + "extends": "../tsconfig.json", + "compilerOptions": { + "jsxFactory": "console.error" + } +} diff --git a/tests/specs/typescript/tsconfig.ts b/tests/specs/typescript/tsconfig.ts index 4155798..317d2cb 100644 --- a/tests/specs/typescript/tsconfig.ts +++ b/tests/specs/typescript/tsconfig.ts @@ -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', { diff --git a/tests/utils/node-with-loader.ts b/tests/utils/node-with-loader.ts index 0d8e105..a1363be 100644 --- a/tests/utils/node-with-loader.ts +++ b/tests/utils/node-with-loader.ts @@ -7,6 +7,7 @@ type Options = { args: string[]; nodePath: string; cwd?: string; + env?: NodeJS.ProcessEnv; nodeOptions?: string[]; }; @@ -18,6 +19,7 @@ export const nodeWithLoader = async ( { env: { ESBK_DISABLE_CACHE: '1', + ...options.env, }, nodeOptions: [ ...(options.nodeOptions ?? []), @@ -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, }, );