forked from sveltejs/svelte-preprocess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpug.ts
133 lines (104 loc) · 2.59 KB
/
pug.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import pug from 'pug';
import type { Transformer, Options } from '../types';
// Mixins to use svelte template features
const GET_MIXINS = (indentationType: 'tab' | 'space') =>
`mixin if(condition)
%_| {#if !{condition}}
%_block
%_| {/if}
mixin else
%_| {:else}
%_block
mixin elseif(condition)
%_| {:else if !{condition}}
%_block
mixin key(expression)
%_| {#key !{expression}}
%_block
%_| {/key}
mixin each(loop)
%_| {#each !{loop}}
%_block
%_| {/each}
mixin await(promise)
%_| {#await !{promise}}
%_block
%_| {/await}
mixin then(answer)
%_| {:then !{answer}}
%_block
mixin catch(error)
%_| {:catch !{error}}
%_block
mixin html(expression)
%_| {@html !{expression}}
mixin const(expression)
%_| {@const !{expression}}
mixin debug(variables)
%_| {@debug !{variables}}`.replace(
/%_/g,
indentationType === 'tab' ? '\t' : ' ',
);
const transformer: Transformer<Options.Pug> = async ({
content,
filename,
options,
}) => {
const pugOptions = {
// needed so pug doesn't mirror boolean attributes
// and prop spreading expressions.
doctype: 'html',
compileDebug: false,
filename,
...options,
};
const spaces = guessIndentString(content);
const input = `${GET_MIXINS(spaces ? 'space' : 'tab')}\n${content}`;
const compiled = pug.compile(
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,
dependencies: compiled.dependencies ?? [],
};
};
// Sourced from `golden-fleece`
// https://github.com/Rich-Harris/golden-fleece/blob/f2446f331640f325e13609ed99b74b6a45e755c2/src/patch.ts#L302
function guessIndentString(str: string): number | undefined {
const lines = str.split('\n');
let tabs = 0;
let spaces = 0;
let minSpaces = 8;
lines.forEach((line) => {
const match = /^(?: +|\t+)/.exec(line);
if (!match) return;
const [whitespace] = match;
if (whitespace.length === line.length) return;
if (whitespace[0] === '\t') {
tabs += 1;
} else {
spaces += 1;
if (whitespace.length > 1 && whitespace.length < minSpaces) {
minSpaces = whitespace.length;
}
}
});
if (spaces > tabs) {
let result = '';
while (minSpaces--) result += ' ';
return result.length;
}
}
export { transformer };