Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle path negations when scanning public assets #2250

Merged
merged 6 commits into from Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 19 additions & 8 deletions src/build.ts
Expand Up @@ -48,6 +48,9 @@ async function prepareDir(dir: string) {
await fse.emptyDir(dir);
}

const NEGATION_RE = /^(!?)(.*)$/;
const PARENT_DIR_GLOB_RE = /!?\.\.\//;

export async function copyPublicAssets(nitro: Nitro) {
if (nitro.options.noPublicDir) {
return;
Expand All @@ -56,17 +59,25 @@ export async function copyPublicAssets(nitro: Nitro) {
const srcDir = asset.dir;
const dstDir = join(nitro.options.output.publicDir, asset.baseURL!);
if (await isDirectory(srcDir)) {
const publicAssets = await globby("**", {
const includePatterns = [
"**",
...nitro.options.ignore.map((p) => {
const [_, negation, pattern] = p.match(NEGATION_RE);
return (
// Convert ignore to include patterns
(negation ? "" : "!") +
// Make non-glob patterns relative to publicAssetDir
(pattern.startsWith("*")
? pattern
: relative(srcDir, resolve(nitro.options.srcDir, pattern)))
);
}),
].filter((p) => !PARENT_DIR_GLOB_RE.test(p));

const publicAssets = await globby(includePatterns, {
cwd: srcDir,
absolute: false,
dot: true,
ignore: nitro.options.ignore
.map((p) =>
p.startsWith("*") || p.startsWith("!*")
? p
: relative(srcDir, resolve(nitro.options.srcDir, p))
)
.filter((p) => !p.startsWith("../")),
});
await Promise.all(
publicAssets.map(async (file) => {
Expand Down
8 changes: 7 additions & 1 deletion test/fixture/nitro.config.ts
Expand Up @@ -32,7 +32,13 @@ export default defineNitroConfig({
dir: "files",
},
],
ignore: ["api/**/_*", "middleware/_ignored.ts", "routes/_*.ts", "**/_*.txt"],
ignore: [
"api/**/_*",
"middleware/_ignored.ts",
"routes/_*.ts",
"**/_*.txt",
"!**/_unignored.txt",
],
appConfig: {
"nitro-config": true,
dynamic: "initial",
Expand Down
1 change: 1 addition & 0 deletions test/fixture/public/_unignored.txt
@@ -0,0 +1 @@
This file should not be ignored!
1 change: 1 addition & 0 deletions test/presets/cloudflare-pages.test.ts
Expand Up @@ -40,6 +40,7 @@ describe.skipIf(isWindows)("nitro:preset:cloudflare-pages", async () => {
"exclude": [
"/blog/static/*",
"/build/*",
"/_unignored.txt",
"/favicon.ico",
"/json-string",
"/api/hello",
Expand Down
9 changes: 9 additions & 0 deletions test/tests.ts
Expand Up @@ -539,6 +539,15 @@ export function testNitro(
expect((await callHandler({ url: "/favicon.ico" })).status).toBe(200);
}
);

it.skipIf(ctx.isWorker || ctx.isDev)(
"public filesΒ can be un-ignored with patterns",
async () => {
expect((await callHandler({ url: "/_unignored.txt" })).status).toBe(
200
);
}
);
});

describe("headers", () => {
Expand Down