Skip to content

Commit

Permalink
fix: better pug error message (#448)
Browse files Browse the repository at this point in the history
Closes #447
Also add note about a pug limitation using template literals
  • Loading branch information
dummdidumm committed Dec 21, 2021
1 parent e29e51c commit a239e82
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
2 changes: 2 additions & 0 deletions docs/preprocessing.md
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
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

0 comments on commit a239e82

Please sign in to comment.