-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
feat: generate .assetsignore
file for Cloudflare deployment
#13109
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 11 commits
14b662f
32ccc2e
0e9b1fc
ee1e4bb
6892797
9061fc5
1259966
ae98415
38a0220
896318c
3b27f87
99d2fd9
a1ec57a
728fb08
68bdb25
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@sveltejs/adapter-cloudflare': minor | ||
--- | ||
|
||
feat: add `.assetsignore` file to avoid serving known server-only files to browser | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
/files | ||
/files/* | ||
!/files/.assetsignore |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
_worker.js | ||
_routes.json | ||
_headers | ||
_redirects |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -1,4 +1,4 @@ | ||||||||||||||||||||||||||||||
import { 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'; | ||||||||||||||||||||||||||||||
|
@@ -143,6 +143,20 @@ export default function (options = {}) { | |||||||||||||||||||||||||||||
}` | ||||||||||||||||||||||||||||||
); | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
const assetsignore = [`${dest}/.assetsignore`, `${files}/.assetsignore`].reduce( | ||||||||||||||||||||||||||||||
(acc, file) => { | ||||||||||||||||||||||||||||||
if (existsSync(file)) { | ||||||||||||||||||||||||||||||
readFileSync(file, 'utf8') | ||||||||||||||||||||||||||||||
.split('\n') | ||||||||||||||||||||||||||||||
.filter((line) => line.trim()) | ||||||||||||||||||||||||||||||
.forEach((line) => acc.add(line)); | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
return acc; | ||||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||||
new Set() | ||||||||||||||||||||||||||||||
); | ||||||||||||||||||||||||||||||
writeFileSync(`${dest}/.assetsignore`, Array.from(assetsignore).sort().join('\n') + '\n'); | ||||||||||||||||||||||||||||||
|
const assetsignore = [`${dest}/.assetsignore`, `${files}/.assetsignore`].reduce( | |
(acc, file) => { | |
if (existsSync(file)) { | |
readFileSync(file, 'utf8') | |
.split('\n') | |
.filter((line) => line.trim()) | |
.forEach((line) => acc.add(line)); | |
} | |
return acc; | |
}, | |
new Set() | |
); | |
writeFileSync(`${dest}/.assetsignore`, Array.from(assetsignore).sort().join('\n') + '\n'); | |
writeFileSync(`${dest}/.assetsignore`, generate_assetsignore(), { flag: 'a' }); |
function generate_assetsignore() {
return `
_worker.js
_routes.json
_headers
_redirects
`.trimEnd();
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have simplified the code and added generate_assetsignore()
function to handle the .assetsignore
separately. It is not clear why keeping the source code intact (.gitignore
and files
) would take priority over avoiding the hardcoding. The .assetsignore
comes from another project cloudflare/workers-sdk
and it makes sense to keep it separated for readability and maintainability in case of further updates.
For now, the generate_assetsignore()
simply merges the user's .assetsignore
file and the original one into the final .assetsignore
. The proposed snipped above doesn't provide the merging, so cannot be used as is.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The proposed snipped above doesn't provide the merging, so cannot be used as is.
It does merge it because if the user has the file /static/.assetsignore
it would have been copied over to the destination and those entries would be appended to the end of the existing file or write to a new file if it doesn't already exist.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're right, I missed the first part of the update besides the snippet. It looks like my concerns are ignored, so I've updated the PR as suggested but added comments about the source of the .assetsignore
.
Uh oh!
There was an error while loading. Please reload this page.