Skip to content

Commit

Permalink
fix: dont drop ts-node options if there are no extends (#853)
Browse files Browse the repository at this point in the history
* fix: dont drop ts-node options if there are no extends

* fix: remove github warning
  • Loading branch information
mdonnalley authored Nov 6, 2023
1 parent 6062edf commit 039b42a
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 11 deletions.
9 changes: 2 additions & 7 deletions src/config/ts-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,16 +211,11 @@ export async function tsPath(root: string, orig: string | undefined, plugin?: Pl
memoizedWarn(
`${plugin?.name} is a linked ESM module and cannot be auto-transpiled. Existing compiled source will be used instead.`,
)

if (plugin?.options.url)
memoizedWarn(
`${plugin?.name} is an ESM module installed from github and cannot be auto-transpiled. Existing compiled source will be used instead.`,
)
return orig
}

// Do not skip ts-node registration if the plugin is linked or installed from github
if (settings.tsnodeEnabled === undefined && isProduction && plugin?.type !== 'link' && !plugin?.options.url) {
// Do not skip ts-node registration if the plugin is linked
if (settings.tsnodeEnabled === undefined && isProduction && plugin?.type !== 'link') {
debug(`Skipping ts-node registration for ${root} because NODE_ENV is NOT "test" or "development"`)
return orig
}
Expand Down
6 changes: 3 additions & 3 deletions src/util/fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import {Stats, existsSync as fsExistsSync, readFileSync} from 'node:fs'
import {readFile, stat} from 'node:fs/promises'
import {join} from 'node:path'

import {mergeNestedObjects} from './util'

export function requireJson<T>(...pathParts: string[]): T {
return JSON.parse(readFileSync(join(...pathParts), 'utf8'))
}
Expand Down Expand Up @@ -73,8 +75,6 @@ export function existsSync(path: string): boolean {
export async function readTSConfig(path: string) {
const {parse} = await import('tsconfck')
const result = await parse(path)
const tsNodeOpts = Object.fromEntries(
(result.extended ?? []).flatMap((e) => Object.entries(e.tsconfig['ts-node'] ?? {})).reverse(),
)
const tsNodeOpts = mergeNestedObjects(result.extended ?? [result], 'tsconfig.ts-node')
return {...result.tsconfig, 'ts-node': tsNodeOpts}
}
8 changes: 8 additions & 0 deletions src/util/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,11 @@ export function mapValues<T extends Record<string, any>, TResult>(
return o
}, {} as any)
}

function get(obj: Record<string, any>, path: string): unknown {
return path.split('.').reduce((o, p) => o?.[p], obj)
}

export function mergeNestedObjects(objs: Record<string, any>[], path: string): Record<string, any> {
return Object.fromEntries(objs.flatMap((o) => Object.entries(get(o, path) ?? {})).reverse())
}
34 changes: 33 additions & 1 deletion test/util/util.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {expect} from 'chai'

import {capitalize, castArray, isNotFalsy, isTruthy, last, maxBy, sumBy} from '../../src/util/util'
import {capitalize, castArray, isNotFalsy, isTruthy, last, maxBy, mergeNestedObjects, sumBy} from '../../src/util/util'

describe('capitalize', () => {
it('capitalizes the string', () => {
Expand Down Expand Up @@ -96,3 +96,35 @@ describe('castArray', () => {
expect(castArray()).to.deep.equal([])
})
})

describe('mergeNestedObjects', () => {
it('should merge nested objects', () => {
const a = {
tsconfig: {
compilerOptions: {
outDir: 'dist',
rootDir: 'src',
},
'ts-node': {
transpileOnly: true,
},
},
}

const b = {
tsconfig: {
compilerOptions: {
outDir: 'dist',
rootDir: 'src',
},
'ts-node': {
transpileOnly: false,
},
},
}

expect(mergeNestedObjects([a, b], 'tsconfig.ts-node')).to.deep.equal({
transpileOnly: true,
})
})
})

0 comments on commit 039b42a

Please sign in to comment.