-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
65 lines (46 loc) · 2.02 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
const { readdirSync, rmSync } = require('fs');
const chokidar = require('chokidar');
const { pageDependencies, renderPage } = require('./utils/renderPage')
const getComponents = require('./utils/getComponents')
const renderAllPages = ({ production = false }) => {
rmSync('./dist', { recursive: true, force: true });
const pageFileNames = readdirSync('./src/pages/');
const { components, componentDependencies, rerenderComponent } = getComponents({ production });
pageFileNames.forEach(fileName => {
const pageName = fileName.replace('.html', '');
renderPage({ components, pageName, production });
if (!production) {
const pageWatcher = chokidar.watch(`./src/pages/${fileName}`);
pageWatcher.on('change', () => renderPage({ components, pageName }));
pageWatcher.on('unlink', () => rmSync(`./dist/${fileName}`, { recursive: true, force: true }));
}
});
if (production) return;
const componentsWatcher = chokidar.watch('./src/components/**');
componentsWatcher.on('change', (path) => {
const componentsChanged = [];
const checkDependencies = (componentName) => {
const componentsDependingOnThis = [];
Object.entries(componentDependencies).forEach(([key, dependencies]) => {
if (dependencies.includes(componentName)) {
componentsDependingOnThis.push(key);
}
})
componentsChanged.push(componentName)
rerenderComponent(componentName);
if (componentsDependingOnThis?.length > 0) {
componentsDependingOnThis.forEach(dependency => checkDependencies(dependency));
}
}
const componentName = path
.replace('src/components/', '')
.replace('.html', '');
checkDependencies(componentName);
Object.entries(pageDependencies).forEach(([path, dependencies]) => {
if (componentsChanged.some(dependency => dependencies.includes(dependency))) {
renderPage({ components, pageName: path.replace('./src/pages/', '').replace('.html', '') });
}
})
});
}
module.exports = renderAllPages