Skip to content
This repository was archived by the owner on Dec 1, 2019. It is now read-only.

Commit 91b55ef

Browse files
committed
Merge pull request #52 from s-panferov/feature/fix-checker
feat(*): checker in sep. process now runs only with ForkCheckerPlugin
2 parents 2b5176e + 85e9e1b commit 91b55ef

File tree

6 files changed

+50
-47
lines changed

6 files changed

+50
-47
lines changed

README.md

+14
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,20 @@ Use this setting to disable type checking if you want.
111111

112112
Do type checking in a separate process, so webpack don't need to wait. **Significantly** improves development workflow with tools like [react-hot-loader](https://github.com/gaearon/react-hot-loader).
113113

114+
Works only with `ForkCheckerPlugin`:
115+
116+
```js
117+
var ForkCheckerPlugin = require('awesome-typescript-loader').ForkCheckerPlugin;
118+
119+
plugins: [
120+
new ForkCheckerPlugin(),
121+
]
122+
```
123+
124+
### forkCheckerSilent *(boolean) (default=false)*
125+
126+
Less logging from the checker.
127+
114128
### useBabel *(boolean) (default=false)*
115129

116130
Invoke Babel to traspile files. Useful with ES6 target.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,6 @@
4242
"grunt-shell": "^1.1.2",
4343
"grunt-ts": "^3.0.0",
4444
"load-grunt-tasks": "^0.6.0",
45-
"typescript": "^1.7.0-dev.20150901"
45+
"typescript": "^1.6.0-dev.20150910"
4646
}
4747
}

src/checker.ts

+14-40
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,6 @@ import { ICompilerOptions, ICompilerInfo, IFile } from './host';
22
import * as colors from 'colors';
33
import * as _ from 'lodash';
44

