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

Commit c0d10bf

Browse files
kimamulas-panferov
authored andcommitted
feat: pass ts.Program as an argument for getCustomTransformers (#594)
* feat: pass ts.Program as an argument for getCustomTransformers * docs: add an explanation for getCustomTransformers
1 parent e1e95b1 commit c0d10bf

File tree

4 files changed

+71
-4
lines changed

4 files changed

+71
-4
lines changed

README.md

+26
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,32 @@ reportFiles: [
222222
]
223223
```
224224

225+
### getCustomTransformers *(string | ((program: ts.Program) => ts.CustomTransformers | undefined)) (default=undefined)*
226+
227+
Provide custom transformers, TypeScript 2.4.1+. Example:
228+
229+
```js
230+
const styledComponentsTransformer = require('typescript-plugin-styled-components').default;
231+
const keysTransformer = require('ts-transformer-keys/transformer').default;
232+
233+
// ...
234+
rules: [
235+
{
236+
test: /\.tsx?$/,
237+
loader: 'awesome-typescript-loader',
238+
options: {
239+
// ... other loader's options
240+
getCustomTransformers: program => ({
241+
before: [
242+
styledComponentsTransformer(),
243+
keysTransformer(program)
244+
]
245+
})
246+
}
247+
}
248+
]
249+
```
250+
225251
## Compiler options
226252

227253
You can pass compiler options inside the loader query string or in a TS config file.

src/__test__/custom-transformers.ts

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import {src, webpackConfig, tsconfig, compile, checkOutput, expectErrors, spec, expect, query} from './utils'
2+
3+
[true, false].forEach(transpileOnly => {
4+
spec(`${__filename}-transpileOnly:${transpileOnly}`, async function() {
5+
src(
6+
'index.ts',
7+
`
8+
class HiThere {
9+
constructor(a: number, b: string) {
10+
const t = a + b;
11+
}
12+
}
13+
`
14+
)
15+
16+
tsconfig()
17+
let getCustomTransformersCalled = false
18+
let stats = await compile(webpackConfig(query({
19+
transpileOnly,
20+
getCustomTransformers: program => {
21+
expect(typeof program.getTypeChecker === 'function').true
22+
getCustomTransformersCalled = true
23+
return {};
24+
}
25+
})))
26+
27+
expectErrors(stats, 0)
28+
checkOutput(
29+
'index.js',
30+
`
31+
class HiThere {
32+
constructor(a, b) {
33+
const t = a + b;
34+
}
35+
}
36+
`
37+
)
38+
expect(getCustomTransformersCalled).true
39+
})
40+
})

src/checker/runtime.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ function createChecker(receive: (cb: (msg: Req) => void) => void, send: (msg: Re
104104
let watchHost: WatchHost
105105
let watch: ts.WatchOfFilesAndCompilerOptions<ts.SemanticDiagnosticsBuilderProgram>
106106

107-
let finalTransformers: undefined | (() => ts.CustomTransformers)
107+
let finalTransformers: undefined | ((program: ts.Program) => ts.CustomTransformers)
108108

109109
function createWatchHost(): WatchHost {
110110
return {
@@ -410,7 +410,7 @@ function createChecker(receive: (cb: (msg: Req) => void) => void, send: (msg: Re
410410
writeFile,
411411
/*cancellationToken*/ undefined,
412412
/*emitOnlyDtsFiles*/ false,
413-
finalTransformers && finalTransformers()
413+
finalTransformers && finalTransformers(program.getProgram())
414414
)
415415
return outputFiles
416416
}
@@ -433,7 +433,8 @@ function createChecker(receive: (cb: (msg: Req) => void) => void, send: (msg: Re
433433
const trans = compiler.transpileModule(files.get(fileName).text, {
434434
compilerOptions: compilerOptions,
435435
fileName,
436-
reportDiagnostics: false
436+
reportDiagnostics: false,
437+
transformers: finalTransformers ? finalTransformers(getProgram().getProgram()) : undefined
437438
})
438439

439440
return {

src/interfaces.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export interface LoaderConfig {
2828
debug?: boolean
2929
reportFiles?: string[]
3030
context?: string
31-
getCustomTransformers?: string | (() => ts.CustomTransformers | undefined)
31+
getCustomTransformers?: string | ((program: ts.Program) => ts.CustomTransformers | undefined)
3232
}
3333

3434
export interface OutputFile {

0 commit comments

Comments
 (0)