diff --git a/CHANGELOG.md b/CHANGELOG.md index 86d4fca3..19e8f693 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/lib/index.ts b/lib/index.ts index 1b33828e..b15e6a02 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -25,3 +25,7 @@ export { export {render} from './src/legacy'; export const info = `sass-embedded\t${pkg.version}`; + +export const Logger = { + silent: {warn() {}, debug() {}}, +}; diff --git a/lib/src/compile.ts b/lib/src/compile.ts index cdbc8426..1d88408c 100644 --- a/lib/src/compile.ts +++ b/lib/src/compile.ts @@ -8,7 +8,7 @@ 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'; @@ -16,6 +16,7 @@ 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, @@ -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( @@ -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; @@ -246,6 +247,40 @@ function createDispatcher( ); } +/** 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`. * diff --git a/package.json b/package.json index 727f4c15..c200c1ec 100644 --- a/package.json +++ b/package.json @@ -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",