diff --git a/package.json b/package.json index 0145fcc..24449f9 100644 --- a/package.json +++ b/package.json @@ -69,6 +69,7 @@ "husky": "^0.14.3", "typescript": "^2.7.2", "webpack": "^4.1.0", - "webpack-cli": "^2.0.10" + "webpack-cli": "^2.0.10", + "webpack-log": "^1.2.0" } } diff --git a/src/checker/runtime.ts b/src/checker/runtime.ts index dc2bbeb..006f57a 100644 --- a/src/checker/runtime.ts +++ b/src/checker/runtime.ts @@ -2,6 +2,7 @@ import * as ts from 'typescript' import * as path from 'path' import * as micromatch from 'micromatch' import chalk from 'chalk' +import * as weblog from 'webpack-log' import { findResultFor, toUnix, unorderedRemoveItem } from '../helpers' import { Req, @@ -89,6 +90,7 @@ function createChecker(receive: (cb: (msg: Req) => void) => void, send: (msg: Re let instanceName: string let context: string let rootFilesChanged = false + let log = weblog({ name: 'atl' }) let filesRegex: RegExp const watchedFiles: WatchCallbacks = new Map() @@ -306,14 +308,21 @@ function createChecker(receive: (cb: (msg: Req) => void) => void, send: (msg: Re context = payload.context filesRegex = compilerOptions.allowJs ? TS_AND_JS_FILES : TS_FILES + if (loaderConfig.debug) { + log = weblog({ name: 'atl', level: 'debug' }) + } + instanceName = loaderConfig.instance || 'at-loader' compilerConfig.fileNames.forEach(fileName => ensureFile(fileName)) watch = createWatch() - if (loaderConfig.debug) { - console.log(`[${instanceName}] @DEBUG Initial files`, Object.keys(files)) - } + log.debug( + 'Initial files:', + Object.keys(files) + .map(file => chalk.cyan(file)) + .join(', ') + ) if (loaderConfig.ignoreDiagnostics) { loaderConfig.ignoreDiagnostics.forEach(diag => { @@ -469,7 +478,7 @@ function createChecker(receive: (cb: (msg: Req) => void) => void, send: (msg: Re let silent = !!loaderConfig.silent if (!silent) { - console.log(chalk.cyan(`\n[${instanceName}] Checking started in a separate process...`)) + log.info(`Checking started in a separate process...`) } const program = getProgram() @@ -526,9 +535,7 @@ function createChecker(receive: (cb: (msg: Req) => void) => void, send: (msg: Re allDiagnostics.push(...program.getSemanticDiagnostics(file)) }) - if (loaderConfig.debug) { - console.log(`[${instanceName}] @DEBUG Typechecked files`, program.getSourceFiles()) - } + log.debug(`Typechecked files:`, program.getSourceFiles()) const processedDiagnostics = allDiagnostics .filter(diag => !ignoreDiagnostics[diag.code]) @@ -609,7 +616,7 @@ function createChecker(receive: (cb: (msg: Req) => void) => void, send: (msg: Re break } } catch (e) { - console.error(`[${instanceName}]: Child process failed to process the request: `, e) + log.error(`Child process failed to process the request:`, e) replyErr(req.seq, null) } }) diff --git a/src/instance.ts b/src/instance.ts index ee25d39..adc2a36 100644 --- a/src/instance.ts +++ b/src/instance.ts @@ -2,6 +2,7 @@ import * as fs from 'fs' import * as path from 'path' import * as _ from 'lodash' import * as ts from 'typescript' +import * as weblog from 'webpack-log' import { toUnix } from './helpers' import { Checker } from './checker' import { CompilerInfo, LoaderConfig, TsConfig } from './interfaces' @@ -12,6 +13,8 @@ import { Compiler } from 'webpack' import chalk from 'chalk' +const log = weblog({ name: 'atl' }) + let pkg = require('../package.json') let mkdirp = require('mkdirp') let enhancedResolve = require('enhanced-resolve') @@ -115,12 +118,12 @@ export function ensureInstance( applyDefaults(configFilePath, compilerConfig, loaderConfig, context) if (!loaderConfig.silent) { + const tscVersion = compilerInfo.compilerVersion + const tscPath = compilerInfo.compilerPath + log.info(`Using typescript@${chalk.bold(tscVersion)} from ${chalk.bold(tscPath)}`) + const sync = watching === WatchMode.Enabled ? ' (in a forked process)' : '' - console.log( - `\n[${instanceName}] Using typescript@${compilerInfo.compilerVersion} from ${ - compilerInfo.compilerPath - } and ` + `"tsconfig.json" from ${configFilePath}${sync}.\n` - ) + log.info(`Using ${chalk.bold('tsconfig.json')} from ${chalk.bold(configFilePath)}${sync}`) } let babelImpl = setupBabel(loaderConfig, context) @@ -477,21 +480,12 @@ function setupAfterCompile(compiler, instanceName, forkChecker = false) { : instance.checker.getDiagnostics().then(diags => { if (!silent) { if (diags.length) { - console.error( - chalk.red( - `\n[${instanceName}] Checking finished with ${ - diags.length - } errors` - ) + log.error( + chalk.red(`Checking finished with ${diags.length} errors`) ) } else { - let timeEnd = +new Date() - console.log( - chalk.green( - `\n[${instanceName}] Ok, ${(timeEnd - timeStart) / - 1000} sec.` - ) - ) + let totalTime = (+new Date() - timeStart).toString() + log.info(`Time: ${chalk.bold(totalTime)}ms`) } } diff --git a/yarn.lock b/yarn.lock index 8ff49f3..ec8f1f3 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3835,6 +3835,10 @@ log-update@^1.0.2: ansi-escapes "^1.0.0" cli-cursor "^1.0.2" +loglevelnext@^1.0.1: + version "1.0.3" + resolved "https://registry.yarnpkg.com/loglevelnext/-/loglevelnext-1.0.3.tgz#0f69277e73bbbf2cd61b94d82313216bf87ac66e" + longest@^1.0.1: version "1.0.1" resolved "https://registry.yarnpkg.com/longest/-/longest-1.0.1.tgz#30a0b2da38f73770e8294a0d22e6625ed77d0097" @@ -5454,7 +5458,7 @@ staged-git-files@1.1.0: version "1.1.0" resolved "https://registry.yarnpkg.com/staged-git-files/-/staged-git-files-1.1.0.tgz#1a9bb131c1885601023c7aaddd3d54c22142c526" -standard-version@^4.2.0: +standard-version@^4.3.0: version "4.3.0" resolved "https://registry.yarnpkg.com/standard-version/-/standard-version-4.3.0.tgz#41006cfee4eeab7c0ff3a47eecaa4c7506ed2e3f" dependencies: @@ -6099,6 +6103,15 @@ webpack-cli@^2.0.10: yeoman-environment "^2.0.0" yeoman-generator "github:ev1stensberg/generator#Feature-getArgument" +webpack-log@^1.2.0: + version "1.2.0" + resolved "https://registry.yarnpkg.com/webpack-log/-/webpack-log-1.2.0.tgz#a4b34cda6b22b518dbb0ab32e567962d5c72a43d" + dependencies: + chalk "^2.1.0" + log-symbols "^2.1.0" + loglevelnext "^1.0.1" + uuid "^3.1.0" + webpack-sources@^1.0.1: version "1.0.1" resolved "https://registry.yarnpkg.com/webpack-sources/-/webpack-sources-1.0.1.tgz#c7356436a4d13123be2e2426a05d1dad9cbe65cf"