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

Commit

Permalink
feat: Use webpack-log for nicer logging (#568)
Browse files Browse the repository at this point in the history
  • Loading branch information
astorije authored and s-panferov committed Apr 6, 2018
1 parent 118f91e commit f7e77b7
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 28 deletions.
3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -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"
}
}
23 changes: 15 additions & 8 deletions src/checker/runtime.ts
Expand Up @@ -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,
Expand Down Expand Up @@ -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<ts.FileWatcherCallback> = new Map()
Expand Down Expand Up @@ -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 => {
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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])
Expand Down Expand Up @@ -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)
}
})
Expand Down
30 changes: 12 additions & 18 deletions src/instance.ts
Expand Up @@ -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'
Expand All @@ -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')
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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`)
}
}

Expand Down
15 changes: 14 additions & 1 deletion yarn.lock
Expand Up @@ -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"
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit f7e77b7

Please sign in to comment.