Skip to content

Commit

Permalink
feat(register): support tsconfig in subdirectory
Browse files Browse the repository at this point in the history
  • Loading branch information
tbvjaos510 authored and Brooooooklyn committed Nov 8, 2021
1 parent 19da5ff commit 634d766
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
17 changes: 14 additions & 3 deletions packages/register/__test__/read-default-config.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { join } from 'path'
import { join, dirname } from 'path'

import test from 'ava'
import { omit } from 'lodash'
Expand All @@ -21,7 +21,7 @@ test('should RESPECT SWC_NODE_PROJECT env', (t) => {
process.env.SWC_NODE_PROJECT = configPath
const defaultOptions = readDefaultTsConfig()
const { config } = ts.readConfigFile(configPath, ts.sys.readFile)
const { options } = ts.parseJsonConfigFileContent(config, ts.sys, process.cwd())
const { options } = ts.parseJsonConfigFileContent(config, ts.sys, dirname(configPath))
t.deepEqual(omit(defaultOptions, 'files'), options)
})

Expand All @@ -32,6 +32,17 @@ test('should RESPECT TS_NODE_PROJECT env', (t) => {
process.env.TS_NODE_PROJECT = configPath
const defaultOptions = readDefaultTsConfig()
const { config } = ts.readConfigFile(configPath, ts.sys.readFile)
const { options } = ts.parseJsonConfigFileContent(config, ts.sys, process.cwd())
const { options } = ts.parseJsonConfigFileContent(config, ts.sys, dirname(configPath))
t.deepEqual(omit(defaultOptions, 'files'), options)
})

test('should RESPECT tsconfig path in subdirectory', (t) => {
const configPath = join(__dirname, 'subdirectory/tsconfig.extend.json')
delete process.env.SWC_NODE_PROJECT
delete process.env.TS_NODE_PROJECT
process.env.TS_NODE_PROJECT = configPath
const defaultOptions = readDefaultTsConfig()
const { config } = ts.readConfigFile(configPath, ts.sys.readFile)
const { options } = ts.parseJsonConfigFileContent(config, ts.sys, dirname(configPath))
t.deepEqual(omit(defaultOptions, 'files'), options)
})
7 changes: 7 additions & 0 deletions packages/register/__test__/subdirectory/tsconfig.extend.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "../tsconfig.spec.json",
"compilerOptions": {
"sourceMap": false
},
"include": ["../*.ts"]
}
4 changes: 2 additions & 2 deletions packages/register/read-default-tsconfig.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { existsSync } from 'fs'
import { join } from 'path'
import { join, dirname } from 'path'

import type { Options } from '@swc-node/core'
import chalk from 'chalk'
Expand All @@ -24,7 +24,7 @@ export function readDefaultTsConfig(
debug(`Read config file from ${tsConfigPath}`)
const { config } = ts.readConfigFile(tsConfigPath, ts.sys.readFile)

const { options, errors, fileNames } = ts.parseJsonConfigFileContent(config, ts.sys, process.cwd())
const { options, errors, fileNames } = ts.parseJsonConfigFileContent(config, ts.sys, dirname(tsConfigPath))
if (!errors.length) {
compilerOptions = options
compilerOptions.files = fileNames
Expand Down

0 comments on commit 634d766

Please sign in to comment.