Skip to content
Open
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
21 changes: 18 additions & 3 deletions lib/src/compiler/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,25 @@ export function handleLogEvent(
span: span!,
});
} else {
const url = event.span?.url;
if (url) {
const index = formatted.indexOf(url);
if (index !== -1) {
const replacement = utils.prettyUrl(url);
if (url !== replacement) {
formatted =
formatted.slice(0, index) +
replacement +
formatted.slice(index + url.length);
}
}
}
console.error(formatted);
}
} else {
let stack = event.stackTrace;
if (stack && options?.legacy) stack = removeLegacyImporter(stack);

if (options?.logger?.warn) {
const params: (
| {
Expand All @@ -192,14 +208,13 @@ export function handleLogEvent(
: {deprecation: false};
if (span) params.span = span;

const stack = event.stackTrace;
if (stack) {
params.stack = options?.legacy ? removeLegacyImporter(stack) : stack;
params.stack = stack;
}

options.logger.warn(message, params);
} else {
console.error(formatted);
console.error(utils.prettyFormatted(formatted, stack));
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion lib/src/exception.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
import * as proto from './vendor/embedded_sass_pb';
import {Exception as SassException, SourceSpan} from './vendor/sass';
import {deprotofySourceSpan} from './deprotofy-span';
import {prettyFormatted} from './utils';

export class Exception extends Error implements SassException {
readonly sassMessage: string;
readonly sassStack: string;
readonly span: SourceSpan;

constructor(failure: proto.OutboundMessage_CompileResponse_CompileFailure) {
super(failure.formatted);
super(prettyFormatted(failure.formatted, failure.stackTrace));

this.sassMessage = failure.message;
this.sassStack = failure.stackTrace;
Expand Down
59 changes: 59 additions & 0 deletions lib/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,65 @@ export function fileUrlToPathCrossPlatform(fileUrl: url.URL | string): string {
return /^\/[A-Za-z]:\//.test(path) ? path.substring(1) : path;
}

/**
* Returns a pretty URL similar to Dart's `path.prettyUri`.
*/
export function prettyUrl(url: string): string {
if (!url.startsWith('file:')) return url;

const absolutePath = fileUrlToPathCrossPlatform(url);
const relativePath = p.relative(process.cwd(), absolutePath);
return relativePath.split(p.sep).length > absolutePath.split(p.sep).length
? absolutePath
: relativePath;
}

/**
* Reformat URLs in formatted text from the embedded compiler.
*/
export function prettyFormatted(formatted: string, stack: string): string {
let longest = 0;

const frames = stack
.split('\n')
.filter(frame => frame !== '')
.map(frame => {
let [location, member] = frame.split(/ +/, 2);
let [url, lineColumn] = location.split(' ', 2);

url = prettyUrl(url);
location = lineColumn === undefined ? url : `${url} ${lineColumn}`;

if (location.length > longest) {
longest = location.length;
}
return {
frame,
location,
member,
};
});

let offset = formatted.length;

frames.reverse().forEach(({frame, location, member}) => {
const index = formatted.lastIndexOf(frame, offset);
if (index !== -1) {
offset = index;

const replacement = `${location.padEnd(longest)} ${member}`;
if (frame !== replacement) {
formatted =
formatted.slice(0, index) +
replacement +
formatted.slice(index + frame.length);
}
}
});

return formatted;
}

/** Returns `path` without an extension, if it had one. */
export function withoutExtension(path: string): string {
const extension = p.extname(path);
Expand Down
Loading