diff --git a/MIGRATION.md b/MIGRATION.md index a251e691..cbb86eb5 100644 --- a/MIGRATION.md +++ b/MIGRATION.md @@ -68,6 +68,7 @@ - Use the lib `dom` and `dom.iterable` types instead of `deno-dom`. - Removed `lume/core/utils.ts` and moved all utilities to different files under `/lume/core/utils/` folder. +- Allow to copy files/directories inside ignored directories #520 ## `search` Plugin diff --git a/core/source.ts b/core/source.ts index e32deddf..0d648190 100644 --- a/core/source.ts +++ b/core/source.ts @@ -245,32 +245,26 @@ export default class Source { if (this.staticPaths.has(entry.path)) { const { dest, dirOnly } = this.staticPaths.get(entry.path)!; - if (entry.type === "file") { - if (dirOnly) { - continue; - } - staticFiles.push({ - entry, - outputPath: getOutputPath(entry, path, dest), - }); - continue; - } - - staticFiles.push(...this.#getStaticFiles( - entry, - typeof dest === "string" ? dest : posix.join(path, entry.name), - typeof dest === "function" ? dest : undefined, - )); + staticFiles.push(...this.#getStaticFiles(path, entry, dest, dirOnly)); continue; } - // Ignore .filename and _filename - if (entry.name.startsWith(".") || entry.name.startsWith("_")) { - continue; - } - - // Check if the file should be ignored - if (this.ignored.has(entry.path)) { + // Check if the entry should be ignored + if ( + entry.name.startsWith(".") || entry.name.startsWith("_") || + this.ignored.has(entry.path) + ) { + for (const [staticSrc, { dest, dirOnly }] of this.staticPaths) { + if (staticSrc.startsWith(entry.path)) { + const staticEntry = this.fs.entries.get(staticSrc)!; + const staticPath = posix.dirname( + posix.join(path, staticEntry.path.slice(entry.path.length)), + ); + staticFiles.push( + ...this.#getStaticFiles(staticPath, staticEntry, dest, dirOnly), + ); + } + } continue; } @@ -288,14 +282,13 @@ export default class Source { const dest = this.copyRemainingFiles(entry.path); if (dest) { - staticFiles.push({ - entry, - outputPath: getOutputPath( - entry, + staticFiles.push( + ...this.#getStaticFiles( path, + entry, typeof dest === "string" ? dest : undefined, ), - }); + ); } } continue; @@ -303,14 +296,13 @@ export default class Source { // The file is a static file if (format.copy) { - staticFiles.push({ - entry, - outputPath: getOutputPath( - entry, + staticFiles.push( + ...this.#getStaticFiles( path, + entry, typeof format.copy === "function" ? format.copy : undefined, ), - }); + ); continue; } @@ -404,7 +396,7 @@ export default class Source { } /** Scan the static files in a directory */ - *#getStaticFiles( + *#scanStaticFiles( dirEntry: Entry, destPath: string, destFn?: (file: string) => string, @@ -429,7 +421,7 @@ export default class Source { } if (entry.type === "directory") { - yield* this.#getStaticFiles( + yield* this.#scanStaticFiles( entry, posix.join(destPath, entry.name), destFn, @@ -437,6 +429,29 @@ export default class Source { } } } + + #getStaticFiles( + path: string, + entry: Entry, + dest: string | ((path: string) => string) | undefined, + dirOnly = false, + ): StaticFile[] { + if (entry.type === "file") { + if (!dirOnly) { + return [{ + entry, + outputPath: getOutputPath(entry, path, dest), + }]; + } + return []; + } + + return Array.from(this.#scanStaticFiles( + entry, + typeof dest === "string" ? dest : posix.join(path, entry.name), + typeof dest === "function" ? dest : undefined, + )); + } } /** diff --git a/tests/__snapshots__/static_files.test.ts.snap b/tests/__snapshots__/static_files.test.ts.snap index 3529599c..8c6a7f19 100644 --- a/tests/__snapshots__/static_files.test.ts.snap +++ b/tests/__snapshots__/static_files.test.ts.snap @@ -121,6 +121,9 @@ snapshot[`Copy static files 1`] = ` src: [ "/", "/_headers", + "/_static", + "/_static/inner", + "/_static/inner/yes.txt", "/four.no", "/one.yes", "/other", @@ -170,6 +173,11 @@ snapshot[`Copy static files 2`] = ` flags: [], outputPath: "/_headers", }, + { + entry: "/_static/inner/yes.txt", + flags: [], + outputPath: "/inner/yes.txt", + }, { entry: "/other/one", flags: [], diff --git a/tests/assets/static_files/_static/inner/yes.txt b/tests/assets/static_files/_static/inner/yes.txt new file mode 100644 index 00000000..396a0ba2 --- /dev/null +++ b/tests/assets/static_files/_static/inner/yes.txt @@ -0,0 +1 @@ +yes \ No newline at end of file diff --git a/tests/static_files.test.ts b/tests/static_files.test.ts index cebec113..23aa2770 100644 --- a/tests/static_files.test.ts +++ b/tests/static_files.test.ts @@ -27,6 +27,9 @@ Deno.test("Copy static files", async (t) => { // not copied because of the trailing slash site.copy("three.no/"); + // Copy a directory inside a ignored directory + site.copy("_static/inner", "inner"); + await build(site); await assertSiteSnapshot(t, site); });