From 2bd1ae25bd3af52908e12088c28028ef38f118c2 Mon Sep 17 00:00:00 2001
From: Rich Harris <richard.a.harris@gmail.com>
Date: Fri, 4 Mar 2022 11:57:26 -0500
Subject: [PATCH] prevent Vite from copying static assets if directory is
 called public (#4214)

* prevent Vite from copying static assets if directory is called public - fixes #587

* add test
---
 .changeset/brave-toys-joke.md                            | 5 +++++
 packages/kit/src/core/build/build_client.js              | 5 ++++-
 packages/kit/test/prerendering/options/public/robots.txt | 3 +++
 packages/kit/test/prerendering/options/svelte.config.js  | 4 ++++
 packages/kit/test/prerendering/options/test/test.js      | 4 ++++
 5 files changed, 20 insertions(+), 1 deletion(-)
 create mode 100644 .changeset/brave-toys-joke.md
 create mode 100644 packages/kit/test/prerendering/options/public/robots.txt

diff --git a/.changeset/brave-toys-joke.md b/.changeset/brave-toys-joke.md
new file mode 100644
index 000000000000..fd9557ebb256
--- /dev/null
+++ b/.changeset/brave-toys-joke.md
@@ -0,0 +1,5 @@
+---
+'@sveltejs/kit': patch
+---
+
+Prevent Vite from copying static assets if directory is called "public"
diff --git a/packages/kit/src/core/build/build_client.js b/packages/kit/src/core/build/build_client.js
index 735b57b87164..3f0cfaf60c3d 100644
--- a/packages/kit/src/core/build/build_client.js
+++ b/packages/kit/src/core/build/build_client.js
@@ -89,7 +89,10 @@ export async function build_client({
 					hydratable: !!config.kit.browser.hydrate
 				}
 			})
-		]
+		],
+		// prevent Vite copying the contents of `config.kit.files.assets`,
+		// if it happens to be 'public' instead of 'static'
+		publicDir: false
 	});
 
 	print_config_conflicts(conflicts, 'kit.vite.', 'build_client');
diff --git a/packages/kit/test/prerendering/options/public/robots.txt b/packages/kit/test/prerendering/options/public/robots.txt
new file mode 100644
index 000000000000..5537f0739799
--- /dev/null
+++ b/packages/kit/test/prerendering/options/public/robots.txt
@@ -0,0 +1,3 @@
+# https://www.robotstxt.org/robotstxt.html
+User-agent: *
+Disallow:
\ No newline at end of file
diff --git a/packages/kit/test/prerendering/options/svelte.config.js b/packages/kit/test/prerendering/options/svelte.config.js
index d84e1d47641d..e4e0d0d73bc7 100644
--- a/packages/kit/test/prerendering/options/svelte.config.js
+++ b/packages/kit/test/prerendering/options/svelte.config.js
@@ -12,6 +12,10 @@ const config = {
 			}
 		},
 
+		files: {
+			assets: 'public'
+		},
+
 		paths: {
 			base: '/path-base',
 			assets: 'https://cdn.example.com/stuff'
diff --git a/packages/kit/test/prerendering/options/test/test.js b/packages/kit/test/prerendering/options/test/test.js
index d38c68002250..0d8e2df63ad3 100644
--- a/packages/kit/test/prerendering/options/test/test.js
+++ b/packages/kit/test/prerendering/options/test/test.js
@@ -27,4 +27,8 @@ test('adds CSP headers via meta tag', () => {
 	);
 });
 
+test('does not copy `public` into `_app`', () => {
+	assert.ok(!fs.existsSync(`${build}/_app/robots.txt`));
+});
+
 test.run();