Skip to content

Commit

Permalink
typescript path can now be specified (#204)
Browse files Browse the repository at this point in the history
* typescript path can now be specified

* update changelog

* fix no implicit dependencies
  • Loading branch information
johnnyreilly authored Jan 19, 2019
1 parent bb8dbf8 commit 4537c16
Show file tree
Hide file tree
Showing 18 changed files with 215 additions and 85 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## v1.0.0-alpha.5

* [can now provide path where typescript can be found](https://github.com/Realytics/fork-ts-checker-webpack-plugin/pull/204)

## v1.0.0-alpha.4

* [make node 6 compatible](https://github.com/Realytics/fork-ts-checker-webpack-plugin/pull/202)
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,9 @@ may be much faster, even compared to multi-threaded compilation.
If true, the plugin will measure the time spent inside the compilation code. This may be useful to compare modes,
especially if there are other loaders/plugins involved in the compilation. **requires node 8+**

* **typescript** `string`:
If supplied this is a custom path where `typescript` can be found. Defaults to `require.resolve('typescript')`.

### Pre-computed consts:
* `ForkTsCheckerWebpackPlugin.ONE_CPU` - always use one CPU
* `ForkTsCheckerWebpackPlugin.ALL_CPUS` - always use all CPUs (will increase build time)
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.0.0-alpha.4",
"version": "1.0.0-alpha.5",
"description": "Runs typescript type checker and linter on separate process.",
"main": "lib/index.js",
"types": "lib/index.d.ts",
Expand Down
10 changes: 8 additions & 2 deletions src/ApiIncrementalChecker.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import * as ts from 'typescript';
// tslint:disable-next-line:no-implicit-dependencies
import * as ts from 'typescript'; // Imported for types alone
// tslint:disable-next-line:no-implicit-dependencies
import { Configuration, Linter, LintResult } from 'tslint';
import * as minimatch from 'minimatch';
import * as path from 'path';
import { IncrementalCheckerInterface } from './IncrementalCheckerInterface';
import { CancellationToken } from './CancellationToken';
import { NormalizedMessage } from './NormalizedMessage';
import { Configuration, Linter, LintResult } from 'tslint';
import { CompilerHost } from './CompilerHost';
import { FsHelper } from './FsHelper';

Expand All @@ -28,6 +30,7 @@ export class ApiIncrementalChecker implements IncrementalCheckerInterface {
private lastRemovedFiles: string[] = [];

constructor(
typescript: typeof ts,
programConfigFile: string,
compilerOptions: ts.CompilerOptions,
private linterConfigFile: string | false,
Expand All @@ -37,6 +40,7 @@ export class ApiIncrementalChecker implements IncrementalCheckerInterface {
this.initLinterConfig();

this.tsIncrementalCompiler = new CompilerHost(
typescript,
programConfigFile,
compilerOptions,
checkSyntacticErrors
Expand Down Expand Up @@ -64,6 +68,7 @@ export class ApiIncrementalChecker implements IncrementalCheckerInterface {
}

private static loadLinterConfig(configFile: string): ConfigurationFile {
// tslint:disable-next-line:no-implicit-dependencies
const tslint = require('tslint');

return tslint.Configuration.loadConfigurationFromPath(
Expand All @@ -72,6 +77,7 @@ export class ApiIncrementalChecker implements IncrementalCheckerInterface {
}

private createLinter(program: ts.Program): Linter {
// tslint:disable-next-line:no-implicit-dependencies
const tslint = require('tslint');

return new tslint.Linter({ fix: this.linterAutoFix }, program);
Expand Down
22 changes: 17 additions & 5 deletions src/CancellationToken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import * as crypto from 'crypto';
import * as fs from 'fs';
import * as os from 'os';
import * as path from 'path';
import * as ts from 'typescript';
// tslint:disable-next-line:no-implicit-dependencies
import * as ts from 'typescript'; // Imported for types alone

import { FsHelper } from './FsHelper';

Expand All @@ -15,15 +16,26 @@ export class CancellationToken {
private isCancelled: boolean;
private cancellationFileName: string;
private lastCancellationCheckTime: number;
constructor(cancellationFileName?: string, isCancelled?: boolean) {
constructor(
private typescript: typeof ts,
cancellationFileName?: string,
isCancelled?: boolean
) {
this.isCancelled = !!isCancelled;
this.cancellationFileName =
cancellationFileName || crypto.randomBytes(64).toString('hex');
this.lastCancellationCheckTime = 0;
}

public static createFromJSON(json: CancellationTokenData) {
return new CancellationToken(json.cancellationFileName, json.isCancelled);
public static createFromJSON(
typescript: typeof ts,
json: CancellationTokenData
) {
return new CancellationToken(
typescript,
json.cancellationFileName,
json.isCancelled
);
}

public toJSON() {
Expand Down Expand Up @@ -56,7 +68,7 @@ export class CancellationToken {

public throwIfCancellationRequested() {
if (this.isCancellationRequested()) {
throw new ts.OperationCanceledException();
throw new this.typescript.OperationCanceledException();
}
}

Expand Down
35 changes: 23 additions & 12 deletions src/CompilerHost.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as ts from 'typescript';
// tslint:disable-next-line:no-implicit-dependencies
import * as ts from 'typescript'; // Imported for types alone
import { LinkedList } from './LinkedList';
import { VueProgram } from './VueProgram';

Expand Down Expand Up @@ -51,15 +52,16 @@ export class CompilerHost
private compilationStarted = false;

constructor(
private typescript: typeof ts,
programConfigFile: string,
compilerOptions: ts.CompilerOptions,
checkSyntacticErrors: boolean
) {
this.tsHost = ts.createWatchCompilerHost(
this.tsHost = typescript.createWatchCompilerHost(
programConfigFile,
compilerOptions,
ts.sys,
ts.createEmitAndSemanticDiagnosticsBuilderProgram,
typescript.sys,
typescript.createEmitAndSemanticDiagnosticsBuilderProgram,
(diag: ts.Diagnostic) => {
if (!checkSyntacticErrors && diag.code >= 1000 && diag.code < 2000) {
return;
Expand Down Expand Up @@ -133,11 +135,13 @@ export class CompilerHost
item.callback(e.fileName, e.eventKind);
files.push(e.fileName);
if (
e.eventKind === ts.FileWatcherEventKind.Created ||
e.eventKind === ts.FileWatcherEventKind.Changed
e.eventKind === this.typescript.FileWatcherEventKind.Created ||
e.eventKind === this.typescript.FileWatcherEventKind.Changed
) {
updatedFiles.push(e.fileName);
} else if (e.eventKind === ts.FileWatcherEventKind.Deleted) {
} else if (
e.eventKind === this.typescript.FileWatcherEventKind.Deleted
) {
removedFiles.push(e.fileName);
}
}
Expand Down Expand Up @@ -186,11 +190,11 @@ export class CompilerHost
// dramatic compilation time increase), so we have to stick with these
// hacks for now.
this.compilationStarted = true;
return ts.sys.setTimeout!(callback, 1, args);
return this.typescript.sys.setTimeout!(callback, 1, args);
}

public clearTimeout(timeoutId: any): void {
ts.sys.clearTimeout!(timeoutId);
this.typescript.sys.clearTimeout!(timeoutId);
}

public onWatchStatusChange(
Expand Down Expand Up @@ -258,7 +262,7 @@ export class CompilerHost

// get typescript contents from Vue file
if (content && VueProgram.isVue(path)) {
const resolved = VueProgram.resolveScriptBlock(content);
const resolved = VueProgram.resolveScriptBlock(this.typescript, content);
return resolved.content;
}

Expand All @@ -285,10 +289,17 @@ export class CompilerHost
include?: ReadonlyArray<string>,
depth?: number
): string[] {
return ts.sys.readDirectory(path, extensions, exclude, include, depth);
return this.typescript.sys.readDirectory(
path,
extensions,
exclude,
include,
depth
);
}

public createProgram = ts.createEmitAndSemanticDiagnosticsBuilderProgram;
public createProgram = this.typescript
.createEmitAndSemanticDiagnosticsBuilderProgram;

public getCurrentDirectory(): string {
return this.tsHost.getCurrentDirectory();
Expand Down
6 changes: 4 additions & 2 deletions src/FilesRegister.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import * as ts from 'typescript';
import { RuleFailure } from 'tslint';
// tslint:disable-next-line:no-implicit-dependencies
import * as ts from 'typescript'; // import for types alone
// tslint:disable-next-line:no-implicit-dependencies
import { RuleFailure } from 'tslint'; // import for types alone

export interface DataShape {
source?: ts.SourceFile;
Expand Down
31 changes: 24 additions & 7 deletions src/IncrementalChecker.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import * as fs from 'fs';
import * as path from 'path';
import * as ts from 'typescript';
// tslint:disable-next-line:no-implicit-dependencies
import * as ts from 'typescript'; // Imported for types alone; actual requires take place in methods below
// tslint:disable-next-line:no-implicit-dependencies
import { Configuration, Linter, RuleFailure } from 'tslint'; // Imported for types alone; actual requires take place in methods below
import { FilesRegister } from './FilesRegister';
import { FilesWatcher } from './FilesWatcher';
Expand Down Expand Up @@ -42,6 +44,7 @@ export class IncrementalChecker implements IncrementalCheckerInterface {
private watcher?: FilesWatcher;

constructor(
private typescript: typeof ts,
private programConfigFile: string,
private compilerOptions: object,
private linterConfigFile: string | false,
Expand All @@ -53,25 +56,33 @@ export class IncrementalChecker implements IncrementalCheckerInterface {
private vue: boolean = false
) {}

public static loadProgramConfig(configFile: string, compilerOptions: object) {
const tsconfig = ts.readConfigFile(configFile, ts.sys.readFile).config;
public static loadProgramConfig(
typescript: typeof ts,
configFile: string,
compilerOptions: object
) {
const tsconfig = typescript.readConfigFile(
configFile,
typescript.sys.readFile
).config;

tsconfig.compilerOptions = tsconfig.compilerOptions || {};
tsconfig.compilerOptions = {
...tsconfig.compilerOptions,
...compilerOptions
};

const parsed = ts.parseJsonConfigFileContent(
const parsed = typescript.parseJsonConfigFileContent(
tsconfig,
ts.sys,
typescript.sys,
path.dirname(configFile)
);

return parsed;
}

private static loadLinterConfig(configFile: string): ConfigurationFile {
// tslint:disable-next-line:no-implicit-dependencies
const tslint = require('tslint');

return tslint.Configuration.loadConfigurationFromPath(
Expand All @@ -80,12 +91,13 @@ export class IncrementalChecker implements IncrementalCheckerInterface {
}

private static createProgram(
typescript: typeof ts,
programConfig: ts.ParsedCommandLine,
files: FilesRegister,
watcher: FilesWatcher,
oldProgram: ts.Program
) {
const host = ts.createCompilerHost(programConfig.options);
const host = typescript.createCompilerHost(programConfig.options);
const realGetSourceFile = host.getSourceFile;

host.getSourceFile = (filePath, languageVersion, onError) => {
Expand All @@ -111,7 +123,7 @@ export class IncrementalChecker implements IncrementalCheckerInterface {
return files.getData(filePath).source;
};

return ts.createProgram(
return typescript.createProgram(
programConfig.fileNames,
programConfig.options,
host,
Expand All @@ -120,6 +132,7 @@ export class IncrementalChecker implements IncrementalCheckerInterface {
}

private createLinter(program: ts.Program) {
// tslint:disable-next-line:no-implicit-dependencies
const tslint = require('tslint');

return new tslint.Linter({ fix: this.linterAutoFix }, program);
Expand Down Expand Up @@ -186,11 +199,13 @@ export class IncrementalChecker implements IncrementalCheckerInterface {
this.programConfig =
this.programConfig ||
VueProgram.loadProgramConfig(
this.typescript,
this.programConfigFile,
this.compilerOptions
);

return VueProgram.createProgram(
this.typescript,
this.programConfig,
path.dirname(this.programConfigFile),
this.files,
Expand All @@ -203,11 +218,13 @@ export class IncrementalChecker implements IncrementalCheckerInterface {
this.programConfig =
this.programConfig ||
IncrementalChecker.loadProgramConfig(
this.typescript,
this.programConfigFile,
this.compilerOptions
);

return IncrementalChecker.createProgram(
this.typescript,
this.programConfig,
this.files,
this.watcher!,
Expand Down
2 changes: 2 additions & 0 deletions src/NormalizedMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import {
Diagnostic,
DiagnosticCategory,
flattenDiagnosticMessageText
// tslint:disable-next-line:no-implicit-dependencies
} from 'typescript';
// tslint:disable-next-line:no-implicit-dependencies
import { RuleFailure } from 'tslint';

type ErrorType = 'diagnostic' | 'lint';
Expand Down
Loading

0 comments on commit 4537c16

Please sign in to comment.