Skip to content

Commit d7c1618

Browse files
authored
fix: preprocess error reporting (#260)
* fix: include stack and filename in error reporting for svelte preprocess * chore: add changeset
1 parent d93c4de commit d7c1618

File tree

7 files changed

+36
-12
lines changed

7 files changed

+36
-12
lines changed

.changeset/pretty-hairs-press.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/vite-plugin-svelte': patch
3+
---
4+
5+
include stack and filename in error reporting for svelte preprocess errors

packages/vite-plugin-svelte/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ export function svelte(inlineOptions?: Partial<Options>): Plugin {
170170
try {
171171
compileData = await compileSvelte(svelteRequest, code, options);
172172
} catch (e) {
173-
throw toRollupError(e);
173+
throw toRollupError(e, options);
174174
}
175175
logCompilerWarnings(compileData.compiled.warnings, options);
176176
cache.update(compileData);

packages/vite-plugin-svelte/src/utils/compile.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,13 @@ const _createCompileSvelte = (makeHot: Function) =>
3737
let preprocessed;
3838

3939
if (options.preprocess) {
40-
preprocessed = await preprocess(code, options.preprocess, { filename });
40+
try {
41+
preprocessed = await preprocess(code, options.preprocess, { filename });
42+
} catch (e) {
43+
e.message = `Error while preprocessing ${filename}${e.message ? ` - ${e.message}` : ''}`;
44+
throw e;
45+
}
46+
4147
if (preprocessed.dependencies) dependencies.push(...preprocessed.dependencies);
4248
if (preprocessed.map) compileOptions.sourcemap = preprocessed.map;
4349
}

packages/vite-plugin-svelte/src/utils/error.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { RollupError } from 'rollup';
2-
import { Warning } from './options';
2+
import { ResolvedOptions, Warning } from './options';
33
import { buildExtendedLogMessage } from './log';
44
import { PartialMessage } from 'esbuild';
55

@@ -8,15 +8,15 @@ import { PartialMessage } from 'esbuild';
88
* @param error a svelte compiler error, which is a mix of Warning and an error
99
* @returns {RollupError} the converted error
1010
*/
11-
export function toRollupError(error: Warning & Error): RollupError {
12-
const { filename, frame, start, code, name } = error;
11+
export function toRollupError(error: Warning & Error, options: ResolvedOptions): RollupError {
12+
const { filename, frame, start, code, name, stack } = error;
1313
const rollupError: RollupError = {
1414
name, // needed otherwise sveltekit coalesce_to_error turns it into a string
1515
id: filename,
1616
message: buildExtendedLogMessage(error), // include filename:line:column so that it's clickable
1717
frame: formatFrameForVite(frame),
1818
code,
19-
stack: ''
19+
stack: options.isBuild || options.isDebug || !frame ? stack : ''
2020
};
2121
if (start) {
2222
rollupError.loc = {
@@ -33,8 +33,8 @@ export function toRollupError(error: Warning & Error): RollupError {
3333
* @param error a svelte compiler error, which is a mix of Warning and an error
3434
* @returns {PartialMessage} the converted error
3535
*/
36-
export function toESBuildError(error: Warning & Error): PartialMessage {
37-
const { filename, frame, start } = error;
36+
export function toESBuildError(error: Warning & Error, options: ResolvedOptions): PartialMessage {
37+
const { filename, frame, start, stack } = error;
3838
const partialMessage: PartialMessage = {
3939
text: buildExtendedLogMessage(error)
4040
};
@@ -46,6 +46,9 @@ export function toESBuildError(error: Warning & Error): PartialMessage {
4646
lineText: lineFromFrame(start.line, frame) // needed to get a meaningful error message on cli
4747
};
4848
}
49+
if (options.isBuild || options.isDebug || !frame) {
50+
partialMessage.detail = stack;
51+
}
4952
return partialMessage;
5053
}
5154

packages/vite-plugin-svelte/src/utils/esbuild.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export function esbuildSveltePlugin(options: ResolvedOptions): EsbuildPlugin {
2727
const contents = await compileSvelte(options, { filename, code });
2828
return { contents };
2929
} catch (e) {
30-
return { errors: [toESBuildError(e)] };
30+
return { errors: [toESBuildError(e, options)] };
3131
}
3232
});
3333
}
@@ -73,7 +73,12 @@ async function compileSvelte(
7373
let preprocessed;
7474

7575
if (options.preprocess) {
76-
preprocessed = await preprocess(code, options.preprocess, { filename });
76+
try {
77+
preprocessed = await preprocess(code, options.preprocess, { filename });
78+
} catch (e) {
79+
e.message = `Error while preprocessing ${filename}${e.message ? ` - ${e.message}` : ''}`;
80+
throw e;
81+
}
7782
if (preprocessed.map) compileOptions.sourcemap = preprocessed.map;
7883
}
7984

packages/vite-plugin-svelte/src/utils/log.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,10 @@ export function buildExtendedLogMessage(w: Warning) {
164164
parts.push(':', w.start.line, ':', w.start.column);
165165
}
166166
if (w.message) {
167-
parts.push(' ', w.message);
167+
if (parts.length > 0) {
168+
parts.push(' ');
169+
}
170+
parts.push(w.message);
168171
}
169172
return parts.join('');
170173
}

packages/vite-plugin-svelte/src/utils/options.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,8 @@ export async function preResolveOptions(
8282
// extras
8383
root: viteConfigWithResolvedRoot.root!,
8484
isBuild: viteEnv.command === 'build',
85-
isServe: viteEnv.command === 'serve'
85+
isServe: viteEnv.command === 'serve',
86+
isDebug: process.env.DEBUG != null
8687
};
8788
// configFile of svelteConfig contains the absolute path it was loaded from,
8889
// prefer it over the possibly relative inline path
@@ -491,6 +492,7 @@ export interface PreResolvedOptions extends Options {
491492
root: string;
492493
isBuild: boolean;
493494
isServe: boolean;
495+
isDebug: boolean;
494496
}
495497

496498
export interface ResolvedOptions extends PreResolvedOptions {

0 commit comments

Comments
 (0)