Skip to content

Commit 4340c42

Browse files
authored
Always emit a .assetsignore file in the client output directory (#12358)
1 parent 4c4d5a5 commit 4340c42

File tree

5 files changed

+61
-30
lines changed

5 files changed

+61
-30
lines changed

.changeset/grumpy-nails-care.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
"@cloudflare/vite-plugin": patch
3+
---
4+
5+
Always emit a `.assetsignore` file in the client output directory.
6+
7+
Previously, we would emit a `.assetsignore` file in the client output directory only if the client output included a `wrangler.json` file.
8+
We now always emit it, which prevents a `wrangler.json` file being deployed as an asset if it is copied into this directory by mistake.

packages/vite-plugin-cloudflare/playground/spa-with-api/__tests__/custom-output-directories/spa-with-api.spec.ts

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,16 @@ import { isBuild, rootDir, WAIT_FOR_OPTIONS } from "../../../__test-utils__";
55
import "../base-tests";
66

77
describe.runIf(isBuild)("output directories", () => {
8-
test(
9-
"creates the correct output directories",
10-
async () => {
11-
await vi.waitFor(() => {
12-
expect(
13-
fs.existsSync(
14-
path.join(rootDir, "custom-root-output-directory", "worker")
15-
)
16-
).toBe(true);
17-
expect(
18-
fs.existsSync(path.join(rootDir, "custom-client-output-directory"))
19-
).toBe(true);
20-
});
21-
},
22-
WAIT_FOR_OPTIONS
23-
);
8+
test("creates the correct output directories", async () => {
9+
await vi.waitFor(() => {
10+
expect(
11+
fs.existsSync(
12+
path.join(rootDir, "custom-root-output-directory", "worker")
13+
)
14+
).toBe(true);
15+
expect(
16+
fs.existsSync(path.join(rootDir, "custom-client-output-directory"))
17+
).toBe(true);
18+
}, WAIT_FOR_OPTIONS);
19+
});
2420
});

packages/vite-plugin-cloudflare/playground/spa-with-api/__tests__/spa-with-api.spec.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1+
import * as fs from "node:fs";
2+
import * as path from "node:path";
13
import { expect, test } from "vitest";
24
import {
35
getResponse,
46
getTextResponse,
57
isBuild,
68
page,
9+
rootDir,
710
viteTestUrl,
811
} from "../../__test-utils__";
912
import "./base-tests";
@@ -41,3 +44,15 @@ test.runIf(!isBuild)(
4144
expect(text).toBe(`Some file content.\n`);
4245
}
4346
);
47+
48+
test.runIf(isBuild)(
49+
"emits .assetsignore file in client output directory",
50+
() => {
51+
expect(
52+
fs.readFileSync(
53+
path.join(rootDir, "dist", "client", ".assetsignore"),
54+
"utf-8"
55+
)
56+
).toBe(`wrangler.json\n.dev.vars\n`);
57+
}
58+
);

packages/vite-plugin-cloudflare/playground/static-mpa/__tests__/static-mpa.spec.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1+
import * as fs from "node:fs";
2+
import * as path from "node:path";
13
import { describe, expect, test } from "vitest";
24
import {
35
getResponse,
46
isBuild,
57
page,
8+
rootDir,
69
serverLogs,
710
viteTestUrl,
811
} from "../../__test-utils__";
@@ -93,3 +96,12 @@ describe.runIf(isBuild)("_redirects", () => {
9396
expect(content).toBe("Home");
9497
});
9598
});
99+
100+
test.runIf(isBuild)(
101+
"emits .assetsignore file in client output directory",
102+
() => {
103+
expect(
104+
fs.readFileSync(path.join(rootDir, "dist", ".assetsignore"), "utf-8")
105+
).toBe(`wrangler.json\n.dev.vars\n`);
106+
}
107+
);

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

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -77,27 +77,27 @@ export const outputConfigPlugin = createPlugin("output-config", (ctx) => {
7777
});
7878
}
7979
}
80-
} else if (
81-
ctx.resolvedPluginConfig.type === "assets-only" &&
82-
this.environment.name === "client"
83-
) {
84-
const inputAssetsOnlyConfig = ctx.resolvedPluginConfig.config;
85-
86-
outputConfig = {
87-
...inputAssetsOnlyConfig,
88-
assets: {
89-
...inputAssetsOnlyConfig.assets,
90-
directory: ".",
91-
},
92-
};
93-
80+
} else if (this.environment.name === "client") {
9481
const filesToAssetsIgnore = ["wrangler.json", ".dev.vars"];
9582

9683
this.emitFile({
9784
type: "asset",
9885
fileName: ".assetsignore",
9986
source: `${filesToAssetsIgnore.join("\n")}\n`,
10087
});
88+
89+
// For assets only projects the `wrangler.json` file is emitted in the client output directory
90+
if (ctx.resolvedPluginConfig.type === "assets-only") {
91+
const inputAssetsOnlyConfig = ctx.resolvedPluginConfig.config;
92+
93+
outputConfig = {
94+
...inputAssetsOnlyConfig,
95+
assets: {
96+
...inputAssetsOnlyConfig.assets,
97+
directory: ".",
98+
},
99+
};
100+
}
101101
}
102102

103103
if (!outputConfig) {

0 commit comments

Comments
 (0)