/
PugTransformer.js
47 lines (39 loc) 路 1.07 KB
/
PugTransformer.js
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
// @flow
import path from 'path';
import {Transformer} from '@parcel/plugin';
import pug from 'pug';
export default (new Transformer({
async loadConfig({config}) {
let configFile = await config.getConfig([
'.pugrc',
'.pugrc.js',
'.pugrc.cjs',
'pug.config.js',
'pug.config.cjs',
]);
if (configFile) {
let isJavascript = path.extname(configFile.filePath).endsWith('js');
if (isJavascript) {
config.invalidateOnStartup();
}
return configFile.contents;
}
},
async transform({asset, config}) {
const pugConfig = config ?? {};
const content = await asset.getCode();
const render = pug.compile(content, {
compileDebug: false,
basedir: path.dirname(asset.filePath),
filename: asset.filePath,
...pugConfig,
pretty: pugConfig.pretty || false,
});
for (let filePath of render.dependencies) {
await asset.invalidateOnFileChange(filePath);
}
asset.type = 'html';
asset.setCode(render(pugConfig.locals));
return [asset];
},
}): Transformer);