-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
51 lines (41 loc) · 1.42 KB
/
index.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
48
49
50
51
const pug = require("pug");
const path = require("path");
module.exports = function plugin(snowpackConfig, pluginOptions) {
const extendsMaps = new Map();
return {
name: "@marlonmarcello/snowpack-plugin-pug",
resolve: {
input: [".pug"],
output: [".html"],
},
onChange({ filePath }) {
const listOfDeps = extendsMaps.get(filePath);
if (!listOfDeps) return;
// when a file update, if it's on the list of dependencies
// mark all its children as changed
for (let dep of listOfDeps) this.markChanged(dep);
},
load: ({ filePath }) => {
if (!filePath) return;
if (path.basename(filePath).startsWith("_")) return;
const { data, ...pugOptions } = pluginOptions;
const fn = pug.compileFile(filePath, pugOptions || {});
const deps = fn.dependencies;
// If a file `extends` another it will show as dependency
// in that case if the depency updates, we need to update this too
// this creates a map of dependencies and its children
if (deps && deps.length > 0) {
for (let dep of deps) {
const listOfDeps = extendsMaps.get(dep);
if (!listOfDeps) extendsMaps.set(dep, [filePath]);
else if (!listOfDeps.includes(filePath))
extendsMaps.set(dep, listOfDeps.concat([filePath]));
}
}
const html = fn(data || {});
return {
".html": html,
};
},
};
};