Skip to content

Commit 50d9072

Browse files
authored
fix: correctly resolve paths provided through Wrangler config on Windows (#13671)
* fix fixes #13667 This PR uses the non-posix relative() method so that it correctly resolves when used with Windows paths that may be provided through the Wrangler configuration. It also converts the path to a POSIX path before embedding them into the worker script since backslashes get interpreted as escape characters and are lost in the process. Also includes a minor fix where if the user had pages_build_output_dir set in their wrangler configuration, the adapter wasn't writing the worker to that directory (Cloudflare Pages expects the worker and assets to be in the same directory).
1 parent 7fd7bcb commit 50d9072

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

.changeset/bright-houses-kick.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/adapter-cloudflare': patch
3+
---
4+
5+
fix: correctly write the worker to the `pages_build_output_dir` path if set in the Wrangler configuration path

.changeset/gentle-parrots-clap.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/adapter-cloudflare': patch
3+
---
4+
5+
fix: correctly resolve paths provided by the Wrangler config on Windows

packages/adapter-cloudflare/index.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ export default function (options = {}) {
3737
if (building_for_cloudflare_pages) {
3838
if (wrangler_config.pages_build_output_dir) {
3939
dest = wrangler_config.pages_build_output_dir;
40+
worker_dest = `${dest}/_worker.js`;
4041
}
4142
} else {
4243
if (wrangler_config.main) {
@@ -92,8 +93,11 @@ export default function (options = {}) {
9293
);
9394
builder.copy(`${files}/worker.js`, worker_dest, {
9495
replace: {
95-
SERVER: `${path.posix.relative(worker_dest_dir, builder.getServerDirectory())}/index.js`,
96-
MANIFEST: `${path.posix.relative(worker_dest_dir, tmp)}/manifest.js`,
96+
// the paths returned by the Wrangler config might be Windows paths,
97+
// so we need to convert them to POSIX paths or else the backslashes
98+
// will be interpreted as escape characters and create an incorrect import path
99+
SERVER: `${posixify(path.relative(worker_dest_dir, builder.getServerDirectory()))}/index.js`,
100+
MANIFEST: `${posixify(path.relative(worker_dest_dir, tmp))}/manifest.js`,
97101
ASSETS: assets_binding
98102
}
99103
});
@@ -309,3 +313,8 @@ function is_building_for_cloudflare_pages(wrangler_config) {
309313
!wrangler_config.assets
310314
);
311315
}
316+
317+
/** @param {string} str */
318+
function posixify(str) {
319+
return str.replace(/\\/g, '/');
320+
}

0 commit comments

Comments
 (0)