Skip to content

Commit

Permalink
fix: handle path negations when scanning public assets (#2250)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe authored Mar 13, 2024
1 parent 7e13a51 commit 0b4b5c2
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 9 deletions.
27 changes: 19 additions & 8 deletions src/build.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This file should not be ignored!
1 change: 1 addition & 0 deletions test/presets/cloudflare-pages.test.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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

0 comments on commit 0b4b5c2

Please sign in to comment.