-
-
Notifications
You must be signed in to change notification settings - Fork 433
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
#249 Add explicit info logger to avoid writing info logs to stderr #313
Changes from 3 commits
0995e45
11d9455
5165114
c045ec5
51f8c68
e546515
79ae1ed
5ccd4fc
6c337d5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,7 +15,8 @@ var Console = require('console').Console; | |
var semver = require('semver') | ||
require('colors'); | ||
|
||
const console = new Console(process.stderr); | ||
const stderrConsole = new Console(process.stderr); | ||
const stdoutConsole = new Console(process.stdout); | ||
|
||
var pushArray = function(arr, toPush) { | ||
Array.prototype.splice.apply(arr, [0, 0].concat(toPush)); | ||
|
@@ -25,8 +26,15 @@ function hasOwnProperty(obj, property) { | |
return Object.prototype.hasOwnProperty.call(obj, property) | ||
} | ||
|
||
enum LogLevel { | ||
INFO = 1, | ||
WARN = 2, | ||
ERROR = 3 | ||
} | ||
|
||
interface LoaderOptions { | ||
silent: boolean; | ||
logLevel: string; | ||
instance: string; | ||
compiler: string; | ||
configFileName: string; | ||
|
@@ -148,12 +156,30 @@ function findConfigFile(compiler: typeof typescript, searchPath: string, configF | |
// `instance` property. | ||
function ensureTypeScriptInstance(loaderOptions: LoaderOptions, loader: any): { instance?: TSInstance, error?: WebpackError } { | ||
|
||
function log(...messages: string[]): void { | ||
function log(console:any, messages: string[]): void { | ||
if (!loaderOptions.silent) { | ||
console.log.apply(console, messages); | ||
} | ||
} | ||
|
||
function logInfo(...messages: string[]): void { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To avoid using |
||
if (LogLevel[loaderOptions.logLevel.toUpperCase()] <= LogLevel.INFO) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Rather than doing |
||
log(stdoutConsole, messages); | ||
} | ||
} | ||
|
||
function logError(...messages: string[]): void { | ||
if (LogLevel[loaderOptions.logLevel.toUpperCase()] <= LogLevel.ERROR) { | ||
log(stderrConsole, messages); | ||
} | ||
} | ||
|
||
function logWarning(...messages: string[]): void { | ||
if (LogLevel[loaderOptions.logLevel.toUpperCase()] <= LogLevel.WARN) { | ||
log(stderrConsole, messages); | ||
} | ||
} | ||
|
||
if (hasOwnProperty(instances, loaderOptions.instance)) { | ||
return { instance: instances[loaderOptions.instance] }; | ||
} | ||
|
@@ -180,11 +206,11 @@ function ensureTypeScriptInstance(loaderOptions: LoaderOptions, loader: any): { | |
compilerCompatible = true; | ||
} | ||
else { | ||
log(`${motd}. This version is incompatible with ts-loader. Please upgrade to the latest version of TypeScript.`.red); | ||
logError(`${motd}. This version is incompatible with ts-loader. Please upgrade to the latest version of TypeScript.`.red); | ||
} | ||
} | ||
else { | ||
log(`${motd}. This version may or may not be compatible with ts-loader.`.yellow); | ||
logWarning(`${motd}. This version may or may not be compatible with ts-loader.`.yellow); | ||
} | ||
|
||
var files = <TSFiles>{}; | ||
|
@@ -209,8 +235,8 @@ function ensureTypeScriptInstance(loaderOptions: LoaderOptions, loader: any): { | |
error?: typescript.Diagnostic; | ||
}; | ||
if (configFilePath) { | ||
if (compilerCompatible) log(`${motd} and ${configFilePath}`.green) | ||
else log(`ts-loader: Using config file at ${configFilePath}`.green) | ||
if (compilerCompatible) logInfo(`${motd} and ${configFilePath}`.green) | ||
else logInfo(`ts-loader: Using config file at ${configFilePath}`.green) | ||
|
||
// HACK: relies on the fact that passing an extra argument won't break | ||
// the old API that has a single parameter | ||
|
@@ -225,7 +251,7 @@ function ensureTypeScriptInstance(loaderOptions: LoaderOptions, loader: any): { | |
} | ||
} | ||
else { | ||
if (compilerCompatible) log(motd.green) | ||
if (compilerCompatible) logInfo(motd.green) | ||
|
||
configFile = { | ||
config: { | ||
|
@@ -538,6 +564,7 @@ function loader(contents) { | |
|
||
var options = objectAssign<LoaderOptions>({}, { | ||
silent: false, | ||
logLevel: 'info', | ||
instance: 'default', | ||
compiler: 'typescript', | ||
configFileName: 'tsconfig.json', | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we specify details about
logInfoToStdOut
beforelogLevel
in the documentation? Also, instead of:could we say: