Skip to content

Commit

Permalink
Enables custom resolver only if they're used in incremental mode (Typ…
Browse files Browse the repository at this point in the history
…eStrong#260)

* Enables custom resolver only if they're used in incremental mode

* Adds a test

* Updates the changelog

* Bumps patch
  • Loading branch information
arcanis authored and johnnyreilly committed Apr 22, 2019
1 parent dfd8933 commit 3990a13
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 45 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## v1.1.1

* [Fix a regression w/ plugins like tsconfig-paths-webpack-plugin](https://github.com/Realytics/fork-ts-checker-webpack-plugin/pull/260)

## v1.1.0

* [Add new custom resolution options](https://github.com/Realytics/fork-ts-checker-webpack-plugin/pull/250)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "fork-ts-checker-webpack-plugin",
"version": "1.1.0",
"version": "1.1.1",
"description": "Runs typescript type checker and linter on separate process.",
"main": "lib/index.js",
"types": "lib/index.d.ts",
Expand Down
91 changes: 48 additions & 43 deletions src/CompilerHost.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@
import * as ts from 'typescript'; // Imported for types alone
import { LinkedList } from './LinkedList';
import { VueProgram } from './VueProgram';
import {
ResolveModuleName,
ResolveTypeReferenceDirective,
makeResolutionFunctions
} from './resolution';
import { ResolveModuleName, ResolveTypeReferenceDirective } from './resolution';

interface DirectoryWatchDelaySlot {
events: { fileName: string }[];
Expand Down Expand Up @@ -56,8 +52,21 @@ export class CompilerHost

private compilationStarted = false;

private readonly resolveModuleName: ResolveModuleName;
private readonly resolveTypeReferenceDirective: ResolveTypeReferenceDirective;
public resolveModuleNames:
| ((
moduleNames: string[],
containingFile: string,
reusedNames?: string[] | undefined,
redirectedReference?: ts.ResolvedProjectReference | undefined
) => (ts.ResolvedModule | undefined)[])
| undefined;
public resolveTypeReferenceDirectives:
| ((
typeReferenceDirectiveNames: string[],
containingFile: string,
redirectedReference?: ts.ResolvedProjectReference | undefined
) => (ts.ResolvedTypeReferenceDirective | undefined)[])
| undefined;

constructor(
private typescript: typeof ts,
Expand Down Expand Up @@ -86,16 +95,39 @@ export class CompilerHost
this.configFileName = this.tsHost.configFileName;
this.optionsToExtend = this.tsHost.optionsToExtend || {};

const {
resolveModuleName,
resolveTypeReferenceDirective
} = makeResolutionFunctions(
userResolveModuleName,
userResolveTypeReferenceDirective
);
if (userResolveModuleName) {
this.resolveModuleNames = (
moduleNames: string[],
containingFile: string
) => {
return moduleNames.map(moduleName => {
return userResolveModuleName(
this.typescript,
moduleName,
containingFile,
this.optionsToExtend,
this
).resolvedModule;
});
};
}

this.resolveModuleName = resolveModuleName;
this.resolveTypeReferenceDirective = resolveTypeReferenceDirective;
if (userResolveTypeReferenceDirective) {
this.resolveTypeReferenceDirectives = (
typeDirectiveNames: string[],
containingFile: string
) => {
return typeDirectiveNames.map(typeDirectiveName => {
return userResolveTypeReferenceDirective(
this.typescript,
typeDirectiveName,
containingFile,
this.optionsToExtend,
this
).resolvedTypeReferenceDirective;
});
};
}
}

public async processChanges(): Promise<{
Expand Down Expand Up @@ -367,33 +399,6 @@ export class CompilerHost
this.afterCompile();
}

public resolveModuleNames(moduleNames: string[], containingFile: string) {
return moduleNames.map(moduleName => {
return this.resolveModuleName(
this.typescript,
moduleName,
containingFile,
this.optionsToExtend,
this
).resolvedModule;
});
}

public resolveTypeReferenceDirectives(
typeDirectiveNames: string[],
containingFile: string
) {
return typeDirectiveNames.map(typeDirectiveName => {
return this.resolveTypeReferenceDirective(
this.typescript,
typeDirectiveName,
containingFile,
this.optionsToExtend,
this
).resolvedTypeReferenceDirective;
});
}

// the functions below are use internally by typescript. we cannot use non-emitting version of incremental watching API
// because it is
// - much slower for some reason,
Expand Down
3 changes: 2 additions & 1 deletion test/integration/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ exports.createVueCompiler = function(options) {
resolve: {
extensions: ['.ts', '.js', '.vue', '.json'],
alias: {
'@': path.resolve(__dirname, './vue/src')
'@': path.resolve(__dirname, './vue/src'),
surprise: './src/index.ts'
}
},
module: {
Expand Down
17 changes: 17 additions & 0 deletions test/integration/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ function makeCommonTests(useTypescriptIncrementalApi) {
return compiler.webpack;
}

const skipIfIncremental = useTypescriptIncrementalApi ? it.skip : it;

/**
* Implicitly check whether killService was called by checking that
* the service property was set to undefined.
Expand Down Expand Up @@ -132,6 +134,21 @@ function makeCommonTests(useTypescriptIncrementalApi) {
});
});

skipIfIncremental('should support custom resolution w/ "paths"', function(
callback
) {
var compiler = createCompiler({
tsconfig: 'tsconfig-weird-resolutions-with-paths.json',
resolveModuleNameModule: `${__dirname}/project/weirdResolver.js`,
resolveTypeReferenceDirectiveModule: `${__dirname}/project/weirdResolver.js`
});

compiler.run(function(err, stats) {
expect(stats.compilation.errors.length).to.be.eq(0);
callback();
});
});

it('should fix linting errors with tslintAutofix flag set to true', function(callback) {
const fileName = 'lintingError1';
helpers.testLintAutoFixTest(
Expand Down
1 change: 1 addition & 0 deletions test/integration/project/src/pathResolutions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import 'surprise';
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"compilerOptions": {
"lib": ["es2016"],
"paths": {
"surprise": "./index.ts"
}
},
"include": [
"src/pathResolutions.ts",
"src/weirdResolutions.ts"
]
}
1 change: 1 addition & 0 deletions test/integration/project/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"lib": ["es2016"],
},
"exclude": [
"src/pathResolutions.ts",
"src/weirdResolutions.ts"
]
}

0 comments on commit 3990a13

Please sign in to comment.