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

Commit

Permalink
feat: pass ts.Program as an argument for getCustomTransformers (#594)
Browse files Browse the repository at this point in the history
* feat: pass ts.Program as an argument for getCustomTransformers

* docs: add an explanation for getCustomTransformers
  • Loading branch information
kimamula authored and s-panferov committed Jun 21, 2018
1 parent e1e95b1 commit c0d10bf
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 4 deletions.
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,32 @@ reportFiles: [
]
```

### getCustomTransformers *(string | ((program: ts.Program) => ts.CustomTransformers | undefined)) (default=undefined)*

Provide custom transformers, TypeScript 2.4.1+. Example:

```js
const styledComponentsTransformer = require('typescript-plugin-styled-components').default;
const keysTransformer = require('ts-transformer-keys/transformer').default;

// ...
rules: [
{
test: /\.tsx?$/,
loader: 'awesome-typescript-loader',
options: {
// ... other loader's options
getCustomTransformers: program => ({
before: [
styledComponentsTransformer(),
keysTransformer(program)
]
})
}
}
]
```

## Compiler options

You can pass compiler options inside the loader query string or in a TS config file.
40 changes: 40 additions & 0 deletions src/__test__/custom-transformers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import {src, webpackConfig, tsconfig, compile, checkOutput, expectErrors, spec, expect, query} from './utils'

[true, false].forEach(transpileOnly => {
spec(`${__filename}-transpileOnly:${transpileOnly}`, async function() {
src(
'index.ts',
`
class HiThere {
constructor(a: number, b: string) {
const t = a + b;
}
}
`
)

tsconfig()
let getCustomTransformersCalled = false
let stats = await compile(webpackConfig(query({
transpileOnly,
getCustomTransformers: program => {
expect(typeof program.getTypeChecker === 'function').true
getCustomTransformersCalled = true
return {};
}
})))

expectErrors(stats, 0)
checkOutput(
'index.js',
`
class HiThere {
constructor(a, b) {
const t = a + b;
}
}
`
)
expect(getCustomTransformersCalled).true
})
})
7 changes: 4 additions & 3 deletions src/checker/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ function createChecker(receive: (cb: (msg: Req) => void) => void, send: (msg: Re
let watchHost: WatchHost
let watch: ts.WatchOfFilesAndCompilerOptions<ts.SemanticDiagnosticsBuilderProgram>

let finalTransformers: undefined | (() => ts.CustomTransformers)
let finalTransformers: undefined | ((program: ts.Program) => ts.CustomTransformers)

function createWatchHost(): WatchHost {
return {
Expand Down Expand Up @@ -410,7 +410,7 @@ function createChecker(receive: (cb: (msg: Req) => void) => void, send: (msg: Re
writeFile,
/*cancellationToken*/ undefined,
/*emitOnlyDtsFiles*/ false,
finalTransformers && finalTransformers()
finalTransformers && finalTransformers(program.getProgram())
)
return outputFiles
}
Expand All @@ -433,7 +433,8 @@ function createChecker(receive: (cb: (msg: Req) => void) => void, send: (msg: Re
const trans = compiler.transpileModule(files.get(fileName).text, {
compilerOptions: compilerOptions,
fileName,
reportDiagnostics: false
reportDiagnostics: false,
transformers: finalTransformers ? finalTransformers(getProgram().getProgram()) : undefined
})

return {
Expand Down
2 changes: 1 addition & 1 deletion src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export interface LoaderConfig {
debug?: boolean
reportFiles?: string[]
context?: string
getCustomTransformers?: string | (() => ts.CustomTransformers | undefined)
getCustomTransformers?: string | ((program: ts.Program) => ts.CustomTransformers | undefined)
}

export interface OutputFile {
Expand Down

0 comments on commit c0d10bf

Please sign in to comment.