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

fix: preprocess error reporting #260

Merged
merged 2 commits into from
Jan 27, 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
5 changes: 5 additions & 0 deletions .changeset/pretty-hairs-press.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/vite-plugin-svelte': patch
---

include stack and filename in error reporting for svelte preprocess errors
2 changes: 1 addition & 1 deletion packages/vite-plugin-svelte/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ export function svelte(inlineOptions?: Partial<Options>): Plugin {
try {
compileData = await compileSvelte(svelteRequest, code, options);
} catch (e) {
throw toRollupError(e);
throw toRollupError(e, options);
}
logCompilerWarnings(compileData.compiled.warnings, options);
cache.update(compileData);
Expand Down
8 changes: 7 additions & 1 deletion packages/vite-plugin-svelte/src/utils/compile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,13 @@ const _createCompileSvelte = (makeHot: Function) =>
let preprocessed;

if (options.preprocess) {
preprocessed = await preprocess(code, options.preprocess, { filename });
try {
preprocessed = await preprocess(code, options.preprocess, { filename });
} catch (e) {
e.message = `Error while preprocessing ${filename}${e.message ? ` - ${e.message}` : ''}`;
throw e;
}

if (preprocessed.dependencies) dependencies.push(...preprocessed.dependencies);
if (preprocessed.map) compileOptions.sourcemap = preprocessed.map;
}
Expand Down
15 changes: 9 additions & 6 deletions packages/vite-plugin-svelte/src/utils/error.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { RollupError } from 'rollup';
import { Warning } from './options';
import { ResolvedOptions, Warning } from './options';
import { buildExtendedLogMessage } from './log';
import { PartialMessage } from 'esbuild';

Expand All @@ -8,15 +8,15 @@ import { PartialMessage } from 'esbuild';
* @param error a svelte compiler error, which is a mix of Warning and an error
* @returns {RollupError} the converted error
*/
export function toRollupError(error: Warning & Error): RollupError {
const { filename, frame, start, code, name } = error;
export function toRollupError(error: Warning & Error, options: ResolvedOptions): RollupError {
const { filename, frame, start, code, name, stack } = error;
const rollupError: RollupError = {
name, // needed otherwise sveltekit coalesce_to_error turns it into a string
id: filename,
message: buildExtendedLogMessage(error), // include filename:line:column so that it's clickable
frame: formatFrameForVite(frame),
code,
stack: ''
stack: options.isBuild || options.isDebug || !frame ? stack : ''
};
if (start) {
rollupError.loc = {
Expand All @@ -33,8 +33,8 @@ export function toRollupError(error: Warning & Error): RollupError {
* @param error a svelte compiler error, which is a mix of Warning and an error
* @returns {PartialMessage} the converted error
*/
export function toESBuildError(error: Warning & Error): PartialMessage {
const { filename, frame, start } = error;
export function toESBuildError(error: Warning & Error, options: ResolvedOptions): PartialMessage {
const { filename, frame, start, stack } = error;
const partialMessage: PartialMessage = {
text: buildExtendedLogMessage(error)
};
Expand All @@ -46,6 +46,9 @@ export function toESBuildError(error: Warning & Error): PartialMessage {
lineText: lineFromFrame(start.line, frame) // needed to get a meaningful error message on cli
};
}
if (options.isBuild || options.isDebug || !frame) {
partialMessage.detail = stack;
}
return partialMessage;
}

Expand Down
9 changes: 7 additions & 2 deletions packages/vite-plugin-svelte/src/utils/esbuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export function esbuildSveltePlugin(options: ResolvedOptions): EsbuildPlugin {
const contents = await compileSvelte(options, { filename, code });
return { contents };
} catch (e) {
return { errors: [toESBuildError(e)] };
return { errors: [toESBuildError(e, options)] };
}
});
}
Expand Down Expand Up @@ -73,7 +73,12 @@ async function compileSvelte(
let preprocessed;

if (options.preprocess) {
preprocessed = await preprocess(code, options.preprocess, { filename });
try {
preprocessed = await preprocess(code, options.preprocess, { filename });
} catch (e) {
e.message = `Error while preprocessing ${filename}${e.message ? ` - ${e.message}` : ''}`;
throw e;
}
if (preprocessed.map) compileOptions.sourcemap = preprocessed.map;
}

Expand Down
5 changes: 4 additions & 1 deletion packages/vite-plugin-svelte/src/utils/log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,10 @@ export function buildExtendedLogMessage(w: Warning) {
parts.push(':', w.start.line, ':', w.start.column);
}
if (w.message) {
parts.push(' ', w.message);
if (parts.length > 0) {
parts.push(' ');
}
parts.push(w.message);
}
return parts.join('');
}
4 changes: 3 additions & 1 deletion packages/vite-plugin-svelte/src/utils/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ export async function preResolveOptions(
// extras
root: viteConfigWithResolvedRoot.root!,
isBuild: viteEnv.command === 'build',
isServe: viteEnv.command === 'serve'
isServe: viteEnv.command === 'serve',
isDebug: process.env.DEBUG != null
};
// configFile of svelteConfig contains the absolute path it was loaded from,
// prefer it over the possibly relative inline path
Expand Down Expand Up @@ -491,6 +492,7 @@ export interface PreResolvedOptions extends Options {
root: string;
isBuild: boolean;
isServe: boolean;
isDebug: boolean;
}

export interface ResolvedOptions extends PreResolvedOptions {
Expand Down