5-
const AWESOME_SYNONYMS = [
6-
'awesome',
7-
'impressive',
8-
'amazing',
9-
'grand',
10-
'majestic',
11-
'magnificent',
12-
'wonderful',
13-
'great',
14-
'marvellous',
15-
'incredible',
16-
'fabulous',
17-
'outstanding',
18-
'unbelievable',
19-
'beautiful',
20-
'excellent',
21-
'mind-blowing',
22-
'superb',
23-
'badass',
24-
'brilliant',
25-
'exciting',
26-
'eye-opening',
27-
'fucking good',
28-
'fine',
29-
'perfect',
30-
'cool',
31-
'fantastical',
32-
'five-star'
33-
];
34-
355
export enum MessageType {
366
Init = <any>'init',
377
Compile = <any>'compile'
@@ -129,7 +99,12 @@ function processInit(payload: IInitPayload) {
12999
}
130100

131101
function processCompile(payload: ICompilePayload) {
132-
console.log(colors.cyan(`[${ env.options.instanceName || '' }] Checking started...`));
102+
let instanceName = env.options.instanceName || 'default';
103+
let silent = !!env.options.forkCheckerSilent;
104+
if (!silent) {
105+
console.log(colors.cyan(`[${ instanceName }] Checking started in a separate process...`));
106+
}
107+
133108
let timeStart = +new Date();
134109
process.send({
135110
messageType: 'progress',
@@ -143,23 +118,22 @@ function processCompile(payload: ICompilePayload) {
143118
let program = env.program = env.service.getProgram();
144119
let allDiagnostics = env.compiler.getPreEmitDiagnostics(program);
145120
if (allDiagnostics.length) {
146-
console.error(colors.yellow('Checker diagnostics:'))
147121
allDiagnostics.forEach(diagnostic => {
148122
let message = env.compiler.flattenDiagnosticMessageText(diagnostic.messageText, '\n');
149-
150123
if (diagnostic.file) {
151124
let { line, character } = diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start);
152-
console.error(`${colors.cyan(diagnostic.file.fileName)} (${line + 1},${character + 1}):\n ${colors.red(message)}`);
125+
console.error(`[${ instanceName }] ${colors.red(diagnostic.file.fileName)} (${line + 1},${character + 1}):\n ${colors.red(message)}`);
153126
} else {
154-
console.error(colors.red(message));
127+
console.error(colors.red(`[${ instanceName }] ${ message }`));
155128
}
156129
});
157130
} else {
158-
let timeEnd = +new Date();
159-
console.error(
160-
colors.green(`[${ env.options.instanceName || '' }] Program is `
161-
+ AWESOME_SYNONYMS[_.random(AWESOME_SYNONYMS.length - 1)] + `! ${(timeEnd - timeStart) / 1000} sec.`)
162-
);
131+
if (!silent) {
132+
let timeEnd = +new Date();
133+
console.log(
134+
colors.green(`[${ instanceName }] Ok, ${(timeEnd - timeStart) / 1000} sec.`)
135+
);
136+
}
163137
}
164138

165139
process.send({

src/helpers.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,14 @@ export function codegenErrorReport(errors) {
5555
.join('\n');
5656
}
5757

58-
export function formatErrors(errors: ts.Diagnostic[]) {
58+
export function formatErrors(instanceName: string, errors: ts.Diagnostic[]) {
5959
return errors.map(function (diagnostic) {
6060
let lineChar;
6161
if (diagnostic.file) {
6262
lineChar = diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start);
6363
}
6464
return (
65-
(diagnostic.file ? diagnostic.file.fileName : '')
65+
`[${ instanceName }] ` + (diagnostic.file ? diagnostic.file.fileName : '')
6666
+ (lineChar ? formatLineChar(lineChar) + ' ' : '') + "\n"
6767
+ (typeof diagnostic.messageText == "string" ?
6868
diagnostic.messageText :

src/host.ts

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ export interface ICompilerOptions extends ts.CompilerOptions {
4141
externals?: any;
4242
doTypeCheck?: boolean;
4343
forkChecker?: boolean;
44+
forkCheckerSilent?: boolean;
4445
useBabel?: boolean;
4546
usePrecompiledFiles?: boolean;
4647
useCache?: boolean;

src/index.ts

+18-4
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ let cachePromise = Promise.promisify(cache);
2323
interface ICompiler {
2424
inputFileSystem: typeof fs;
2525
_tsInstances: {[key:string]: ICompilerInstance};
26+
_tsFork: boolean;
2627
options: {
2728
externals: {
2829
[ key: string ]: string
@@ -192,7 +193,7 @@ function ensureInstance(webpack: IWebPack, options: ICompilerOptions, instanceNa
192193

193194
let tsConfigFiles = [];
194195
if (configFileName) {
195-
configFile = tsImpl.readConfigFile(configFileName);
196+
configFile = tsImpl.readConfigFile(configFileName, (path) => fs.readFileSync(path).toString());
196197
if (configFile.error) {
197198
throw configFile.error;
198199
}
@@ -297,12 +298,14 @@ function ensureInstance(webpack: IWebPack, options: ICompilerOptions, instanceNa
297298
}
298299
}
299300

301+
let forkChecker = options.forkChecker && getRootCompiler(webpack._compiler)._tsFork;
300302
let syncResolver = deasync(webpack.resolve);
301303

302304
let tsState = new State(options, webpack._compiler.inputFileSystem, compilerInfo, syncResolver);
303305
let compiler = (<any>webpack._compiler);
304306

305307
compiler.plugin('watch-run', (watching, callback) => {
308+
306309
let resolver = createResolver(watching.compiler, watching.compiler.resolvers.normal.resolve);
307310
let instance: ICompilerInstance = resolveInstance(watching.compiler, instanceName);
308311
let state = instance.tsState;
@@ -335,7 +338,7 @@ function ensureInstance(webpack: IWebPack, options: ICompilerOptions, instanceNa
335338
let instance: ICompilerInstance = resolveInstance(compilation.compiler, instanceName);
336339
let state = instance.tsState;
337340

338-
if (options.forkChecker) {
341+
if (forkChecker) {
339342
let payload = {
340343
files: state.files,
341344
resolutionCache: state.host.moduleResolutionHost.resolutionCache
@@ -355,7 +358,7 @@ function ensureInstance(webpack: IWebPack, options: ICompilerOptions, instanceNa
355358
compilation.errors.push(new Error(err))
356359
};
357360

358-
let errors = helpers.formatErrors(diagnostics);
361+
let errors = helpers.formatErrors(instanceName, diagnostics);
359362
errors.forEach(emitError);
360363
}
361364

@@ -380,7 +383,7 @@ function ensureInstance(webpack: IWebPack, options: ICompilerOptions, instanceNa
380383
compiledFiles: {},
381384
options,
382385
externalsInvoked: false,
383-
checker: options.forkChecker
386+
checker: forkChecker
384387
? createChecker(compilerInfo, options)
385388
: null,
386389
cacheIdentifier
@@ -540,4 +543,15 @@ function compiler(webpack: IWebPack, text: string): void {
540543
.catch((err) => { callback(err) })
541544
}
542545

546+
class ForkCheckerPlugin {
547+
apply(compiler) {
548+
compiler.plugin("watch-run", function(params, callback) {
549+
compiler._tsFork = true;
550+
callback();
551+
});
552+
}
553+
}
554+
555+
(loader as any).ForkCheckerPlugin = ForkCheckerPlugin;
556+
543557
export = loader;

0 commit comments

Comments
 (0)