Skip to content

Commit

Permalink
feat: allow ignoring public assets with ignore options (#945)
Browse files Browse the repository at this point in the history
Co-authored-by: Pooya Parsa <pooya@pi0.io>
  • Loading branch information
motea927 and pi0 authored Jul 14, 2023
1 parent a3bf7e3 commit 5febdd3
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 7 deletions.
25 changes: 19 additions & 6 deletions src/build.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { promises as fsp } from "node:fs";
import { existsSync, promises as fsp } from "node:fs";
import { relative, resolve, join, dirname, isAbsolute } from "pathe";
import { resolveAlias } from "pathe/utils";
import * as rollup from "rollup";
Expand All @@ -11,6 +11,7 @@ import type { TSConfig } from "pkg-types";
import type { RollupError } from "rollup";
import type { OnResolveResult, PartialMessage } from "esbuild";
import type { RouterMethod } from "h3";
import { globby } from "globby";
import { generateFSTree } from "./utils/tree";
import { getRollupConfig, RollupConfig } from "./rollup/config";
import { prettyPath, writeFile, isDirectory } from "./utils";
Expand Down Expand Up @@ -40,11 +41,23 @@ export async function copyPublicAssets(nitro: Nitro) {
return;
}
for (const asset of nitro.options.publicAssets) {
if (await isDirectory(asset.dir)) {
await fse.copy(
asset.dir,
join(nitro.options.output.publicDir, asset.baseURL!),
{ overwrite: false }
const srcDir = asset.dir;
const dstDir = join(nitro.options.output.publicDir, asset.baseURL!);
if (await isDirectory(srcDir)) {
const publicAssets = await globby("**", {
cwd: srcDir,
absolute: false,
dot: true,
ignore: nitro.options.ignore,
});
await Promise.all(
publicAssets.map(async (file) => {
const src = join(srcDir, file);
const dst = join(dstDir, file);
if (!existsSync(dst)) {
await fsp.cp(src, dst);
}
})
);
}
}
Expand Down
2 changes: 1 addition & 1 deletion test/fixture/nitro.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export default defineNitroConfig({
dir: "files",
},
],
ignore: ["api/**/_*", "middleware/_ignored.ts", "routes/_*.ts"],
ignore: ["api/**/_*", "middleware/_ignored.ts", "routes/_*.ts", "_*.txt"],
appConfig: {
"nitro-config": true,
dynamic: "initial",
Expand Down
1 change: 1 addition & 0 deletions test/fixture/public/_ignored.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This file should be ignored!
13 changes: 13 additions & 0 deletions test/tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -400,5 +400,18 @@ export function testNitro(
expect((await callHandler({ url: "/api/_ignored" })).status).toBe(404);
expect((await callHandler({ url: "/_ignored" })).status).toBe(404);
});

it.skipIf(
[
"nitro-dev",
"cloudflare",
"cloudflare-pages",
"cloudflare-module",
"vercel-edge",
].includes(ctx.preset)
)("public files should be ignored", async () => {
expect((await callHandler({ url: "/_ignored.txt" })).status).toBe(404);
expect((await callHandler({ url: "/favicon.ico" })).status).toBe(200);
});
});
}

0 comments on commit 5febdd3

Please sign in to comment.