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

Commit f7e77b7

Browse files
astorijes-panferov
authored andcommitted
feat: Use webpack-log for nicer logging (#568)
1 parent 118f91e commit f7e77b7

File tree

4 files changed

+43
-28
lines changed

4 files changed

+43
-28
lines changed

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
"husky": "^0.14.3",
7070
"typescript": "^2.7.2",
7171
"webpack": "^4.1.0",
72-
"webpack-cli": "^2.0.10"
72+
"webpack-cli": "^2.0.10",
73+
"webpack-log": "^1.2.0"
7374
}
7475
}

src/checker/runtime.ts

+15-8
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import * as ts from 'typescript'
22
import * as path from 'path'
33
import * as micromatch from 'micromatch'
44
import chalk from 'chalk'
5+
import * as weblog from 'webpack-log'
56
import { findResultFor, toUnix, unorderedRemoveItem } from '../helpers'
67
import {
78
Req,
@@ -89,6 +90,7 @@ function createChecker(receive: (cb: (msg: Req) => void) => void, send: (msg: Re
8990
let instanceName: string
9091
let context: string
9192
let rootFilesChanged = false
93+
let log = weblog({ name: 'atl' })
9294

9395
let filesRegex: RegExp
9496
const watchedFiles: WatchCallbacks<ts.FileWatcherCallback> = new Map()
@@ -306,14 +308,21 @@ function createChecker(receive: (cb: (msg: Req) => void) => void, send: (msg: Re
306308
context = payload.context
307309
filesRegex = compilerOptions.allowJs ? TS_AND_JS_FILES : TS_FILES
308310

311+
if (loaderConfig.debug) {
312+
log = weblog({ name: 'atl', level: 'debug' })
313+
}
314+
309315
instanceName = loaderConfig.instance || 'at-loader'
310316

311317
compilerConfig.fileNames.forEach(fileName => ensureFile(fileName))
312318
watch = createWatch()
313319

314-
if (loaderConfig.debug) {
315-
console.log(`[${instanceName}] @DEBUG Initial files`, Object.keys(files))
316-
}
320+
log.debug(
321+
'Initial files:',
322+
Object.keys(files)
323+
.map(file => chalk.cyan(file))
324+
.join(', ')
325+
)
317326

318327
if (loaderConfig.ignoreDiagnostics) {
319328
loaderConfig.ignoreDiagnostics.forEach(diag => {
@@ -469,7 +478,7 @@ function createChecker(receive: (cb: (msg: Req) => void) => void, send: (msg: Re
469478
let silent = !!loaderConfig.silent
470479

471480
if (!silent) {
472-
console.log(chalk.cyan(`\n[${instanceName}] Checking started in a separate process...`))
481+
log.info(`Checking started in a separate process...`)
473482
}
474483

475484
const program = getProgram()
@@ -526,9 +535,7 @@ function createChecker(receive: (cb: (msg: Req) => void) => void, send: (msg: Re
526535
allDiagnostics.push(...program.getSemanticDiagnostics(file))
527536
})
528537

529-
if (loaderConfig.debug) {
530-
console.log(`[${instanceName}] @DEBUG Typechecked files`, program.getSourceFiles())
531-
}
538+
log.debug(`Typechecked files:`, program.getSourceFiles())
532539

533540
const processedDiagnostics = allDiagnostics
534541
.filter(diag => !ignoreDiagnostics[diag.code])
@@ -609,7 +616,7 @@ function createChecker(receive: (cb: (msg: Req) => void) => void, send: (msg: Re
609616
break
610617
}
611618
} catch (e) {
612-
console.error(`[${instanceName}]: Child process failed to process the request: `, e)
619+
log.error(`Child process failed to process the request:`, e)
613620
replyErr(req.seq, null)
614621
}
615622
})

src/instance.ts

+12-18
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import * as fs from 'fs'
22
import * as path from 'path'
33
import * as _ from 'lodash'
44
import * as ts from 'typescript'
5+
import * as weblog from 'webpack-log'
56
import { toUnix } from './helpers'
67
import { Checker } from './checker'
78
import { CompilerInfo, LoaderConfig, TsConfig } from './interfaces'
@@ -12,6 +13,8 @@ import { Compiler } from 'webpack'
1213

1314
import chalk from 'chalk'
1415

16+
const log = weblog({ name: 'atl' })
17+
1518
let pkg = require('../package.json')
1619
let mkdirp = require('mkdirp')
1720
let enhancedResolve = require('enhanced-resolve')
@@ -115,12 +118,12 @@ export function ensureInstance(
115118
applyDefaults(configFilePath, compilerConfig, loaderConfig, context)
116119

117120
if (!loaderConfig.silent) {
121+
const tscVersion = compilerInfo.compilerVersion
122+
const tscPath = compilerInfo.compilerPath
123+
log.info(`Using typescript@${chalk.bold(tscVersion)} from ${chalk.bold(tscPath)}`)
124+
118125
const sync = watching === WatchMode.Enabled ? ' (in a forked process)' : ''
119-
console.log(
120-
`\n[${instanceName}] Using typescript@${compilerInfo.compilerVersion} from ${
121-
compilerInfo.compilerPath
122-
} and ` + `"tsconfig.json" from ${configFilePath}${sync}.\n`
123-
)
126+
log.info(`Using ${chalk.bold('tsconfig.json')} from ${chalk.bold(configFilePath)}${sync}`)
124127
}
125128

126129
let babelImpl = setupBabel(loaderConfig, context)
@@ -477,21 +480,12 @@ function setupAfterCompile(compiler, instanceName, forkChecker = false) {
477480
: instance.checker.getDiagnostics().then(diags => {
478481
if (!silent) {
479482
if (diags.length) {
480-
console.error(
481-
chalk.red(
482-
`\n[${instanceName}] Checking finished with ${
483-
diags.length
484-
} errors`
485-
)
483+
log.error(
484+
chalk.red(`Checking finished with ${diags.length} errors`)
486485
)
487486
} else {
488-
let timeEnd = +new Date()
489-
console.log(
490-
chalk.green(
491-
`\n[${instanceName}] Ok, ${(timeEnd - timeStart) /
492-
1000} sec.`
493-
)
494-
)
487+
let totalTime = (+new Date() - timeStart).toString()
488+
log.info(`Time: ${chalk.bold(totalTime)}ms`)
495489
}
496490
}
497491

yarn.lock

+14-1
Original file line numberDiff line numberDiff line change
@@ -3835,6 +3835,10 @@ log-update@^1.0.2:
38353835
ansi-escapes "^1.0.0"
38363836
cli-cursor "^1.0.2"
38373837

3838+
loglevelnext@^1.0.1:
3839+
version "1.0.3"
3840+
resolved "https://registry.yarnpkg.com/loglevelnext/-/loglevelnext-1.0.3.tgz#0f69277e73bbbf2cd61b94d82313216bf87ac66e"
3841+
38383842
longest@^1.0.1:
38393843
version "1.0.1"
38403844
resolved "https://registry.yarnpkg.com/longest/-/longest-1.0.1.tgz#30a0b2da38f73770e8294a0d22e6625ed77d0097"
@@ -5454,7 +5458,7 @@ [email protected]:
54545458
version "1.1.0"
54555459
resolved "https://registry.yarnpkg.com/staged-git-files/-/staged-git-files-1.1.0.tgz#1a9bb131c1885601023c7aaddd3d54c22142c526"
54565460

5457-
standard-version@^4.2.0:
5461+
standard-version@^4.3.0:
54585462
version "4.3.0"
54595463
resolved "https://registry.yarnpkg.com/standard-version/-/standard-version-4.3.0.tgz#41006cfee4eeab7c0ff3a47eecaa4c7506ed2e3f"
54605464
dependencies:
@@ -6099,6 +6103,15 @@ webpack-cli@^2.0.10:
60996103
yeoman-environment "^2.0.0"
61006104
yeoman-generator "github:ev1stensberg/generator#Feature-getArgument"
61016105

6106+
webpack-log@^1.2.0:
6107+
version "1.2.0"
6108+
resolved "https://registry.yarnpkg.com/webpack-log/-/webpack-log-1.2.0.tgz#a4b34cda6b22b518dbb0ab32e567962d5c72a43d"
6109+
dependencies:
6110+
chalk "^2.1.0"
6111+
log-symbols "^2.1.0"
6112+
loglevelnext "^1.0.1"
6113+
uuid "^3.1.0"
6114+
61026115
webpack-sources@^1.0.1:
61036116
version "1.0.1"
61046117
resolved "https://registry.yarnpkg.com/webpack-sources/-/webpack-sources-1.0.1.tgz#c7356436a4d13123be2e2426a05d1dad9cbe65cf"

0 commit comments

Comments
 (0)