Skip to content

Commit 494ee7b

Browse files
fix: Append to existing .assetsignore, not overwriting (#12628)
Co-authored-by: devin-ai-integration[bot] <158243242+devin-ai-integration[bot]@users.noreply.github.com>
1 parent b584456 commit 494ee7b

File tree

4 files changed

+49
-3
lines changed

4 files changed

+49
-3
lines changed

.changeset/cool-oranges-fail.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"@cloudflare/vite-plugin": patch
3+
---
4+
5+
Append Cloudflare defaults to existing `.assetsignore` files during build output
6+
7+
When a project includes a `PUBLIC_DIR/.assetsignore`, the plugin now preserves those rules and appends the required `wrangler.json` and `.dev.vars` entries instead of replacing the file content.

packages/vite-plugin-cloudflare/playground/assets/__tests__/assets.spec.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
1+
import * as fs from "node:fs";
2+
import * as path from "node:path";
13
import { test } from "vitest";
2-
import { getResponse, page, viteTestUrl } from "../../__test-utils__";
4+
import {
5+
getResponse,
6+
isBuild,
7+
page,
8+
rootDir,
9+
viteTestUrl,
10+
} from "../../__test-utils__";
311
import "./base-tests";
412

513
test("fetches transformed HTML asset", async ({ expect }) => {
@@ -25,3 +33,15 @@ test("fetches original HTML asset if requested directly", async ({
2533
const content = await page.textContent("h1");
2634
expect(content).toBe("Original content");
2735
});
36+
37+
test.runIf(isBuild)(
38+
"emits .assetsignore in client output directory merged with defaults",
39+
({ expect }) => {
40+
expect(
41+
fs.readFileSync(
42+
path.join(rootDir, "dist", "client", ".assetsignore"),
43+
"utf-8"
44+
)
45+
).toBe(`test-file.txt\n*.bak\nwrangler.json\n.dev.vars\n`);
46+
}
47+
);
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
test-file.txt
2+
*.bak

packages/vite-plugin-cloudflare/src/plugins/output-config.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import assert from "node:assert";
2+
import { existsSync, readFileSync } from "node:fs";
23
import * as path from "node:path";
34
import { MAIN_ENTRY_NAME } from "../cloudflare-environment";
45
import { assertIsNotPreview } from "../context";
@@ -86,12 +87,18 @@ export const outputConfigPlugin = createPlugin("output-config", (ctx) => {
8687
}
8788
}
8889
} else if (this.environment.name === "client") {
89-
const filesToAssetsIgnore = ["wrangler.json", ".dev.vars"];
90+
const filesToAssetsIgnore = ["wrangler.json", ".dev.vars"] as const;
91+
const existingAssetsIgnoreContent =
92+
ctx.resolvedViteConfig.publicDir.length > 0
93+
? readAssetsIgnoreFile(
94+
path.join(ctx.resolvedViteConfig.publicDir, ".assetsignore")
95+
)
96+
: "";
9097

9198
this.emitFile({
9299
type: "asset",
93100
fileName: ".assetsignore",
94-
source: `${filesToAssetsIgnore.join("\n")}\n`,
101+
source: `${existingAssetsIgnoreContent}${filesToAssetsIgnore.join("\n")}\n`,
95102
});
96103

97104
// For assets only projects the `wrangler.json` file is emitted in the client output directory
@@ -143,6 +150,16 @@ export const outputConfigPlugin = createPlugin("output-config", (ctx) => {
143150
};
144151
});
145152

153+
function readAssetsIgnoreFile(assetsIgnorePath: string): string {
154+
const content = existsSync(assetsIgnorePath)
155+
? readFileSync(assetsIgnorePath, "utf-8")
156+
: "";
157+
if (content.length === 0) {
158+
return "";
159+
}
160+
return content.at(-1) === "\n" ? content : `${content}\n`;
161+
}
162+
146163
function getAssetsDirectory(
147164
workerOutputDirectory: string,
148165
resolvedViteConfig: vite.ResolvedConfig

0 commit comments

Comments
 (0)