Skip to content
This repository was archived by the owner on Oct 1, 2020. It is now read-only.

Commit bfdc37d

Browse files
authored
fix: Handle when transpileOnly option is used (#94)
1 parent d33103f commit bfdc37d

File tree

2 files changed

+34
-4
lines changed

2 files changed

+34
-4
lines changed

lib/typescript-overrides.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,29 @@
11
const debug = require('debug')('cypress:webpack')
2+
const _ = require('lodash')
3+
4+
import { CompilerOptions, CreateProgramOptions } from 'typescript'
25

36
let sourceMapOverride: null | boolean = null
47

8+
export const getProgramOptions = (rootNamesOrOptions: CreateProgramOptions, options: CompilerOptions): CompilerOptions => {
9+
return _.isArray(rootNamesOrOptions) ? options : rootNamesOrOptions.options
10+
}
11+
512
export const tryRequireTypescript = () => {
613
try {
714
// reset each time this is called
815
sourceMapOverride = null
916

10-
const typescript = require('typescript')
17+
const typescript = require('typescript') as typeof import('typescript')
1118

1219
debug('typescript found, overriding typescript.createProgram()')
1320

1421
const { createProgram } = typescript
1522

16-
typescript.createProgram = (...args: [any]) => {
17-
const [programOptions] = args
18-
const { options } = programOptions
23+
typescript.createProgram = (...args: any[]) => {
24+
const [rootNamesOrOptions, _options] = args
25+
26+
const options = getProgramOptions(rootNamesOrOptions, _options)
1927

2028
debug('typescript unmodified createProgram options %o', options)
2129

@@ -31,6 +39,7 @@ export const tryRequireTypescript = () => {
3139
debug('typescript modified createProgram options %o', options)
3240
}
3341

42+
// @ts-ignore
3443
return createProgram.apply(typescript, args)
3544
}
3645

test/unit/typescript-overrides.spec.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,5 +117,26 @@ describe('./lib/typescript-overrides', () => {
117117
},
118118
})
119119
})
120+
121+
it('sets options when given an array', () => {
122+
const typescriptOverrides = proxyquire('../../lib/typescript-overrides', {
123+
typescript,
124+
})
125+
126+
typescriptOverrides.overrideSourceMaps(true)
127+
128+
expect(typescriptOverrides.getSourceMapOverride()).to.be.true
129+
130+
typescript.createProgram([], {
131+
sourceMap: false,
132+
inlineSources: true,
133+
inlineSourceMap: true,
134+
})
135+
136+
expect(createProgram).to.be.calledOn(typescript)
137+
expect(createProgram).to.be.calledWith([], {
138+
sourceMap: true,
139+
})
140+
})
120141
})
121142
})

0 commit comments

Comments
 (0)