Skip to content

Commit 3990a13

Browse files
arcanisjohnnyreilly
authored andcommitted
Enables custom resolver only if they're used in incremental mode (#260)
* Enables custom resolver only if they're used in incremental mode * Adds a test * Updates the changelog * Bumps patch
1 parent dfd8933 commit 3990a13

8 files changed

+86
-45
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## v1.1.1
2+
3+
* [Fix a regression w/ plugins like tsconfig-paths-webpack-plugin](https://github.com/Realytics/fork-ts-checker-webpack-plugin/pull/260)
4+
15
## v1.1.0
26

37
* [Add new custom resolution options](https://github.com/Realytics/fork-ts-checker-webpack-plugin/pull/250)

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "fork-ts-checker-webpack-plugin",
3-
"version": "1.1.0",
3+
"version": "1.1.1",
44
"description": "Runs typescript type checker and linter on separate process.",
55
"main": "lib/index.js",
66
"types": "lib/index.d.ts",

src/CompilerHost.ts

+48-43
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,7 @@
22
import * as ts from 'typescript'; // Imported for types alone
33
import { LinkedList } from './LinkedList';
44
import { VueProgram } from './VueProgram';
5-
import {
6-
ResolveModuleName,
7-
ResolveTypeReferenceDirective,
8-
makeResolutionFunctions
9-
} from './resolution';
5+
import { ResolveModuleName, ResolveTypeReferenceDirective } from './resolution';
106

117
interface DirectoryWatchDelaySlot {
128
events: { fileName: string }[];
@@ -56,8 +52,21 @@ export class CompilerHost
5652

5753
private compilationStarted = false;
5854

59-
private readonly resolveModuleName: ResolveModuleName;
60-
private readonly resolveTypeReferenceDirective: ResolveTypeReferenceDirective;
55+
public resolveModuleNames:
56+
| ((
57+
moduleNames: string[],
58+
containingFile: string,
59+
reusedNames?: string[] | undefined,
60+
redirectedReference?: ts.ResolvedProjectReference | undefined
61+
) => (ts.ResolvedModule | undefined)[])
62+
| undefined;
63+
public resolveTypeReferenceDirectives:
64+
| ((
65+
typeReferenceDirectiveNames: string[],
66+
containingFile: string,
67+
redirectedReference?: ts.ResolvedProjectReference | undefined
68+
) => (ts.ResolvedTypeReferenceDirective | undefined)[])
69+
| undefined;
6170

6271
constructor(
6372
private typescript: typeof ts,
@@ -86,16 +95,39 @@ export class CompilerHost
8695
this.configFileName = this.tsHost.configFileName;
8796
this.optionsToExtend = this.tsHost.optionsToExtend || {};
8897

89-
const {
90-
resolveModuleName,
91-
resolveTypeReferenceDirective
92-
} = makeResolutionFunctions(
93-
userResolveModuleName,
94-
userResolveTypeReferenceDirective
95-
);
98+
if (userResolveModuleName) {
99+
this.resolveModuleNames = (
100+
moduleNames: string[],
101+
containingFile: string
102+
) => {
103+
return moduleNames.map(moduleName => {
104+
return userResolveModuleName(
105+
this.typescript,
106+
moduleName,
107+
containingFile,
108+
this.optionsToExtend,
109+
this
110+
).resolvedModule;
111+
});
112+
};
113+
}
96114

97-
this.resolveModuleName = resolveModuleName;
98-
this.resolveTypeReferenceDirective = resolveTypeReferenceDirective;
115+
if (userResolveTypeReferenceDirective) {
116+
this.resolveTypeReferenceDirectives = (
117+
typeDirectiveNames: string[],
118+
containingFile: string
119+
) => {
120+
return typeDirectiveNames.map(typeDirectiveName => {
121+
return userResolveTypeReferenceDirective(
122+
this.typescript,
123+
typeDirectiveName,
124+
containingFile,
125+
this.optionsToExtend,
126+
this
127+
).resolvedTypeReferenceDirective;
128+
});
129+
};
130+
}
99131
}
100132

101133
public async processChanges(): Promise<{
@@ -367,33 +399,6 @@ export class CompilerHost
367399
this.afterCompile();
368400
}
369401

370-
public resolveModuleNames(moduleNames: string[], containingFile: string) {
371-
return moduleNames.map(moduleName => {
372-
return this.resolveModuleName(
373-
this.typescript,
374-
moduleName,
375-
containingFile,
376-
this.optionsToExtend,
377-
this
378-
).resolvedModule;
379-
});
380-
}
381-
382-
public resolveTypeReferenceDirectives(
383-
typeDirectiveNames: string[],
384-
containingFile: string
385-
) {
386-
return typeDirectiveNames.map(typeDirectiveName => {
387-
return this.resolveTypeReferenceDirective(
388-
this.typescript,
389-
typeDirectiveName,
390-
containingFile,
391-
this.optionsToExtend,
392-
this
393-
).resolvedTypeReferenceDirective;
394-
});
395-
}
396-
397402
// the functions below are use internally by typescript. we cannot use non-emitting version of incremental watching API
398403
// because it is
399404
// - much slower for some reason,

test/integration/helpers.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ exports.createVueCompiler = function(options) {
2323
resolve: {
2424
extensions: ['.ts', '.js', '.vue', '.json'],
2525
alias: {
26-
'@': path.resolve(__dirname, './vue/src')
26+
'@': path.resolve(__dirname, './vue/src'),
27+
surprise: './src/index.ts'
2728
}
2829
},
2930
module: {

test/integration/index.spec.js

+17
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ function makeCommonTests(useTypescriptIncrementalApi) {
3535
return compiler.webpack;
3636
}
3737

38+
const skipIfIncremental = useTypescriptIncrementalApi ? it.skip : it;
39+
3840
/**
3941
* Implicitly check whether killService was called by checking that
4042
* the service property was set to undefined.
@@ -132,6 +134,21 @@ function makeCommonTests(useTypescriptIncrementalApi) {
132134
});
133135
});
134136

137+
skipIfIncremental('should support custom resolution w/ "paths"', function(
138+
callback
139+
) {
140+
var compiler = createCompiler({
141+
tsconfig: 'tsconfig-weird-resolutions-with-paths.json',
142+
resolveModuleNameModule: `${__dirname}/project/weirdResolver.js`,
143+
resolveTypeReferenceDirectiveModule: `${__dirname}/project/weirdResolver.js`
144+
});
145+
146+
compiler.run(function(err, stats) {
147+
expect(stats.compilation.errors.length).to.be.eq(0);
148+
callback();
149+
});
150+
});
151+
135152
it('should fix linting errors with tslintAutofix flag set to true', function(callback) {
136153
const fileName = 'lintingError1';
137154
helpers.testLintAutoFixTest(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import 'surprise';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"compilerOptions": {
3+
"lib": ["es2016"],
4+
"paths": {
5+
"surprise": "./index.ts"
6+
}
7+
},
8+
"include": [
9+
"src/pathResolutions.ts",
10+
"src/weirdResolutions.ts"
11+
]
12+
}

test/integration/project/tsconfig.json

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"lib": ["es2016"],
44
},
55
"exclude": [
6+
"src/pathResolutions.ts",
67
"src/weirdResolutions.ts"
78
]
89
}

0 commit comments

Comments
 (0)