From 14b662fa25979ed60fbd11cd1b351fe73999a4f4 Mon Sep 17 00:00:00 2001 From: Timur Gamerov Date: Wed, 4 Dec 2024 19:08:22 -0500 Subject: [PATCH 01/11] .assetsignore file generation for Cloudflare deployment. It is corresponded to https://github.com/cloudflare/workers-sdk/pull/6640 --- packages/adapter-cloudflare/index.js | 5 ++++- packages/adapter-cloudflare/package.json | 1 + .../adapter-cloudflare/templates-experimental/.assetsignore | 4 ++++ 3 files changed, 9 insertions(+), 1 deletion(-) create mode 100644 packages/adapter-cloudflare/templates-experimental/.assetsignore diff --git a/packages/adapter-cloudflare/index.js b/packages/adapter-cloudflare/index.js index 0fe55898e569..06639dbf1a2f 100644 --- a/packages/adapter-cloudflare/index.js +++ b/packages/adapter-cloudflare/index.js @@ -1,4 +1,4 @@ -import { existsSync, writeFileSync } from 'node:fs'; +import { copyFileSync, existsSync, writeFileSync } from 'node:fs'; import * as path from 'node:path'; import { fileURLToPath } from 'node:url'; import * as esbuild from 'esbuild'; @@ -31,6 +31,7 @@ export default function (options = {}) { } const files = fileURLToPath(new URL('./files', import.meta.url).href); + const templates = fileURLToPath(new URL('./templates-experimental', import.meta.url).href); const dest = builder.getBuildDirectory('cloudflare'); const tmp = builder.getBuildDirectory('cloudflare-tmp'); @@ -74,6 +75,8 @@ export default function (options = {}) { }); } + copyFileSync(`${templates}/.assetsignore`, `${dest}/.assetsignore`); + builder.copy(`${files}/worker.js`, `${tmp}/_worker.js`, { replace: { SERVER: `${relativePath}/index.js`, diff --git a/packages/adapter-cloudflare/package.json b/packages/adapter-cloudflare/package.json index 4d49b21f4841..fce61d2d64fd 100644 --- a/packages/adapter-cloudflare/package.json +++ b/packages/adapter-cloudflare/package.json @@ -28,6 +28,7 @@ "types": "index.d.ts", "files": [ "files", + "templates-experimental", "index.js", "index.d.ts", "ambient.d.ts" diff --git a/packages/adapter-cloudflare/templates-experimental/.assetsignore b/packages/adapter-cloudflare/templates-experimental/.assetsignore new file mode 100644 index 000000000000..c2634cc3650a --- /dev/null +++ b/packages/adapter-cloudflare/templates-experimental/.assetsignore @@ -0,0 +1,4 @@ +_worker.js +_routes.json +_headers +_redirects From 32ccc2e0bc8aff600f65a3af11a88da89814b61c Mon Sep 17 00:00:00 2001 From: Timur Gamerov Date: Fri, 6 Dec 2024 19:18:27 -0500 Subject: [PATCH 02/11] move .assetsignore to files folder --- packages/adapter-cloudflare/.gitignore | 1 - .../{templates-experimental => files}/.assetsignore | 0 packages/adapter-cloudflare/files/.gitignore | 2 ++ packages/adapter-cloudflare/index.js | 3 +-- packages/adapter-cloudflare/package.json | 1 - 5 files changed, 3 insertions(+), 4 deletions(-) delete mode 100644 packages/adapter-cloudflare/.gitignore rename packages/adapter-cloudflare/{templates-experimental => files}/.assetsignore (100%) create mode 100644 packages/adapter-cloudflare/files/.gitignore diff --git a/packages/adapter-cloudflare/.gitignore b/packages/adapter-cloudflare/.gitignore deleted file mode 100644 index 2c8afebd8eda..000000000000 --- a/packages/adapter-cloudflare/.gitignore +++ /dev/null @@ -1 +0,0 @@ -/files diff --git a/packages/adapter-cloudflare/templates-experimental/.assetsignore b/packages/adapter-cloudflare/files/.assetsignore similarity index 100% rename from packages/adapter-cloudflare/templates-experimental/.assetsignore rename to packages/adapter-cloudflare/files/.assetsignore diff --git a/packages/adapter-cloudflare/files/.gitignore b/packages/adapter-cloudflare/files/.gitignore new file mode 100644 index 000000000000..e8dc87ab4c0d --- /dev/null +++ b/packages/adapter-cloudflare/files/.gitignore @@ -0,0 +1,2 @@ +* +!.assetsignore diff --git a/packages/adapter-cloudflare/index.js b/packages/adapter-cloudflare/index.js index 06639dbf1a2f..f007e1979727 100644 --- a/packages/adapter-cloudflare/index.js +++ b/packages/adapter-cloudflare/index.js @@ -31,7 +31,6 @@ export default function (options = {}) { } const files = fileURLToPath(new URL('./files', import.meta.url).href); - const templates = fileURLToPath(new URL('./templates-experimental', import.meta.url).href); const dest = builder.getBuildDirectory('cloudflare'); const tmp = builder.getBuildDirectory('cloudflare-tmp'); @@ -75,7 +74,7 @@ export default function (options = {}) { }); } - copyFileSync(`${templates}/.assetsignore`, `${dest}/.assetsignore`); + copyFileSync(`${files}/.assetsignore`, `${dest}/.assetsignore`); builder.copy(`${files}/worker.js`, `${tmp}/_worker.js`, { replace: { diff --git a/packages/adapter-cloudflare/package.json b/packages/adapter-cloudflare/package.json index fce61d2d64fd..4d49b21f4841 100644 --- a/packages/adapter-cloudflare/package.json +++ b/packages/adapter-cloudflare/package.json @@ -28,7 +28,6 @@ "types": "index.d.ts", "files": [ "files", - "templates-experimental", "index.js", "index.d.ts", "ambient.d.ts" From 0e9b1fcab9e76002fe6f07ed7cd937fa11b1c776 Mon Sep 17 00:00:00 2001 From: Timur Gamerov Date: Fri, 6 Dec 2024 19:36:30 -0500 Subject: [PATCH 03/11] add .assetsignore file generation for adapter-cloudflare-workers --- packages/adapter-cloudflare-workers/files/.assetsignore | 4 ++++ packages/adapter-cloudflare-workers/index.js | 4 +++- 2 files changed, 7 insertions(+), 1 deletion(-) create mode 100644 packages/adapter-cloudflare-workers/files/.assetsignore diff --git a/packages/adapter-cloudflare-workers/files/.assetsignore b/packages/adapter-cloudflare-workers/files/.assetsignore new file mode 100644 index 000000000000..c2634cc3650a --- /dev/null +++ b/packages/adapter-cloudflare-workers/files/.assetsignore @@ -0,0 +1,4 @@ +_worker.js +_routes.json +_headers +_redirects diff --git a/packages/adapter-cloudflare-workers/index.js b/packages/adapter-cloudflare-workers/index.js index 8a4f9252d1ac..282930f20b5e 100644 --- a/packages/adapter-cloudflare-workers/index.js +++ b/packages/adapter-cloudflare-workers/index.js @@ -1,4 +1,4 @@ -import { existsSync, readFileSync, writeFileSync } from 'node:fs'; +import { copyFileSync, existsSync, readFileSync, writeFileSync } from 'node:fs'; import { posix, dirname } from 'node:path'; import { execSync } from 'node:child_process'; import esbuild from 'esbuild'; @@ -143,6 +143,8 @@ export default function ({ config = 'wrangler.toml', platformProxy = {} } = {}) ); } + copyFileSync(`${files}/.assetsignore`, `${dirname(main)}/.assetsignore`); + builder.log.minor('Copying assets...'); const bucket_dir = `${site.bucket}${builder.config.kit.paths.base}`; builder.writeClient(bucket_dir); From ee1e4bbb821bf2171caa4c32a75716e4e0eb7a03 Mon Sep 17 00:00:00 2001 From: Timur Gamerov Date: Fri, 6 Dec 2024 19:48:14 -0500 Subject: [PATCH 04/11] changeset update --- .changeset/proud-taxis-admire.md | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 .changeset/proud-taxis-admire.md diff --git a/.changeset/proud-taxis-admire.md b/.changeset/proud-taxis-admire.md new file mode 100644 index 000000000000..c0cb0fa09310 --- /dev/null +++ b/.changeset/proud-taxis-admire.md @@ -0,0 +1,6 @@ +--- +'@sveltejs/adapter-cloudflare-workers': patch +'@sveltejs/adapter-cloudflare': patch +--- + +.assetsignore file generation for Cloudflare deployment. It is corresponded to https://github.com/cloudflare/workers-sdk/pull/6640 From 6892797299d8988faaca8e85d1a4a0d06cc0cc28 Mon Sep 17 00:00:00 2001 From: Timur Gamerov Date: Mon, 9 Dec 2024 16:57:19 -0500 Subject: [PATCH 05/11] move .gitignore per Ben McCann (benmccann) request --- packages/adapter-cloudflare/.gitignore | 2 ++ packages/adapter-cloudflare/files/.gitignore | 2 -- 2 files changed, 2 insertions(+), 2 deletions(-) create mode 100644 packages/adapter-cloudflare/.gitignore delete mode 100644 packages/adapter-cloudflare/files/.gitignore diff --git a/packages/adapter-cloudflare/.gitignore b/packages/adapter-cloudflare/.gitignore new file mode 100644 index 000000000000..cd59bbe51c88 --- /dev/null +++ b/packages/adapter-cloudflare/.gitignore @@ -0,0 +1,2 @@ +/files/* +!/files/.assetsignore diff --git a/packages/adapter-cloudflare/files/.gitignore b/packages/adapter-cloudflare/files/.gitignore deleted file mode 100644 index e8dc87ab4c0d..000000000000 --- a/packages/adapter-cloudflare/files/.gitignore +++ /dev/null @@ -1,2 +0,0 @@ -* -!.assetsignore From 9061fc5098fccbef81efbb068bbe254b46c61538 Mon Sep 17 00:00:00 2001 From: Tee Ming Date: Tue, 10 Dec 2024 09:56:56 +0800 Subject: [PATCH 06/11] Update .changeset/proud-taxis-admire.md --- .changeset/proud-taxis-admire.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.changeset/proud-taxis-admire.md b/.changeset/proud-taxis-admire.md index c0cb0fa09310..ab2de02a60aa 100644 --- a/.changeset/proud-taxis-admire.md +++ b/.changeset/proud-taxis-admire.md @@ -1,6 +1,6 @@ --- -'@sveltejs/adapter-cloudflare-workers': patch -'@sveltejs/adapter-cloudflare': patch +'@sveltejs/adapter-cloudflare-workers': minor +'@sveltejs/adapter-cloudflare': minor --- .assetsignore file generation for Cloudflare deployment. It is corresponded to https://github.com/cloudflare/workers-sdk/pull/6640 From 12599663f64151c1269f4bd6197a1da4d94b5277 Mon Sep 17 00:00:00 2001 From: methanoya <146410440+methanoya@users.noreply.github.com> Date: Tue, 10 Dec 2024 15:43:52 -0500 Subject: [PATCH 07/11] Update .changeset/proud-taxis-admire.md Co-authored-by: Tee Ming --- .changeset/proud-taxis-admire.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changeset/proud-taxis-admire.md b/.changeset/proud-taxis-admire.md index ab2de02a60aa..7db16440bcd8 100644 --- a/.changeset/proud-taxis-admire.md +++ b/.changeset/proud-taxis-admire.md @@ -3,4 +3,4 @@ '@sveltejs/adapter-cloudflare': minor --- -.assetsignore file generation for Cloudflare deployment. It is corresponded to https://github.com/cloudflare/workers-sdk/pull/6640 +feat: add `.assetsignore` file to avoid serving known server-only files to browser From ae984154e4bdeafdd8c9478fdd068316c7bae372 Mon Sep 17 00:00:00 2001 From: Timur Gamerov Date: Thu, 12 Dec 2024 09:10:15 -0500 Subject: [PATCH 08/11] Revert "add .assetsignore file generation for adapter-cloudflare-workers" This reverts commit 0e9b1fcab9e76002fe6f07ed7cd937fa11b1c776. --- packages/adapter-cloudflare-workers/files/.assetsignore | 4 ---- packages/adapter-cloudflare-workers/index.js | 4 +--- 2 files changed, 1 insertion(+), 7 deletions(-) delete mode 100644 packages/adapter-cloudflare-workers/files/.assetsignore diff --git a/packages/adapter-cloudflare-workers/files/.assetsignore b/packages/adapter-cloudflare-workers/files/.assetsignore deleted file mode 100644 index c2634cc3650a..000000000000 --- a/packages/adapter-cloudflare-workers/files/.assetsignore +++ /dev/null @@ -1,4 +0,0 @@ -_worker.js -_routes.json -_headers -_redirects diff --git a/packages/adapter-cloudflare-workers/index.js b/packages/adapter-cloudflare-workers/index.js index 282930f20b5e..8a4f9252d1ac 100644 --- a/packages/adapter-cloudflare-workers/index.js +++ b/packages/adapter-cloudflare-workers/index.js @@ -1,4 +1,4 @@ -import { copyFileSync, existsSync, readFileSync, writeFileSync } from 'node:fs'; +import { existsSync, readFileSync, writeFileSync } from 'node:fs'; import { posix, dirname } from 'node:path'; import { execSync } from 'node:child_process'; import esbuild from 'esbuild'; @@ -143,8 +143,6 @@ export default function ({ config = 'wrangler.toml', platformProxy = {} } = {}) ); } - copyFileSync(`${files}/.assetsignore`, `${dirname(main)}/.assetsignore`); - builder.log.minor('Copying assets...'); const bucket_dir = `${site.bucket}${builder.config.kit.paths.base}`; builder.writeClient(bucket_dir); From 38a0220490787a7ec20e96573bfbfb2f017e2b46 Mon Sep 17 00:00:00 2001 From: Timur Gamerov Date: Thu, 12 Dec 2024 10:18:53 -0500 Subject: [PATCH 09/11] merge user's .assetsignore with the generated one --- packages/adapter-cloudflare/index.js | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/packages/adapter-cloudflare/index.js b/packages/adapter-cloudflare/index.js index f007e1979727..5fb9e6755bfc 100644 --- a/packages/adapter-cloudflare/index.js +++ b/packages/adapter-cloudflare/index.js @@ -1,4 +1,4 @@ -import { copyFileSync, existsSync, writeFileSync } from 'node:fs'; +import { existsSync, readFileSync, writeFileSync } from 'node:fs'; import * as path from 'node:path'; import { fileURLToPath } from 'node:url'; import * as esbuild from 'esbuild'; @@ -74,8 +74,6 @@ export default function (options = {}) { }); } - copyFileSync(`${files}/.assetsignore`, `${dest}/.assetsignore`); - builder.copy(`${files}/worker.js`, `${tmp}/_worker.js`, { replace: { SERVER: `${relativePath}/index.js`, @@ -145,6 +143,19 @@ export default function (options = {}) { }` ); } + + const assetsignore = [`${dest}/.assetsignore`, `${files}/.assetsignore`].reduce( + (acc, file) => { + if (existsSync(file)) { + readFileSync(file, 'utf8') + .split('\n') + .forEach((line) => acc.add(line.trimEnd())); + } + return acc; + }, + new Set() + ); + writeFileSync(`${dest}/.assetsignore`, Array.from(assetsignore).sort().reverse().join('\n')); }, async emulate() { const proxy = await getPlatformProxy(options.platformProxy); From 896318cf2417a2d4c477a11e7eb6573abe9edf6d Mon Sep 17 00:00:00 2001 From: methanoya <146410440+methanoya@users.noreply.github.com> Date: Thu, 12 Dec 2024 15:28:08 -0500 Subject: [PATCH 10/11] Update proud-taxis-admire.md remove mention of the unaffected package --- .changeset/proud-taxis-admire.md | 1 - 1 file changed, 1 deletion(-) diff --git a/.changeset/proud-taxis-admire.md b/.changeset/proud-taxis-admire.md index 7db16440bcd8..fd763e3242c5 100644 --- a/.changeset/proud-taxis-admire.md +++ b/.changeset/proud-taxis-admire.md @@ -1,5 +1,4 @@ --- -'@sveltejs/adapter-cloudflare-workers': minor '@sveltejs/adapter-cloudflare': minor --- From 3b27f873e2cce5d9177abdf6408428ca5ff78681 Mon Sep 17 00:00:00 2001 From: Timur Gamerov Date: Thu, 12 Dec 2024 15:34:06 -0500 Subject: [PATCH 11/11] add ending newline to the generated .assetsignore --- packages/adapter-cloudflare/index.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/adapter-cloudflare/index.js b/packages/adapter-cloudflare/index.js index 5fb9e6755bfc..c084b4adbdfe 100644 --- a/packages/adapter-cloudflare/index.js +++ b/packages/adapter-cloudflare/index.js @@ -149,13 +149,14 @@ export default function (options = {}) { if (existsSync(file)) { readFileSync(file, 'utf8') .split('\n') - .forEach((line) => acc.add(line.trimEnd())); + .filter((line) => line.trim()) + .forEach((line) => acc.add(line)); } return acc; }, new Set() ); - writeFileSync(`${dest}/.assetsignore`, Array.from(assetsignore).sort().reverse().join('\n')); + writeFileSync(`${dest}/.assetsignore`, Array.from(assetsignore).sort().join('\n') + '\n'); }, async emulate() { const proxy = await getPlatformProxy(options.platformProxy);