Skip to content

Commit f7dfdcf

Browse files
authored
feat: migrate from reporters to workers (#691)
Use simple functions and modules to simplify worker code
1 parent a0ad06f commit f7dfdcf

File tree

105 files changed

+2187
-3123
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

105 files changed

+2187
-3123
lines changed

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
*.log
33

44
# Package artifacts
5-
lib
5+
/lib
66

77
# Package archive used by e2e tests
88
fork-ts-checker-webpack-plugin-0.0.0-semantic-release.tgz

src/ForkTsCheckerWebpackPlugin.ts

+19-14
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import * as path from 'path';
2+
13
import { cosmiconfigSync } from 'cosmiconfig';
24
import merge from 'deepmerge';
35
import type { JSONSchema7 } from 'json-schema';
@@ -15,10 +17,12 @@ import { dependenciesPool, issuesPool } from './hooks/pluginPools';
1517
import { tapAfterCompileToAddDependencies } from './hooks/tapAfterCompileToAddDependencies';
1618
import { tapAfterEnvironmentToPatchWatching } from './hooks/tapAfterEnvironmentToPatchWatching';
1719
import { tapErrorToLogMessage } from './hooks/tapErrorToLogMessage';
18-
import { tapStartToConnectAndRunReporter } from './hooks/tapStartToConnectAndRunReporter';
19-
import { tapStopToDisconnectReporter } from './hooks/tapStopToDisconnectReporter';
20-
import { createTypeScriptReporterRpcClient } from './typescript-reporter/reporter/TypeScriptReporterRpcClient';
21-
import { assertTypeScriptSupport } from './typescript-reporter/TypeScriptSupport';
20+
import { tapStartToRunWorkers } from './hooks/tapStartToRunWorkers';
21+
import { tapStopToTerminateWorkers } from './hooks/tapStopToTerminateWorkers';
22+
import { assertTypeScriptSupport } from './typescript/TypeScriptSupport';
23+
import type { GetDependenciesWorker } from './typescript/worker/get-dependencies-worker';
24+
import type { GetIssuesWorker } from './typescript/worker/get-issues-worker';
25+
import { createRpcWorker } from './utils/rpc';
2226

2327
class ForkTsCheckerWebpackPlugin {
2428
/**
@@ -61,19 +65,20 @@ class ForkTsCheckerWebpackPlugin {
6165
const state = createForkTsCheckerWebpackPluginState();
6266

6367
assertTypeScriptSupport(configuration.typescript);
64-
const issuesReporter = createTypeScriptReporterRpcClient(configuration.typescript);
65-
const dependenciesReporter = createTypeScriptReporterRpcClient(configuration.typescript);
68+
const getIssuesWorker = createRpcWorker<GetIssuesWorker>(
69+
path.resolve(__dirname, './typescript/worker/get-issues-worker.js'),
70+
configuration.typescript,
71+
configuration.typescript.memoryLimit
72+
);
73+
const getDependenciesWorker = createRpcWorker<GetDependenciesWorker>(
74+
path.resolve(__dirname, './typescript/worker/get-dependencies-worker.js'),
75+
configuration.typescript
76+
);
6677

6778
tapAfterEnvironmentToPatchWatching(compiler, state);
68-
tapStartToConnectAndRunReporter(
69-
compiler,
70-
issuesReporter,
71-
dependenciesReporter,
72-
configuration,
73-
state
74-
);
79+
tapStartToRunWorkers(compiler, getIssuesWorker, getDependenciesWorker, configuration, state);
7580
tapAfterCompileToAddDependencies(compiler, configuration, state);
76-
tapStopToDisconnectReporter(compiler, issuesReporter, dependenciesReporter, state);
81+
tapStopToTerminateWorkers(compiler, getIssuesWorker, getDependenciesWorker, state);
7782
tapErrorToLogMessage(compiler, configuration);
7883
}
7984
}

src/ForkTsCheckerWebpackPluginConfiguration.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import type { IssueConfiguration } from './issue/IssueConfiguration';
77
import { createIssueConfiguration } from './issue/IssueConfiguration';
88
import type { LoggerConfiguration } from './logger/LoggerConfiguration';
99
import { createLoggerConfiguration } from './logger/LoggerConfiguration';
10-
import type { TypeScriptReporterConfiguration } from './typescript-reporter/TypeScriptReporterConfiguration';
11-
import { createTypeScriptReporterConfiguration } from './typescript-reporter/TypeScriptReporterConfiguration';
10+
import type { TypeScriptReporterConfiguration } from './typescript/TypeScriptReporterConfiguration';
11+
import { createTypeScriptReporterConfiguration } from './typescript/TypeScriptReporterConfiguration';
1212

1313
interface ForkTsCheckerWebpackPluginConfiguration {
1414
async: boolean;

src/ForkTsCheckerWebpackPluginOptions.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { FormatterOptions } from './formatter';
22
import type { IssueOptions } from './issue/IssueOptions';
33
import type LoggerOptions from './logger/LoggerOptions';
4-
import type { TypeScriptReporterOptions } from './typescript-reporter/TypeScriptReporterOptions';
4+
import type { TypeScriptReporterOptions } from './typescript/TypeScriptReporterOptions';
55

66
interface ForkTsCheckerWebpackPluginOptions {
77
async?: boolean;

src/ForkTsCheckerWebpackPluginState.ts

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,26 @@
11
import type { FullTap } from 'tapable';
22

3+
import type { FilesMatch } from './files-match';
34
import type { Issue } from './issue';
4-
import type { FilesMatch, Report } from './reporter';
55

66
interface ForkTsCheckerWebpackPluginState {
7-
issuesReportPromise: Promise<Report | undefined>;
8-
dependenciesReportPromise: Promise<Report | undefined>;
97
issuesPromise: Promise<Issue[] | undefined>;
108
dependenciesPromise: Promise<FilesMatch | undefined>;
119
lastDependencies: FilesMatch | undefined;
1210
watching: boolean;
1311
initialized: boolean;
12+
iteration: number;
1413
webpackDevServerDoneTap: FullTap | undefined;
1514
}
1615

1716
function createForkTsCheckerWebpackPluginState(): ForkTsCheckerWebpackPluginState {
1817
return {
19-
issuesReportPromise: Promise.resolve(undefined),
20-
dependenciesReportPromise: Promise.resolve(undefined),
2118
issuesPromise: Promise.resolve(undefined),
2219
dependenciesPromise: Promise.resolve(undefined),
2320
lastDependencies: undefined,
2421
watching: false,
2522
initialized: false,
23+
iteration: 0,
2624
webpackDevServerDoneTap: undefined,
2725
};
2826
}

src/error/OperationCanceledError.ts

-5
This file was deleted.

src/reporter/FilesChange.ts renamed to src/files-change.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
1-
import type { Compiler } from 'webpack';
1+
import type * as webpack from 'webpack';
22

3-
import subtract from '../utils/array/substract';
4-
import unique from '../utils/array/unique';
3+
import subtract from './utils/array/substract';
4+
import unique from './utils/array/unique';
55

66
interface FilesChange {
77
changedFiles?: string[];
88
deletedFiles?: string[];
99
}
1010

11-
const compilerFilesChangeMap = new WeakMap<Compiler, FilesChange>();
11+
const compilerFilesChangeMap = new WeakMap<webpack.Compiler, FilesChange>();
1212

13-
function getFilesChange(compiler: Compiler): FilesChange {
13+
function getFilesChange(compiler: webpack.Compiler): FilesChange {
1414
return compilerFilesChangeMap.get(compiler) || { changedFiles: [], deletedFiles: [] };
1515
}
1616

17-
function updateFilesChange(compiler: Compiler, change: FilesChange): void {
17+
function updateFilesChange(compiler: webpack.Compiler, change: FilesChange): void {
1818
compilerFilesChangeMap.set(compiler, aggregateFilesChanges([getFilesChange(compiler), change]));
1919
}
2020

21-
function clearFilesChange(compiler: Compiler): void {
21+
function clearFilesChange(compiler: webpack.Compiler): void {
2222
compilerFilesChangeMap.delete(compiler);
2323
}
2424

File renamed without changes.

src/hooks/interceptDoneToGetWebpackDevServerTap.ts

+4
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@ import type webpack from 'webpack';
22

33
import type { ForkTsCheckerWebpackPluginConfiguration } from '../ForkTsCheckerWebpackPluginConfiguration';
44
import type { ForkTsCheckerWebpackPluginState } from '../ForkTsCheckerWebpackPluginState';
5+
import { getInfrastructureLogger } from '../infrastructure-logger';
56

67
function interceptDoneToGetWebpackDevServerTap(
78
compiler: webpack.Compiler,
89
configuration: ForkTsCheckerWebpackPluginConfiguration,
910
state: ForkTsCheckerWebpackPluginState
1011
) {
12+
const { debug } = getInfrastructureLogger(compiler);
13+
1114
// inspired by https://github.com/ypresto/fork-ts-checker-async-overlay-webpack-plugin
1215
compiler.hooks.done.intercept({
1316
register: (tap) => {
@@ -16,6 +19,7 @@ function interceptDoneToGetWebpackDevServerTap(
1619
tap.type === 'sync' &&
1720
configuration.logger.devServer
1821
) {
22+
debug('Intercepting webpack-dev-server tap.');
1923
state.webpackDevServerDoneTap = tap;
2024
}
2125
return tap;

src/hooks/pluginHooks.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { SyncHook, SyncWaterfallHook, AsyncSeriesWaterfallHook } from 'tapable';
22
import type * as webpack from 'webpack';
33

4+
import type { FilesChange } from '../files-change';
45
import type { Issue } from '../issue';
5-
import type { FilesChange } from '../reporter';
66

77
const compilerHookMap = new WeakMap<
88
webpack.Compiler | webpack.MultiCompiler,

src/hooks/tapAfterCompileToAddDependencies.ts

+4
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@ import type webpack from 'webpack';
22

33
import type { ForkTsCheckerWebpackPluginConfiguration } from '../ForkTsCheckerWebpackPluginConfiguration';
44
import type { ForkTsCheckerWebpackPluginState } from '../ForkTsCheckerWebpackPluginState';
5+
import { getInfrastructureLogger } from '../infrastructure-logger';
56

67
function tapAfterCompileToAddDependencies(
78
compiler: webpack.Compiler,
89
configuration: ForkTsCheckerWebpackPluginConfiguration,
910
state: ForkTsCheckerWebpackPluginState
1011
) {
12+
const { debug } = getInfrastructureLogger(compiler);
13+
1114
compiler.hooks.afterCompile.tapPromise('ForkTsCheckerWebpackPlugin', async (compilation) => {
1215
if (compilation.compiler !== compiler) {
1316
// run only for the compiler that the plugin was registered for
@@ -16,6 +19,7 @@ function tapAfterCompileToAddDependencies(
1619

1720
const dependencies = await state.dependenciesPromise;
1821

22+
debug(`Got dependencies from the getDependenciesWorker.`, dependencies);
1923
if (dependencies) {
2024
state.lastDependencies = dependencies;
2125

src/hooks/tapAfterCompileToGetIssues.ts

+4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type webpack from 'webpack';
22

33
import type { ForkTsCheckerWebpackPluginConfiguration } from '../ForkTsCheckerWebpackPluginConfiguration';
44
import type { ForkTsCheckerWebpackPluginState } from '../ForkTsCheckerWebpackPluginState';
5+
import { getInfrastructureLogger } from '../infrastructure-logger';
56
import type { Issue } from '../issue';
67
import { IssueWebpackError } from '../issue/IssueWebpackError';
78

@@ -13,6 +14,7 @@ function tapAfterCompileToGetIssues(
1314
state: ForkTsCheckerWebpackPluginState
1415
) {
1516
const hooks = getForkTsCheckerWebpackPluginHooks(compiler);
17+
const { debug } = getInfrastructureLogger(compiler);
1618

1719
compiler.hooks.afterCompile.tapPromise('ForkTsCheckerWebpackPlugin', async (compilation) => {
1820
if (compilation.compiler !== compiler) {
@@ -29,6 +31,8 @@ function tapAfterCompileToGetIssues(
2931
return;
3032
}
3133

34+
debug('Got issues from getIssuesWorker.', issues?.length);
35+
3236
if (!issues) {
3337
// some error has been thrown or it was canceled
3438
return;

src/hooks/tapAfterEnvironmentToPatchWatching.ts

+6
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,29 @@
11
import type webpack from 'webpack';
22

33
import type { ForkTsCheckerWebpackPluginState } from '../ForkTsCheckerWebpackPluginState';
4+
import { getInfrastructureLogger } from '../infrastructure-logger';
45
import { InclusiveNodeWatchFileSystem } from '../watch/InclusiveNodeWatchFileSystem';
56
import type { WatchFileSystem } from '../watch/WatchFileSystem';
67

78
function tapAfterEnvironmentToPatchWatching(
89
compiler: webpack.Compiler,
910
state: ForkTsCheckerWebpackPluginState
1011
) {
12+
const { debug } = getInfrastructureLogger(compiler);
13+
1114
compiler.hooks.afterEnvironment.tap('ForkTsCheckerWebpackPlugin', () => {
1215
const watchFileSystem = compiler.watchFileSystem;
1316
if (watchFileSystem) {
17+
debug("Overwriting webpack's watch file system.");
1418
// wrap original watch file system
1519
compiler.watchFileSystem = new InclusiveNodeWatchFileSystem(
1620
// we use some internals here
1721
watchFileSystem as WatchFileSystem,
1822
compiler,
1923
state
2024
);
25+
} else {
26+
debug('No watch file system found - plugin may not work correctly.');
2127
}
2228
});
2329
}

src/hooks/tapDoneToAsyncGetIssues.ts

+5-9
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import type webpack from 'webpack';
44
import type { ForkTsCheckerWebpackPluginConfiguration } from '../ForkTsCheckerWebpackPluginConfiguration';
55
import type { ForkTsCheckerWebpackPluginState } from '../ForkTsCheckerWebpackPluginState';
66
import { createWebpackFormatter } from '../formatter/WebpackFormatter';
7+
import { getInfrastructureLogger } from '../infrastructure-logger';
78
import type { Issue } from '../issue';
89
import { IssueWebpackError } from '../issue/IssueWebpackError';
910
import isPending from '../utils/async/isPending';
@@ -17,14 +18,14 @@ function tapDoneToAsyncGetIssues(
1718
state: ForkTsCheckerWebpackPluginState
1819
) {
1920
const hooks = getForkTsCheckerWebpackPluginHooks(compiler);
21+
const { log, debug } = getInfrastructureLogger(compiler);
2022

2123
compiler.hooks.done.tap('ForkTsCheckerWebpackPlugin', async (stats) => {
2224
if (stats.compilation.compiler !== compiler) {
2325
// run only for the compiler that the plugin was registered for
2426
return;
2527
}
2628

27-
const reportPromise = state.issuesReportPromise;
2829
const issuesPromise = state.issuesPromise;
2930
let issues: Issue[] | undefined;
3031

@@ -38,6 +39,7 @@ function tapDoneToAsyncGetIssues(
3839
}
3940

4041
issues = await issuesPromise;
42+
debug('Got issues from getIssuesWorker.', issues?.length);
4143
} catch (error) {
4244
hooks.error.call(error, stats.compilation);
4345
return;
@@ -48,11 +50,6 @@ function tapDoneToAsyncGetIssues(
4850
return;
4951
}
5052

51-
if (reportPromise !== state.issuesReportPromise) {
52-
// there is a newer report - ignore this one
53-
return;
54-
}
55-
5653
// filter list of issues by provided issue predicate
5754
issues = issues.filter(configuration.issue.predicate);
5855

@@ -81,13 +78,12 @@ function tapDoneToAsyncGetIssues(
8178
}
8279
});
8380

81+
debug('Sending issues to the webpack-dev-server.');
8482
state.webpackDevServerDoneTap.fn(stats);
8583
}
8684

8785
if (stats.startTime) {
88-
configuration.logger.infrastructure.log(
89-
`Time: ${Math.round(Date.now() - stats.startTime).toString()} ms`
90-
);
86+
log(`Time: ${Math.round(Date.now() - stats.startTime).toString()} ms`);
9187
}
9288
});
9389
}

src/hooks/tapErrorToLogMessage.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import chalk from 'chalk';
22
import type webpack from 'webpack';
33

44
import type { ForkTsCheckerWebpackPluginConfiguration } from '../ForkTsCheckerWebpackPluginConfiguration';
5-
import { RpcIpcMessagePortClosedError } from '../rpc/rpc-ipc/error/RpcIpcMessagePortClosedError';
5+
import { RpcExitError } from '../utils/rpc';
66

77
import { getForkTsCheckerWebpackPluginHooks } from './pluginHooks';
88

@@ -15,7 +15,7 @@ function tapErrorToLogMessage(
1515
hooks.error.tap('ForkTsCheckerWebpackPlugin', (error) => {
1616
configuration.logger.issues.error(String(error));
1717

18-
if (error instanceof RpcIpcMessagePortClosedError) {
18+
if (error instanceof RpcExitError) {
1919
if (error.signal === 'SIGINT') {
2020
configuration.logger.issues.error(
2121
chalk.red(

0 commit comments

Comments
 (0)