Skip to content

Commit 4baabbe

Browse files
committed
improved livereload
1 parent bc3f2a7 commit 4baabbe

File tree

4 files changed

+36
-23
lines changed

4 files changed

+36
-23
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ Go to the `v1` branch to see the changelog of Lume 1.
8080
- Parsing the escaped URLs in CSS files.
8181
- Improved the output CSS and JS code of components.
8282
- Components interoperability, specially between JSX vs text engines.
83+
- Improved reload after renaming or removing a folder.
8384

8485
[#660]: https://github.com/lumeland/lume/issues/660
8586
[#736]: https://github.com/lumeland/lume/issues/736

core/fs.ts

+5-9
Original file line numberDiff line numberDiff line change
@@ -93,15 +93,6 @@ export default class FS {
9393
entry.src = src;
9494
}
9595

96-
// New directory, walk it
97-
if (entry.type === "directory") {
98-
if (!exist) {
99-
this.#walkFs(entry);
100-
}
101-
102-
return;
103-
}
104-
10596
try {
10697
entry.getInfo();
10798
} catch (error) {
@@ -117,6 +108,11 @@ export default class FS {
117108
return exist;
118109
}
119110
}
111+
112+
// New directory, walk it
113+
if (entry.type === "directory" && !exist) {
114+
this.#walkFs(entry);
115+
}
120116
}
121117

122118
#isValid(path: string) {

core/site.ts

+14-6
Original file line numberDiff line numberDiff line change
@@ -628,12 +628,13 @@ export default class Site {
628628
}
629629

630630
// Remove pages or static files depending on this entry
631-
const pages = this.pages.filter((page) => page.src.entry === entry).map((
632-
page,
633-
) => page.outputPath);
634-
const files = this.files.filter((file) => file.src.entry === entry).map((
635-
file,
636-
) => file.outputPath);
631+
const pages = this.pages
632+
.filter((page) => pathBelongs(entry.path, page.src.entry?.path))
633+
.map((page) => page.outputPath);
634+
const files = this.files
635+
.filter((file) => pathBelongs(entry.path, file.src.entry?.path))
636+
.map((file) => file.outputPath);
637+
637638
await this.writer.removeFiles([...pages, ...files]);
638639
}
639640

@@ -1106,3 +1107,10 @@ export type SiteEventType = keyof SiteEventMap;
11061107

11071108
/** A generic Lume plugin */
11081109
export type Plugin = (site: Site) => void;
1110+
1111+
function pathBelongs(base: string, path?: string): boolean {
1112+
if (!path) {
1113+
return false;
1114+
}
1115+
return base === path || path?.startsWith(base + "/");
1116+
}

core/writer.ts

+16-8
Original file line numberDiff line numberDiff line change
@@ -176,19 +176,27 @@ export class FSWriter implements Writer {
176176
const outputPath = posix.join(this.dest, file);
177177
this.#outputs.delete(outputPath.toLowerCase());
178178
await Deno.remove(outputPath);
179+
179180
// Remove empty directories
180-
const dir = posix.dirname(outputPath);
181-
try {
182-
if (dir !== this.dest) {
183-
await Deno.remove(dir);
184-
}
185-
} catch {
186-
// Ignored
187-
}
181+
removeEmptyDirectory(outputPath, this.dest);
188182
} catch {
189183
// Ignored
190184
}
191185
},
192186
);
193187
}
194188
}
189+
190+
function removeEmptyDirectory(path: string, base: string) {
191+
const dir = posix.dirname(path);
192+
193+
try {
194+
if (dir !== base) {
195+
Deno.removeSync(dir);
196+
// Check if the parent directory is also empty
197+
removeEmptyDirectory(dir, base);
198+
}
199+
} catch {
200+
// Ignored
201+
}
202+
}

0 commit comments

Comments
 (0)