Skip to content

Commit

Permalink
feat: simplify logger options (#695)
Browse files Browse the repository at this point in the history
Currently, the logger options are overcomplicated. To simplify them, we
are removing logger.infrastructure option.

BREAKING CHANGE: 🧨 Changes in options: `logger.issues` becomes `logger`, `logger.devServer`
becomes `devServer`, `logger.infrastructure` has been removed
  • Loading branch information
piotr-oles authored Jan 28, 2022
1 parent 39ebae6 commit 4b13fd3
Show file tree
Hide file tree
Showing 14 changed files with 33 additions and 170 deletions.
17 changes: 9 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,14 @@ you can place your configuration in the:

Options passed to the plugin constructor will overwrite options from the cosmiconfig (using [deepmerge](https://github.com/TehShrike/deepmerge)).

| Name | Type | Default value | Description |
| ----------------- | ---------------------------------- | ------------------------------------------------------------------ | ----------- |
| `async` | `boolean` | `compiler.options.mode === 'development'` | If `true`, reports issues **after** webpack's compilation is done. Thanks to that it doesn't block the compilation. Used only in the `watch` mode. |
| `typescript` | `object` | `{}` | See [TypeScript options](#typescript-options). |
| `issue` | `object` | `{}` | See [Issues options](#issues-options). |
| `formatter` | `string` or `object` or `function` | `codeframe` | Available formatters are `basic`, `codeframe` and a custom `function`. To [configure](https://babeljs.io/docs/en/babel-code-frame#options) `codeframe` formatter, pass object: `{ type: 'codeframe', options: { <coderame options> } }`. |
| `logger` | `object` | `{ infrastructure: 'silent', issues: 'console', devServer: true }` | Available loggers are `silent`, `console`, and `webpack-infrastructure`. Infrastructure logger prints additional information, issue logger prints `issues` in the `async` mode. If `devServer` is set to `false`, errors will not be reported to Webpack Dev Server. |
| Name | Type | Default value | Description |
| ----------------- | ------------------------------------ | ----------------------------------------- | ----------- |
| `async` | `boolean` | `compiler.options.mode === 'development'` | If `true`, reports issues **after** webpack's compilation is done. Thanks to that it doesn't block the compilation. Used only in the `watch` mode. |
| `typescript` | `object` | `{}` | See [TypeScript options](#typescript-options). |
| `issue` | `object` | `{}` | See [Issues options](#issues-options). |
| `formatter` | `string` or `object` or `function` | `codeframe` | Available formatters are `basic`, `codeframe` and a custom `function`. To [configure](https://babeljs.io/docs/en/babel-code-frame#options) `codeframe` formatter, pass object: `{ type: 'codeframe', options: { <coderame options> } }`. |
| `logger` | `{ log: function, error: function }` | `console` | Console-like object to print issues in `async` mode. |
| `devServer` | `boolean` | `true` | If set to `false`, errors will not be reported to Webpack Dev Server. |

### TypeScript options

Expand All @@ -105,7 +106,7 @@ Options for the TypeScript checker (`typescript` option object).
| `context` | `string` | `dirname(configuration.configFile)` | The base path for finding files specified in the `tsconfig.json`. Same as the `context` option from the [ts-loader](https://github.com/TypeStrong/ts-loader#context). Useful if you want to keep your `tsconfig.json` in an external package. Keep in mind that **not** having a `tsconfig.json` in your project root can cause different behaviour between `fork-ts-checker-webpack-plugin` and `tsc`. When using editors like `VS Code` it is advised to add a `tsconfig.json` file to the root of the project and extend the config file referenced in option `configFile`. |
| `build` | `boolean` | `false` | The equivalent of the `--build` flag for the `tsc` command. |
| `mode` | `'readonly'` or `'write-tsbuildinfo'` or `'write-references'` | `'write-tsbuildinfo'` | If you use the `babel-loader`, it's recommended to use `write-references` mode to improve initial compilation time. If you use `ts-loader`, it's recommended to use `write-tsbuildinfo` mode to not overwrite files emitted by the `ts-loader`. |
| `diagnosticOptions` | `object` | `{ syntactic: false, semantic: true, declaration: false, global: false }` | Settings to select which diagnostics do we want to perform. |
| `diagnosticOptions` | `object` | `{ syntactic: false, semantic: true, declaration: false, global: false }` | Settings to select which diagnostics do we want to perform. |
| `extensions` | `object` | `{}` | See [TypeScript extensions options](#typescript-extensions-options). |
| `profile` | `boolean` | `false` | Measures and prints timings related to the TypeScript performance. |
| `typescriptPath` | `string` | `require.resolve('typescript')` | If supplied this is a custom path where TypeScript can be found. |
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/intercept-done-to-get-dev-server-tap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ function interceptDoneToGetDevServerTap(
// inspired by https://github.com/ypresto/fork-ts-checker-async-overlay-webpack-plugin
compiler.hooks.done.intercept({
register: (tap) => {
if (tap.name === 'webpack-dev-server' && tap.type === 'sync' && config.logger.devServer) {
if (tap.name === 'webpack-dev-server' && tap.type === 'sync' && config.devServer) {
debug('Intercepting webpack-dev-server tap.');
state.webpackDevServerDoneTap = tap;
}
Expand Down
6 changes: 3 additions & 3 deletions src/hooks/tap-done-to-async-get-issues.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ function tapDoneToAsyncGetIssues(
try {
if (await isPending(issuesPromise)) {
hooks.waiting.call(stats.compilation);
config.logger.issues.log(chalk.cyan('Issues checking in progress...'));
config.logger.log(chalk.cyan('Issues checking in progress...'));
} else {
// wait 10ms to log issues after webpack stats
await wait(10);
Expand Down Expand Up @@ -59,9 +59,9 @@ function tapDoneToAsyncGetIssues(

if (issues.length) {
// follow webpack's approach - one process.write to stderr with all errors and warnings
config.logger.issues.error(issues.map((issue) => formatter(issue)).join('\n'));
config.logger.error(issues.map((issue) => formatter(issue)).join('\n'));
} else {
config.logger.issues.log(chalk.green('No issues found.'));
config.logger.log(chalk.green('No issues found.'));
}

// report issues to webpack-dev-server, if it's listening
Expand Down
6 changes: 3 additions & 3 deletions src/hooks/tap-error-to-log-message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,19 @@ function tapErrorToLogMessage(
const hooks = getPluginHooks(compiler);

hooks.error.tap('ForkTsCheckerWebpackPlugin', (error) => {
config.logger.issues.error(String(error));
config.logger.error(String(error));

if (error instanceof RpcExitError) {
if (error.signal === 'SIGINT') {
config.logger.issues.error(
config.logger.error(
chalk.red(
'Issues checking service interrupted - If running in a docker container, this may be caused ' +
"by the container running out of memory. If so, try increasing the container's memory limit " +
'or lowering the `memoryLimit` value in the ForkTsCheckerWebpackPlugin configuration.'
)
);
} else {
config.logger.issues.error(
config.logger.error(
chalk.red(
'Issues checking service aborted - probably out of memory. ' +
'Check the `memoryLimit` option in the ForkTsCheckerWebpackPlugin configuration.\n' +
Expand Down
3 changes: 1 addition & 2 deletions src/logger/logger.ts → src/logger.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
interface Logger {
info: (message: string) => void;
log: (message: string) => void;
error: (message: string) => void;
}

export default Logger;
export { Logger };
27 changes: 0 additions & 27 deletions src/logger/logger-config.ts

This file was deleted.

30 changes: 0 additions & 30 deletions src/logger/logger-factory.ts

This file was deleted.

10 changes: 0 additions & 10 deletions src/logger/logger-options.ts

This file was deleted.

13 changes: 0 additions & 13 deletions src/logger/partial-logger.ts

This file was deleted.

22 changes: 0 additions & 22 deletions src/logger/webpack-infrastructure-logger.ts

This file was deleted.

9 changes: 5 additions & 4 deletions src/plugin-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import type { FormatterConfig } from './formatter';
import { createFormatterConfig } from './formatter';
import type { IssueConfig } from './issue/issue-config';
import { createIssueConfig } from './issue/issue-config';
import type { LoggerConfig } from './logger/logger-config';
import { createLoggerConfig } from './logger/logger-config';
import type { Logger } from './logger';
import type { ForkTsCheckerWebpackPluginOptions } from './plugin-options';
import type { TypeScriptWorkerConfig } from './typescript/type-script-worker-config';
import { createTypeScriptWorkerConfig } from './typescript/type-script-worker-config';
Expand All @@ -15,7 +14,8 @@ interface ForkTsCheckerWebpackPluginConfig {
typescript: TypeScriptWorkerConfig;
issue: IssueConfig;
formatter: FormatterConfig;
logger: LoggerConfig;
logger: Logger;
devServer: boolean;
}

function createPluginConfig(
Expand All @@ -27,7 +27,8 @@ function createPluginConfig(
typescript: createTypeScriptWorkerConfig(compiler, options.typescript),
issue: createIssueConfig(compiler, options.issue),
formatter: createFormatterConfig(options.formatter),
logger: createLoggerConfig(compiler, options.logger),
logger: options.logger || console,
devServer: options.devServer !== false,
};
}

Expand Down
50 changes: 7 additions & 43 deletions src/plugin-options.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"description": "When true, plugin will not block compilation to finish issues checking"
},
"typescript": {
"$ref": "#/definitions/TypeScriptReporterOptions"
"$ref": "#/definitions/TypeScriptOptions"
},
"formatter": {
"$ref": "#/definitions/FormatterOptions"
Expand All @@ -16,7 +16,11 @@
"$ref": "#/definitions/IssueOptions"
},
"logger": {
"$ref": "#/definitions/LoggerOptions"
"$ref": "#/definitions/Logger"
},
"devServer": {
"type": "boolean",
"description": "Enable reporting to Webpack Dev Server."
}
},
"additionalProperties": false,
Expand Down Expand Up @@ -90,29 +94,18 @@
}
]
},
"LoggerType": {
"type": "string",
"enum": [
"console",
"webpack-infrastructure",
"silent"
]
},
"Logger": {
"type": "object",
"properties": {
"error": {
"instanceof": "Function"
},
"info": {
"instanceof": "Function"
},
"log": {
"instanceof": "Function"
}
}
},
"TypeScriptReporterOptions": {
"TypeScriptOptions": {
"type": "object",
"properties": {
"memoryLimit": {
Expand Down Expand Up @@ -225,35 +218,6 @@
"$ref": "#/definitions/IssuePredicateOption"
}
}
},
"LoggerOptions": {
"type": "object",
"properties": {
"infrastructure": {
"oneOf": [
{
"$ref": "#/definitions/LoggerType"
},
{
"$ref": "#/definitions/Logger"
}
]
},
"issues": {
"oneOf": [
{
"$ref": "#/definitions/LoggerType"
},
{
"$ref": "#/definitions/Logger"
}
]
},
"devServer": {
"type": "boolean",
"description": "Enable reporting to Webpack Dev Server."
}
}
}
}
}
5 changes: 3 additions & 2 deletions src/plugin-options.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import type { FormatterOptions } from './formatter';
import type { IssueOptions } from './issue/issue-options';
import type LoggerOptions from './logger/logger-options';
import type { Logger } from './logger';
import type { TypeScriptWorkerOptions } from './typescript/type-script-worker-options';

interface ForkTsCheckerWebpackPluginOptions {
async?: boolean;
typescript?: TypeScriptWorkerOptions;
formatter?: FormatterOptions;
issue?: IssueOptions;
logger?: LoggerOptions;
logger?: Logger;
devServer?: boolean;
}

export { ForkTsCheckerWebpackPluginOptions };
3 changes: 1 addition & 2 deletions test/unit/plugin.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,9 @@ describe('plugin', () => {
it('accepts a custom logger', () => {
const logger = {
error: (message) => console.log(message),
info: (message) => console.log(message),
log: (message) => console.log(message),
};

expect(() => new ForkTsCheckerWebpackPlugin({ logger: { issues: logger } })).not.toThrowError();
expect(() => new ForkTsCheckerWebpackPlugin({ logger })).not.toThrowError();
});
});

0 comments on commit 4b13fd3

Please sign in to comment.