Skip to content

Commit 928b016

Browse files
committed
manually re-apply commits for TypeStrong#250 and TypeStrong#260
This reverts commit 9db85d7.
1 parent 4296c91 commit 928b016

14 files changed

+209
-25
lines changed

src/index.ts

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ interface Options {
5353
vue: boolean;
5454
useTypescriptIncrementalApi: boolean;
5555
measureCompilationTime: boolean;
56+
resolveModuleNameModule: string;
57+
resolveTypeReferenceDirectiveModule: string;
5658
}
5759

5860
/**
@@ -99,6 +101,8 @@ class ForkTsCheckerWebpackPlugin {
99101
private colors: Chalk;
100102
private formatter: Formatter;
101103
private useTypescriptIncrementalApi: boolean;
104+
private resolveModuleNameModule: string | undefined;
105+
private resolveTypeReferenceDirectiveModule: string | undefined;
102106

103107
private tsconfigPath?: string;
104108
private tslintPath?: string;
@@ -158,6 +162,9 @@ class ForkTsCheckerWebpackPlugin {
158162
this.silent = options.silent === true; // default false
159163
this.async = options.async !== false; // default true
160164
this.checkSyntacticErrors = options.checkSyntacticErrors === true; // default false
165+
this.resolveModuleNameModule = options.resolveModuleNameModule;
166+
this.resolveTypeReferenceDirectiveModule =
167+
options.resolveTypeReferenceDirectiveModule;
161168
this.workersNumber = options.workers || ForkTsCheckerWebpackPlugin.ONE_CPU;
162169
this.memoryLimit =
163170
options.memoryLimit || ForkTsCheckerWebpackPlugin.DEFAULT_MEMORY_LIMIT;
@@ -569,35 +576,50 @@ class ForkTsCheckerWebpackPlugin {
569576
}
570577

571578
private spawnService() {
579+
const env: { [key: string]: string | undefined } = {
580+
...process.env,
581+
TYPESCRIPT_PATH: this.typescriptPath,
582+
TSCONFIG: this.tsconfigPath,
583+
COMPILER_OPTIONS: JSON.stringify(this.compilerOptions),
584+
TSLINT: this.tslintPath || (this.tslint ? 'true' : ''),
585+
CONTEXT: this.compiler.options.context,
586+
TSLINTAUTOFIX: String(this.tslintAutoFix),
587+
WATCH: this.isWatching ? this.watchPaths.join('|') : '',
588+
WORK_DIVISION: String(Math.max(1, this.workersNumber)),
589+
MEMORY_LIMIT: String(this.memoryLimit),
590+
CHECK_SYNTACTIC_ERRORS: String(this.checkSyntacticErrors),
591+
USE_INCREMENTAL_API: String(this.useTypescriptIncrementalApi === true),
592+
VUE: String(this.vue)
593+
};
594+
595+
if (typeof this.resolveModuleNameModule !== 'undefined') {
596+
env.RESOLVE_MODULE_NAME = this.resolveModuleNameModule;
597+
} else {
598+
delete env.RESOLVE_MODULE_NAME;
599+
}
600+
601+
if (typeof this.resolveTypeReferenceDirectiveModule !== 'undefined') {
602+
env.RESOLVE_TYPE_REFERENCE_DIRECTIVE = this.resolveTypeReferenceDirectiveModule;
603+
} else {
604+
delete env.RESOLVE_TYPE_REFERENCE_DIRECTIVE;
605+
}
606+
572607
this.service = childProcess.fork(
573608
path.resolve(
574609
__dirname,
575610
this.workersNumber > 1 ? './cluster.js' : './service.js'
576611
),
577612
[],
578613
{
614+
env,
579615
execArgv: (this.workersNumber > 1
580616
? []
581617
: ['--max-old-space-size=' + this.memoryLimit]
582618
).concat(this.nodeArgs),
583-
env: {
584-
...process.env,
585-
TYPESCRIPT_PATH: this.typescriptPath,
586-
TSCONFIG: this.tsconfigPath,
587-
COMPILER_OPTIONS: JSON.stringify(this.compilerOptions),
588-
TSLINT: this.tslintPath || (this.tslint ? 'true' : ''),
589-
CONTEXT: this.compiler.options.context,
590-
TSLINTAUTOFIX: this.tslintAutoFix,
591-
WATCH: this.isWatching ? this.watchPaths.join('|') : '',
592-
WORK_DIVISION: Math.max(1, this.workersNumber),
593-
MEMORY_LIMIT: this.memoryLimit,
594-
CHECK_SYNTACTIC_ERRORS: this.checkSyntacticErrors,
595-
USE_INCREMENTAL_API: this.useTypescriptIncrementalApi === true,
596-
VUE: this.vue
597-
},
598619
stdio: ['inherit', 'inherit', 'inherit', 'ipc']
599620
}
600621
);
622+
601623
this.serviceRpc = new RpcProvider(message => this.service!.send(message));
602624
this.service.on('message', message => this.serviceRpc!.dispatch(message));
603625

src/resolution.ts

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// tslint:disable-next-line:no-implicit-dependencies
2+
import * as ts from 'typescript'; // Imported for types alone
3+
4+
export type ResolveModuleName = (
5+
typescript: typeof ts,
6+
moduleName: string,
7+
containingFile: string,
8+
compilerOptions: ts.CompilerOptions,
9+
moduleResolutionHost: ts.ModuleResolutionHost
10+
) => ts.ResolvedModuleWithFailedLookupLocations;
11+
12+
export type ResolveTypeReferenceDirective = (
13+
typescript: typeof ts,
14+
typeDirectiveName: string,
15+
containingFile: string,
16+
compilerOptions: ts.CompilerOptions,
17+
moduleResolutionHost: ts.ModuleResolutionHost
18+
) => ts.ResolvedTypeReferenceDirectiveWithFailedLookupLocations;
19+
20+
export function makeResolutionFunctions(
21+
resolveModuleName: ResolveModuleName | undefined,
22+
resolveTypeReferenceDirective: ResolveTypeReferenceDirective | undefined
23+
) {
24+
resolveModuleName =
25+
resolveModuleName ||
26+
((
27+
typescript,
28+
moduleName,
29+
containingFile,
30+
compilerOptions,
31+
moduleResolutionHost
32+
) => {
33+
return typescript.resolveModuleName(
34+
moduleName,
35+
containingFile,
36+
compilerOptions,
37+
moduleResolutionHost
38+
);
39+
});
40+
41+
resolveTypeReferenceDirective =
42+
resolveTypeReferenceDirective ||
43+
((
44+
typescript,
45+
typeDirectiveName,
46+
containingFile,
47+
compilerOptions,
48+
moduleResolutionHost
49+
) => {
50+
return typescript.resolveTypeReferenceDirective(
51+
typeDirectiveName,
52+
containingFile,
53+
compilerOptions,
54+
moduleResolutionHost
55+
);
56+
});
57+
58+
return { resolveModuleName, resolveTypeReferenceDirective };
59+
}

src/service.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,21 @@ const rpc = new RpcProvider(message => {
3535
});
3636
process.on('message', message => rpc.dispatch(message));
3737

38-
const wrapperConfig: TypeScriptWrapperConfig =
39-
process.env.VUE === 'true' ? wrapperConfigWithVue : emptyWrapperConfig;
38+
const resolveModuleName = process.env.RESOLVE_MODULE_NAME
39+
? require(process.env.RESOLVE_MODULE_NAME!).resolveModuleName
40+
: undefined;
41+
const resolveTypeReferenceDirective = process.env
42+
.RESOLVE_TYPE_REFERENCE_DIRECTIVE
43+
? require(process.env.RESOLVE_TYPE_REFERENCE_DIRECTIVE!)
44+
.resolveTypeReferenceDirective
45+
: undefined;
46+
47+
const wrapperConfig: TypeScriptWrapperConfig = {
48+
...emptyWrapperConfig,
49+
resolveModuleName,
50+
resolveTypeReferenceDirective,
51+
...(process.env.VUE === 'true' ? wrapperConfigWithVue : {})
52+
};
4053

4154
const typescript: typeof ts = patchTypescript(
4255
require(process.env.TYPESCRIPT_PATH!),

src/wrapCompilerHost.ts

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// tslint:disable-next-line:no-implicit-dependencies
33
import * as ts from 'typescript'; // Imported for types alone
44
import { TypeScriptWrapperConfig } from './wrapperUtils';
5+
import { makeResolutionFunctions } from './resolution';
56

67
type HostType = ts.CompilerHost | ts.WatchCompilerHostOfConfigFile<any>;
78
// @ts-ignore
@@ -21,16 +22,24 @@ export function wrapCompilerHost<T extends HostType>(
2122
origHost: T,
2223
compilerOptions: ts.CompilerOptions,
2324
typescript: typeof ts,
24-
_config: TypeScriptWrapperConfig
25+
config: TypeScriptWrapperConfig
2526
) {
2627
let wrappedCompilerHost: T;
2728

29+
const {
30+
resolveModuleName,
31+
resolveTypeReferenceDirective
32+
} = makeResolutionFunctions(
33+
config.resolveModuleName,
34+
config.resolveTypeReferenceDirective
35+
);
36+
2837
const compilerHostWrappers: Partial<ts.CompilerHost> = {
2938
resolveModuleNames(
3039
moduleNames,
3140
containingFile,
32-
_reusedNames, // no idea what this is for
33-
redirectedReference
41+
_reusedNames,
42+
_redirectedReference
3443
) {
3544
return moduleNames.map(moduleName => {
3645
for (const suffix of wrapSuffixes) {
@@ -42,13 +51,12 @@ export function wrapCompilerHost<T extends HostType>(
4251
RESET
4352
);
4453
*/
45-
const result = typescript.resolveModuleName(
54+
const result = resolveModuleName(
55+
typescript,
4656
moduleName + suffix,
4757
containingFile,
4858
compilerOptions,
49-
wrappedCompilerHost,
50-
undefined,
51-
redirectedReference
59+
wrappedCompilerHost
5260
);
5361
if (result.resolvedModule) {
5462
/*
@@ -68,6 +76,20 @@ export function wrapCompilerHost<T extends HostType>(
6876
return undefined;
6977
});
7078
},
79+
resolveTypeReferenceDirectives(
80+
typeDirectiveNames: string[],
81+
containingFile: string
82+
) {
83+
return typeDirectiveNames.map(typeDirectiveName => {
84+
return resolveTypeReferenceDirective(
85+
typescript,
86+
typeDirectiveName,
87+
containingFile,
88+
compilerOptions,
89+
wrappedCompilerHost
90+
).resolvedTypeReferenceDirective;
91+
});
92+
},
7193
getSourceFile(...args) {
7294
let result = (origHost as ts.CompilerHost).getSourceFile(...args);
7395
if (result && result.text) {

src/wrapperUtils.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import { extname } from 'path';
44
import { handleMdxContents } from './handleMdxContents';
55
import { handleVueContents } from './handleVueContents';
6+
import { ResolveModuleName, ResolveTypeReferenceDirective } from './resolution';
67

78
export interface TypeScriptWrapperConfig {
89
extensionHandlers: {
@@ -12,6 +13,8 @@ export interface TypeScriptWrapperConfig {
1213
) => string;
1314
};
1415
wrapExtensions: string[];
16+
resolveModuleName?: ResolveModuleName;
17+
resolveTypeReferenceDirective?: ResolveTypeReferenceDirective;
1518
}
1619

1720
export const wrapperConfigWithVue: TypeScriptWrapperConfig = {

test/integration/helpers.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ exports.createVueCompiler = async function(options) {
3939
resolve: {
4040
extensions: ['.ts', '.js', '.vue', '.json'],
4141
alias: {
42-
'@': path.resolve(__dirname, './vue/src')
42+
'@': path.resolve(__dirname, './vue/src'),
43+
surprise: './src/index.ts'
4344
}
4445
},
4546
module: {

test/integration/index.spec.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ function makeCommonTests(useTypescriptIncrementalApi) {
3737
return compiler.webpack;
3838
}
3939

40+
const skipIfIncremental = useTypescriptIncrementalApi ? it.skip : it;
41+
4042
/**
4143
* Implicitly check whether killService was called by checking that
4244
* the service property was set to undefined.
@@ -151,6 +153,34 @@ function makeCommonTests(useTypescriptIncrementalApi) {
151153
});
152154
});
153155

156+
it('should support custom resolution', function(callback) {
157+
var compiler = createCompiler({
158+
tsconfig: 'tsconfig-weird-resolutions.json',
159+
resolveModuleNameModule: `${__dirname}/project/weirdResolver.js`,
160+
resolveTypeReferenceDirectiveModule: `${__dirname}/project/weirdResolver.js`
161+
});
162+
163+
compiler.run(function(err, stats) {
164+
expect(stats.compilation.errors.length).to.be.eq(0);
165+
callback();
166+
});
167+
});
168+
169+
skipIfIncremental('should support custom resolution w/ "paths"', function(
170+
callback
171+
) {
172+
var compiler = createCompiler({
173+
tsconfig: 'tsconfig-weird-resolutions-with-paths.json',
174+
resolveModuleNameModule: `${__dirname}/project/weirdResolver.js`,
175+
resolveTypeReferenceDirectiveModule: `${__dirname}/project/weirdResolver.js`
176+
});
177+
178+
compiler.run(function(err, stats) {
179+
expect(stats.compilation.errors.length).to.be.eq(0);
180+
callback();
181+
});
182+
});
183+
154184
it('should fix linting errors with tslintAutofix flag set to true', function(callback) {
155185
const fileName = 'lintingError1';
156186
helpers.testLintAutoFixTest(
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
declare function helloWorld(): void;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const fortyTwo = 42;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import 'surprise';
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/// <reference types="where-am-i" />
2+
3+
import { fortyTwo } from 'please-find-my-export';
4+
5+
helloWorld();
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"compilerOptions": {
3+
"lib": ["es2016"],
4+
"paths": {
5+
"surprise": "./index.ts"
6+
}
7+
},
8+
"include": ["src/pathResolutions.ts", "src/weirdResolutions.ts"]
9+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"compilerOptions": {
3+
"lib": ["es2016"]
4+
},
5+
"include": ["src/weirdResolutions.ts"]
6+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
exports.resolveModuleName = function() {
2+
return { resolvedModule: { resolvedFileName: `${__dirname}/src/export.ts` } };
3+
};
4+
5+
exports.resolveTypeReferenceDirective = function() {
6+
return {
7+
resolvedTypeReferenceDirective: {
8+
resolvedFileName: `${__dirname}/src/defs.d.ts`
9+
}
10+
};
11+
};

0 commit comments

Comments
 (0)