Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(register): always resolve full file path for tsconfig and add paths if available #646

Merged
merged 5 commits into from
Apr 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions packages/register/__test__/read-default-config.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { join, dirname } from 'path'

import test from 'ava'
import { omit } from 'lodash'
import sinon from 'sinon'
import * as ts from 'typescript'

import { readDefaultTsConfig } from '../read-default-tsconfig'
Expand Down Expand Up @@ -46,3 +47,39 @@ test('should RESPECT tsconfig path in subdirectory', (t) => {
const { options } = ts.parseJsonConfigFileContent(config, ts.sys, dirname(configPath))
t.deepEqual(omit(defaultOptions, 'files'), options)
})

test('should return default compiler options when the tsConfigPath is invalid', (t) => {
const configPath = join(__dirname, 'invalid', 'tsconfig.json')

delete process.env.SWC_NODE_PROJECT
delete process.env.TS_NODE_PROJECT
process.env.TS_NODE_PROJECT = configPath

const defaultOptions = readDefaultTsConfig()
t.deepEqual(defaultOptions, {
target: ts.ScriptTarget.ES2018,
module: ts.ModuleKind.CommonJS,
moduleResolution: ts.ModuleResolutionKind.NodeJs,
sourceMap: true,
esModuleInterop: true,
})
})

test('should RESPECT tsconfig path in subdirectory with a relative path', (t) => {
const configPath = join('..', '__test__', 'tsconfig.spec.json')
const fullConfigPath = join(__dirname, 'tsconfig.spec.json')

delete process.env.SWC_NODE_PROJECT
delete process.env.TS_NODE_PROJECT
process.env.TS_NODE_PROJECT = configPath

sinon.replace(process, 'cwd', () => __dirname)

const defaultOptions = readDefaultTsConfig()

sinon.restore()

const { config } = ts.readConfigFile(fullConfigPath, ts.sys.readFile)
const { options } = ts.parseJsonConfigFileContent(config, ts.sys, dirname(fullConfigPath))
t.deepEqual(omit(defaultOptions, 'files'), options)
})
40 changes: 24 additions & 16 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, dirname } from 'path'
import { join, dirname, resolve } from 'path'

import type { Options } from '@swc-node/core'
import { yellow } from 'colorette'
Expand All @@ -19,24 +19,31 @@ export function readDefaultTsConfig(
esModuleInterop: true,
}

if (tsConfigPath && existsSync(tsConfigPath)) {
try {
debug(`Read config file from ${tsConfigPath}`)
const { config } = ts.readConfigFile(tsConfigPath, ts.sys.readFile)
if (!tsConfigPath) {
return compilerOptions
}

const fullTsConfigPath = resolve(tsConfigPath)

if (!existsSync(fullTsConfigPath)) {
return compilerOptions
}

try {
debug(`Read config file from ${fullTsConfigPath}`)
const { config } = ts.readConfigFile(fullTsConfigPath, ts.sys.readFile)

const { options, errors, fileNames } = ts.parseJsonConfigFileContent(config, ts.sys, dirname(tsConfigPath))
if (!errors.length) {
compilerOptions = options
compilerOptions.files = fileNames
} else {
console.info(
yellow(`Convert compiler options from json failed, ${errors.map((d) => d.messageText).join('\n')}`),
)
}
} catch (e) {
console.info(yellow(`Read ${tsConfigPath} failed: ${(e as Error).message}`))
const { options, errors, fileNames } = ts.parseJsonConfigFileContent(config, ts.sys, dirname(fullTsConfigPath))
if (!errors.length) {
compilerOptions = options
compilerOptions.files = fileNames
} else {
console.info(yellow(`Convert compiler options from json failed, ${errors.map((d) => d.messageText).join('\n')}`))
}
} catch (e) {
console.info(yellow(`Read ${tsConfigPath} failed: ${(e as Error).message}`))
}

return compilerOptions
}

Expand Down Expand Up @@ -118,5 +125,6 @@ export function tsCompilerOptionsToSwcConfig(options: ts.CompilerOptions, filena
dynamicImport: true,
esModuleInterop: options.esModuleInterop ?? false,
keepClassNames: true,
paths: options.paths as Options['paths'],
}
}
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"lib": ["dom", "DOM.Iterable", "ES5", "ES2015", "ES2016", "ES2017", "ES2018", "ES2019", "ES2020", "esnext"],
"baseUrl": "./packages",
"paths": {
"@swc-node/*": ["./*/src/index.ts"],
"@swc-node/core": ["@swc-node/core/index.ts"],
"@swc-node/register/*": ["./register/*"]
}
},
Expand Down