Skip to content

fix: better pug error message #448

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

Merged
merged 2 commits into from
Dec 21, 2021
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
2 changes: 2 additions & 0 deletions docs/preprocessing.md
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,8 @@ This is also necessary to pass callbacks:
button(on:click!="{(e) => doTheThing(e)}")
```

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'}"`.

**Spreading props:**

To spread props into a pug element, wrap the `{...object}` expression with quotes `"`.
Expand Down
19 changes: 16 additions & 3 deletions src/transformers/pug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,28 @@ const transformer: Transformer<Options.Pug> = async ({
};

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

let code: string;

try {
code = compiled();
} catch (e) {
// The error message does not have much context, add more of it
if (e instanceof Error) {
e.message = `[svelte-preprocess] Pug error while preprocessing ${filename}\n\n${e.message}`;
}

throw e;
}

return {
code: compiled(),
code,
dependencies: compiled.dependencies ?? [],
};
};
Expand Down