Skip to content

Commit

Permalink
Support the new Logger API
Browse files Browse the repository at this point in the history
  • Loading branch information
nex3 committed Dec 30, 2021
1 parent 9fdd946 commit 5aae76b
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 3 deletions.
4 changes: 4 additions & 0 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,7 @@ export {
export {render} from './src/legacy';

export const info = `sass-embedded\t${pkg.version}`;

export const Logger = {
silent: {warn() {}, debug() {}},
};
41 changes: 38 additions & 3 deletions lib/src/compile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ import * as supportsColor from 'supports-color';
import * as proto from './vendor/embedded-protocol/embedded_sass_pb';
import * as utils from './utils';
import {AsyncEmbeddedCompiler} from './async-compiler';
import {CompileResult, Options, StringOptions} from './vendor/sass';
import {CompileResult, Options, SourceSpan, StringOptions} from './vendor/sass';
import {Dispatcher, DispatcherHandlers} from './dispatcher';
import {Exception} from './exception';
import {FunctionRegistry} from './function-registry';
import {ImporterRegistry} from './importer-registry';
import {MessageTransformer} from './message-transformer';
import {PacketTransformer} from './packet-transformer';
import {SyncEmbeddedCompiler} from './sync-compiler';
import {deprotofySourceSpan} from './deprotofy-span';

export function compile(
path: string,
Expand Down Expand Up @@ -153,7 +154,7 @@ async function compileRequestAsync(
}
);

// TODO(awjin): Subscribe logger to dispatcher's log events.
dispatcher.logEvents$.subscribe(event => handleLogEvent(options, event));

return handleCompileResponse(
await new Promise<proto.OutboundMessage.CompileResponse>(
Expand Down Expand Up @@ -198,7 +199,7 @@ function compileRequestSync(
}
);

// TODO(awjin): Subscribe logger to dispatcher's log events.
dispatcher.logEvents$.subscribe(event => handleLogEvent(options, event));

let error: unknown;
let response: proto.OutboundMessage.CompileResponse | undefined;
Expand Down Expand Up @@ -246,6 +247,40 @@ function createDispatcher<sync extends 'sync' | 'async'>(
);
}

/** Handles a log event according to `options`. */
function handleLogEvent(
options: Options<'sync' | 'async'> | undefined,
event: proto.OutboundMessage.LogEvent
): void {
if (event.getType() === proto.LogEventType.DEBUG) {
if (options?.logger?.debug) {
options.logger.debug(event.getMessage(), {
span: deprotofySourceSpan(event.getSpan()!),
});
} else {
console.error(event.getFormatted());
}
} else {
if (options?.logger?.warn) {
const params: {deprecation: boolean; span?: SourceSpan; stack?: string} =
{
deprecation:
event.getType() === proto.LogEventType.DEPRECATION_WARNING,
};

const spanProto = event.getSpan();
if (spanProto) params.span = deprotofySourceSpan(spanProto);

const stack = event.getStackTrace();
if (stack) params.stack = stack;

options.logger.warn(event.getMessage(), params);
} else {
console.error(event.getFormatted());
}
}
}

/**
* Converts a `CompileResponse` into a `CompileResult`.
*
Expand Down

0 comments on commit 5aae76b

Please sign in to comment.