Skip to content

Commit a6f7259

Browse files
GauBenbenmccanneltigerchino
authored
fix: resolve assets.directory path from wrangler config (#14036)
* fix(adapter-cloudflare): resolve paths from wrangler config * Create new-donkeys-approve.md * my bad Co-authored-by: Ben McCann <[email protected]> * cleanup --------- Co-authored-by: Ben McCann <[email protected]> Co-authored-by: Chew Tee Ming <[email protected]>
1 parent 23a6564 commit a6f7259

File tree

5 files changed

+27
-10
lines changed

5 files changed

+27
-10
lines changed

.changeset/new-donkeys-approve.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: resolve the absolute path of the Wrangler config setting `assets.directory` in case the config file is in a different directory than the root project

packages/adapter-cloudflare/index.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { VERSION } from '@sveltejs/kit';
22
import { copyFileSync, existsSync, writeFileSync } from 'node:fs';
33
import path from 'node:path';
4+
import process from 'node:process';
45
import { fileURLToPath } from 'node:url';
5-
import { is_building_for_cloudflare_pages, validate_worker_settings } from './utils.js';
66
import { getPlatformProxy, unstable_readConfig } from 'wrangler';
7+
import { is_building_for_cloudflare_pages, validate_worker_settings } from './utils.js';
78

89
const name = '@sveltejs/adapter-cloudflare';
910
const [kit_major, kit_minor] = VERSION.split('.');
@@ -32,6 +33,7 @@ export default function (options = {}) {
3233
}
3334

3435
const wrangler_config = validate_wrangler_config(options.config);
36+
3537
const building_for_cloudflare_pages = is_building_for_cloudflare_pages(wrangler_config);
3638

3739
let dest = builder.getBuildDirectory('cloudflare');
@@ -48,7 +50,12 @@ export default function (options = {}) {
4850
worker_dest = wrangler_config.main;
4951
}
5052
if (wrangler_config.assets?.directory) {
51-
dest = wrangler_config.assets.directory;
53+
// wrangler doesn't resolve `assets.directory` to an absolute path unlike
54+
// `main` and `pages_build_output_dir` so we need to do it ourselves here
55+
const parent_dir = wrangler_config.configPath
56+
? path.dirname(path.resolve(wrangler_config.configPath))
57+
: process.cwd();
58+
dest = path.resolve(parent_dir, wrangler_config.assets.directory);
5259
}
5360
if (wrangler_config.assets?.binding) {
5461
assets_binding = wrangler_config.assets.binding;
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// we've moved the wrangler config away from the root of the project
2+
// to test that the adapter still resolves the paths correctly
3+
{
4+
"$schema": "../node_modules/wrangler/config-schema.json",
5+
"main": "../dist/index.js",
6+
"assets": {
7+
"directory": "../dist/public",
8+
"binding": "ASSETS"
9+
}
10+
}

packages/adapter-cloudflare/test/apps/workers/svelte.config.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ import adapter from '../../../index.js';
33
/** @type {import('@sveltejs/kit').Config} */
44
const config = {
55
kit: {
6-
adapter: adapter()
6+
adapter: adapter({
7+
config: 'config/wrangler.jsonc'
8+
})
79
}
810
};
911

packages/adapter-cloudflare/test/apps/workers/wrangler.jsonc

Lines changed: 0 additions & 7 deletions
This file was deleted.

0 commit comments

Comments
 (0)