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

feat: pass ts.Program as an argument for getCustomTransformers #594

Merged
merged 2 commits into from
Jun 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
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
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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is likely this line was accidentally removed in 38a772f.

See #531 (comment).

})

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