diff --git a/README.md b/README.md index c355bf33..4bff9878 100644 --- a/README.md +++ b/README.md @@ -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: { } }`. | -| `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: { } }`. | +| `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 @@ -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. | diff --git a/src/hooks/intercept-done-to-get-dev-server-tap.ts b/src/hooks/intercept-done-to-get-dev-server-tap.ts index 1291c863..531645b7 100644 --- a/src/hooks/intercept-done-to-get-dev-server-tap.ts +++ b/src/hooks/intercept-done-to-get-dev-server-tap.ts @@ -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; } diff --git a/src/hooks/tap-done-to-async-get-issues.ts b/src/hooks/tap-done-to-async-get-issues.ts index 8d738ea2..98162e25 100644 --- a/src/hooks/tap-done-to-async-get-issues.ts +++ b/src/hooks/tap-done-to-async-get-issues.ts @@ -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); @@ -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 diff --git a/src/hooks/tap-error-to-log-message.ts b/src/hooks/tap-error-to-log-message.ts index 37323230..3145af11 100644 --- a/src/hooks/tap-error-to-log-message.ts +++ b/src/hooks/tap-error-to-log-message.ts @@ -12,11 +12,11 @@ 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 " + @@ -24,7 +24,7 @@ function tapErrorToLogMessage( ) ); } 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' + diff --git a/src/logger/logger.ts b/src/logger.ts similarity index 61% rename from src/logger/logger.ts rename to src/logger.ts index 7bb7c06d..67964dc7 100644 --- a/src/logger/logger.ts +++ b/src/logger.ts @@ -1,7 +1,6 @@ interface Logger { - info: (message: string) => void; log: (message: string) => void; error: (message: string) => void; } -export default Logger; +export { Logger }; diff --git a/src/logger/logger-config.ts b/src/logger/logger-config.ts deleted file mode 100644 index 6847a226..00000000 --- a/src/logger/logger-config.ts +++ /dev/null @@ -1,27 +0,0 @@ -import type webpack from 'webpack'; - -import type Logger from './logger'; -import { createLogger } from './logger-factory'; -import type LoggerOptions from './logger-options'; - -interface LoggerConfig { - infrastructure: Logger; - issues: Logger; - devServer: boolean; -} - -function createLoggerConfig( - compiler: webpack.Compiler, - options: LoggerOptions | undefined -): LoggerConfig { - return { - infrastructure: createLogger( - (options && options.infrastructure) || 'webpack-infrastructure', - compiler - ), - issues: createLogger((options && options.issues) || 'console', compiler), - devServer: options?.devServer !== false, - }; -} - -export { LoggerConfig, createLoggerConfig }; diff --git a/src/logger/logger-factory.ts b/src/logger/logger-factory.ts deleted file mode 100644 index 972dd187..00000000 --- a/src/logger/logger-factory.ts +++ /dev/null @@ -1,30 +0,0 @@ -import type webpack from 'webpack'; - -import type Logger from './logger'; -import { createPartialLogger } from './partial-logger'; -import { createWebpackInfrastructureLogger } from './webpack-infrastructure-logger'; - -type LoggerType = 'console' | 'webpack-infrastructure' | 'silent'; - -function createLogger(type: LoggerType | Logger, compiler: webpack.Compiler): Logger { - if (typeof type !== 'string') { - return type; - } - - switch (type) { - case 'webpack-infrastructure': - return ( - createWebpackInfrastructureLogger(compiler) || - createPartialLogger(['log', 'error'], console) - ); - - case 'silent': - return createPartialLogger([], console); - - case 'console': - default: - return console; - } -} - -export { createLogger, LoggerType }; diff --git a/src/logger/logger-options.ts b/src/logger/logger-options.ts deleted file mode 100644 index d0167563..00000000 --- a/src/logger/logger-options.ts +++ /dev/null @@ -1,10 +0,0 @@ -import type Logger from './logger'; -import type { LoggerType } from './logger-factory'; - -type LoggerOptions = { - infrastructure?: LoggerType | Logger; - issues?: LoggerType | Logger; - devServer?: boolean; -}; - -export default LoggerOptions; diff --git a/src/logger/partial-logger.ts b/src/logger/partial-logger.ts deleted file mode 100644 index f62c0b5f..00000000 --- a/src/logger/partial-logger.ts +++ /dev/null @@ -1,13 +0,0 @@ -import type Logger from './logger'; - -type LoggerMethods = 'info' | 'log' | 'error'; - -function createPartialLogger(methods: LoggerMethods[], logger: Logger): Logger { - return { - info: (message) => (methods.includes('info') ? logger.info(message) : undefined), - log: (message) => (methods.includes('log') ? logger.log(message) : undefined), - error: (message) => (methods.includes('error') ? logger.error(message) : undefined), - }; -} - -export { createPartialLogger }; diff --git a/src/logger/webpack-infrastructure-logger.ts b/src/logger/webpack-infrastructure-logger.ts deleted file mode 100644 index 5a37a803..00000000 --- a/src/logger/webpack-infrastructure-logger.ts +++ /dev/null @@ -1,22 +0,0 @@ -import type webpack from 'webpack'; - -import type Logger from './logger'; - -interface InfrastructureLoggerProvider { - getInfrastructureLogger(name: string): Logger; -} - -function isInfrastructureLoggerProvider( - candidate: unknown -): candidate is InfrastructureLoggerProvider { - // eslint-disable-next-line @typescript-eslint/no-explicit-any - return !!(candidate as any).getInfrastructureLogger; -} - -function createWebpackInfrastructureLogger(compiler: webpack.Compiler): Logger | undefined { - return isInfrastructureLoggerProvider(compiler) - ? compiler.getInfrastructureLogger('ForkTsCheckerWebpackPlugin') - : undefined; -} - -export { createWebpackInfrastructureLogger }; diff --git a/src/plugin-config.ts b/src/plugin-config.ts index 109066a1..7d01e7b3 100644 --- a/src/plugin-config.ts +++ b/src/plugin-config.ts @@ -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'; @@ -15,7 +14,8 @@ interface ForkTsCheckerWebpackPluginConfig { typescript: TypeScriptWorkerConfig; issue: IssueConfig; formatter: FormatterConfig; - logger: LoggerConfig; + logger: Logger; + devServer: boolean; } function createPluginConfig( @@ -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, }; } diff --git a/src/plugin-options.json b/src/plugin-options.json index 8484d089..0a27f494 100644 --- a/src/plugin-options.json +++ b/src/plugin-options.json @@ -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" @@ -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, @@ -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": { @@ -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." - } - } } } } diff --git a/src/plugin-options.ts b/src/plugin-options.ts index 7e7f24f4..09b0ae47 100644 --- a/src/plugin-options.ts +++ b/src/plugin-options.ts @@ -1,6 +1,6 @@ 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 { @@ -8,7 +8,8 @@ interface ForkTsCheckerWebpackPluginOptions { typescript?: TypeScriptWorkerOptions; formatter?: FormatterOptions; issue?: IssueOptions; - logger?: LoggerOptions; + logger?: Logger; + devServer?: boolean; } export { ForkTsCheckerWebpackPluginOptions }; diff --git a/test/unit/plugin.spec.ts b/test/unit/plugin.spec.ts index 1a857f8a..58e05fb0 100644 --- a/test/unit/plugin.spec.ts +++ b/test/unit/plugin.spec.ts @@ -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(); }); });