Skip to content
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

Support the new Logger API #94

Merged
merged 2 commits into from
Jan 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
## 1.0.0-beta.8

* Support custom importers for the new JS API.

* Support the `SassArgumentList` value type.

* Support the `SassFunction` value type.
* Fully support the new JS API:
* Support custom importers for the new JS API.
* Support custom loggers for the new JS API.
* Support the `SassArgumentList` value type.
* Support the `SassFunction` value type.

## 1.0.0-beta.7

Expand Down
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "sass-embedded",
"version": "1.0.0-dev",
"version": "1.0.0-beta.8",
"protocol-version": "1.0.0-beta.16",
"compiler-version": "1.0.0-beta.15",
"description": "Node.js library that communicates with Embedded Dart Sass using the Embedded Sass protocol",
Expand Down