Skip to content

Commit a239e82

Browse files
authoredDec 21, 2021
fix: better pug error message (#448)
Closes #447 Also add note about a pug limitation using template literals
1 parent e29e51c commit a239e82

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed
 

‎docs/preprocessing.md

+2
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,8 @@ This is also necessary to pass callbacks:
316316
button(on:click!="{(e) => doTheThing(e)}")
317317
```
318318

319+
It is not possible to use template literals for attribute values. You can't write `` attr=`Hello ${value ? 'Foo' : 'Bar'}` ``, instead write `attr="Hello {value ? 'Foo' : 'Bar'}"`.
320+
319321
**Spreading props:**
320322

321323
To spread props into a pug element, wrap the `{...object}` expression with quotes `"`.

‎src/transformers/pug.ts

+16-3
Original file line numberDiff line numberDiff line change
@@ -65,15 +65,28 @@ const transformer: Transformer<Options.Pug> = async ({
6565
};
6666

6767
const { type: identationType } = detectIndent(content);
68-
const code = `${GET_MIXINS(identationType ?? 'space')}\n${content}`;
68+
const input = `${GET_MIXINS(identationType ?? 'space')}\n${content}`;
6969
const compiled = pug.compile(
70-
code,
70+
input,
7171
pugOptions,
7272
// @types/pug compile() returned value doesn't have `dependencies` prop
7373
) as pug.compileTemplate & { dependencies?: string[] };
7474

75+
let code: string;
76+
77+
try {
78+
code = compiled();
79+
} catch (e) {
80+
// The error message does not have much context, add more of it
81+
if (e instanceof Error) {
82+
e.message = `[svelte-preprocess] Pug error while preprocessing ${filename}\n\n${e.message}`;
83+
}
84+
85+
throw e;
86+
}
87+
7588
return {
76-
code: compiled(),
89+
code,
7790
dependencies: compiled.dependencies ?? [],
7891
};
7992
};

0 commit comments

Comments
 (0)
Please sign in to comment.