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: replace JSON.parse with typescript builtin config parser #63

Merged
merged 2 commits into from
Jul 15, 2024
Merged
Changes from 1 commit
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
29 changes: 21 additions & 8 deletions lib/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ import { glob } from 'glob'
import { findUp } from 'find-up'
import { createRequire } from 'node:module'
import { join, dirname } from 'node:path'
import { access, readFile } from 'node:fs/promises'
import { access } from 'node:fs/promises'
import { execa } from 'execa'
import { parseJsonConfigFileContent, readConfigFile, sys } from 'typescript'

function deferred () {
let resolve
Expand All @@ -17,7 +18,7 @@ function deferred () {
}

function enableSourceMapSupport (tsconfig) {
if (!tsconfig?.compilerOptions?.sourceMap) {
if (!tsconfig?.options?.sourceMap) {
return
}

Expand Down Expand Up @@ -56,13 +57,19 @@ export default async function runWithTypeScript (config) {
}

if (tsconfigPath && config.typescript !== false) {
const tsconfig = JSON.parse(await readFile(tsconfigPath))
const configFile = readConfigFile(tsconfigPath, sys.readFile)
const tsconfig = parseJsonConfigFileContent(
configFile.config,
sys,
dirname(tsconfigPath)
)

const _require = createRequire(tsconfigPath)
const typescriptPathCWD = _require.resolve('typescript')
tscPath = join(typescriptPathCWD, '..', '..', 'bin', 'tsc')
const outDir = tsconfig.compilerOptions.outDir
const outDir = tsconfig.options.outDir
if (outDir) {
prefix = join(dirname(tsconfigPath), outDir)
prefix = outDir
}

enableSourceMapSupport(tsconfig)
Expand All @@ -73,7 +80,7 @@ export default async function runWithTypeScript (config) {

// Watch is handled aftterwards
if (!config.watch) {
if (Array.isArray(tsconfig.references) && tsconfig.references.length > 0) {
if (Array.isArray(tsconfig.projectReferences) && tsconfig.projectReferences.length > 0) {
typescriptCliArgs.push('--build')
}
const start = Date.now()
Expand Down Expand Up @@ -123,8 +130,14 @@ export default async function runWithTypeScript (config) {
p = deferred()
let outDir = ''
if (config['post-compile'] && tsconfigPath) {
const tsconfig = JSON.parse(await readFile(tsconfigPath))
outDir = tsconfig.compilerOptions.outDir
const configFile = readConfigFile(tsconfigPath, sys.readFile)
const tsconfig = parseJsonConfigFileContent(
configFile.config,
sys,
dirname(tsconfigPath)
)

outDir = tsconfig.options.outDir

enableSourceMapSupport(tsconfig)
}
Expand Down
Loading