Skip to content

Commit

Permalink
resolveTypeReferenceDirective support for yarn PnP (#921)
Browse files Browse the repository at this point in the history
* prepare the ground for type directives

* implement resolveTypeReferenceDirective option

* refactor of resolver construction

* ready 5.4.0 release
  • Loading branch information
johnnyreilly committed Apr 22, 2019
1 parent 4692a22 commit ea2fcf9
Show file tree
Hide file tree
Showing 12 changed files with 227 additions and 148 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Expand Up @@ -14,7 +14,7 @@ install:
- yarn lint
- yarn add $TYPESCRIPT
env:
- TYPESCRIPT=typescript@3.4.1
- TYPESCRIPT=typescript@3.4.4
- TYPESCRIPT=typescript@next
- TYPESCRIPT=typescript@3.3.3
- TYPESCRIPT=typescript@3.2.1
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
@@ -1,5 +1,9 @@
# Changelog

## v5.4.0

* [feat: resolveTypeReferenceDirective support for yarn PnP](https://github.com/TypeStrong/ts-loader/pull/921) - thanks @johnnyreilly!

## v5.3.3

* [fix: Pass ts.Program to getCustomTransformers](https://github.com/TypeStrong/ts-loader/pull/889) (#860) - thanks @andersekdahl!
Expand Down
8 changes: 8 additions & 0 deletions README.md
Expand Up @@ -29,6 +29,10 @@ You probably don't want to give up type checking; that's rather the point of Typ

If you'd like to see a simple setup take a look at [our simple example](examples/fork-ts-checker-webpack-plugin/). For a more complex setup take a look at our [more involved example](examples/react-babel-karma-gulp).

### Yarn Plug’n’Play

`ts-loader` supports [Yarn Plug’n’Play](https://yarnpkg.com/en/docs/pnp). The recommended way to integrate is using the [pnp-webpack-plugin](https://github.com/arcanis/pnp-webpack-plugin#ts-loader-integration).

### Babel

ts-loader works very well in combination with [babel](https://babeljs.io/) and [babel-loader](https://github.com/babel/babel-loader). There is an [example](https://github.com/Microsoft/TypeScriptSamples/tree/master/react-flux-babel-karma) of this in the official [TypeScript Samples](https://github.com/Microsoft/TypeScriptSamples). Alternatively take a look at our own [example](examples/react-babel-karma-gulp).
Expand Down Expand Up @@ -257,6 +261,10 @@ This will ensure that the plugin checks for both syntactic errors (eg `const arr

Also, if you are using `thread-loader` in watch mode, remember to set `poolTimeout: Infinity` so workers don't die.

#### resolveModuleName and resolveTypeReferenceDirective:

These options should be functions which will be used to resolve the import statements and the `<reference types="...">` directives instead of the default TypeScript implementation. It's not intended that these will typically be used by a user of `ts-loader` - they exist to facilitate functionality such as [Yarn Plug’n’Play](https://yarnpkg.com/en/docs/pnp).

#### getCustomTransformers _( (program: Program) => { before?: TransformerFactory<SourceFile>[]; after?: TransformerFactory<SourceFile>[]; } )_

Provide custom transformers - only compatible with TypeScript 2.3+ (and 2.4 if using `transpileOnly` mode). For example usage take a look at [typescript-plugin-styled-components](https://github.com/Igorbek/typescript-plugin-styled-components) or our [test](test/comparison-tests/customTransformer).
Expand Down
2 changes: 1 addition & 1 deletion appveyor.yml
Expand Up @@ -3,7 +3,7 @@ environment:
FORCE_COLOR: 1
nodejs_version: "10"
matrix:
- TYPESCRIPT: typescript@3.4.1
- TYPESCRIPT: typescript@3.4.4
- TYPESCRIPT: typescript@next
- TYPESCRIPT: typescript@3.3.3
- TYPESCRIPT: typescript@3.2.1
Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "ts-loader",
"version": "5.3.3",
"version": "5.4.0",
"description": "TypeScript loader for webpack",
"main": "index.js",
"types": "dist/types/index.d.ts",
Expand Down
3 changes: 2 additions & 1 deletion src/index.ts
Expand Up @@ -255,7 +255,8 @@ const validLoaderOptions: ValidLoaderOptions[] = [
'allowTsInNodeModules',
'experimentalFileCaching',
'projectReferences',
'resolveModuleName'
'resolveModuleName',
'resolveTypeReferenceDirective'
];

/**
Expand Down
23 changes: 17 additions & 6 deletions src/interfaces.ts
Expand Up @@ -227,8 +227,8 @@ export type ResolveSync = (

export interface WatchHost
extends typescript.WatchCompilerHostOfFilesAndCompilerOptions<
typescript.BuilderProgram
> {
typescript.BuilderProgram
> {
invokeFileWatcher(
fileName: string,
eventKind: typescript.FileWatcherEventKind
Expand Down Expand Up @@ -299,7 +299,7 @@ export type ResolveModuleName = (
moduleName: string,
containingFile: string,
compilerOptions: typescript.CompilerOptions,
moduleResolutionHost: typescript.ModuleResolutionHost,
moduleResolutionHost: typescript.ModuleResolutionHost
) => typescript.ResolvedModuleWithFailedLookupLocations;

export type CustomResolveModuleName = (
Expand All @@ -310,6 +310,14 @@ export type CustomResolveModuleName = (
parentResolver: ResolveModuleName
) => typescript.ResolvedModuleWithFailedLookupLocations;

export type CustomResolveTypeReferenceDirective = (
typeDirectiveName: string,
containingFile: string,
compilerOptions: typescript.CompilerOptions,
moduleResolutionHost: typescript.ModuleResolutionHost,
parentResolver: typeof typescript.resolveTypeReferenceDirective
) => typescript.ResolvedTypeReferenceDirectiveWithFailedLookupLocations;

export interface LoaderOptions {
silent: boolean;
logLevel: LogLevel;
Expand All @@ -328,14 +336,17 @@ export interface LoaderOptions {
appendTsSuffixTo: RegExp[];
appendTsxSuffixTo: RegExp[];
happyPackMode: boolean;
getCustomTransformers?:
getCustomTransformers:
| string
| ((program: typescript.Program) => typescript.CustomTransformers | undefined);
| ((
program: typescript.Program
) => typescript.CustomTransformers | undefined);
experimentalWatchApi: boolean;
allowTsInNodeModules: boolean;
experimentalFileCaching: boolean;
projectReferences: boolean;
resolveModuleName?: CustomResolveModuleName;
resolveModuleName: CustomResolveModuleName;
resolveTypeReferenceDirective: CustomResolveTypeReferenceDirective;
}

export interface TSFile {
Expand Down

0 comments on commit ea2fcf9

Please sign in to comment.