Skip to content

Commit 1aa65db

Browse files
committed
fix: update adapter-cloudflare-workers to build module package see #4276 (review)
1 parent 4db18af commit 1aa65db

File tree

3 files changed

+47
-9
lines changed

3 files changed

+47
-9
lines changed

.changeset/few-walls-obey.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#### add the following to your wrangler.toml
88
```toml
9-
[build.upload]
10-
format = "modules"
9+
[build.upload]
10+
format = "modules"
11+
main = "./worker.mjs"
1112
```

packages/adapter-cloudflare-workers/README.md

+6-2
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ Then configure your sites build directory and your account-details in the config
5353
```toml
5454
account_id = 'YOUR ACCOUNT_ID'
5555
zone_id = 'YOUR ZONE_ID' # optional, if you don't specify this a workers.dev subdomain will be used.
56-
site = {bucket = "./build", entry-point = "./workers-site"}
5756

5857
type = "javascript"
5958

@@ -62,7 +61,12 @@ type = "javascript"
6261
command = ""
6362

6463
[build.upload]
65-
format = "service-worker"
64+
format = "modules"
65+
main = "./worker.mjs"
66+
67+
[site]
68+
bucket = "./.cloudflare/assets"
69+
entry-point = "./.cloudflare/worker"
6670
```
6771

6872
It's recommended that you add the `build` and `workers-site` folders (or whichever other folders you specify) to your `.gitignore`.

packages/adapter-cloudflare-workers/index.js

+38-5
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,17 @@ export default function () {
1111
name: '@sveltejs/adapter-cloudflare-workers',
1212

1313
async adapt(builder) {
14-
const { site } = validate_config(builder);
14+
const { site, build } = validate_config(builder);
1515

1616
// @ts-ignore
1717
const { bucket } = site;
1818

1919
// @ts-ignore
2020
const entrypoint = site['entry-point'] || 'workers-site';
2121

22+
// @ts-ignore
23+
const main_path = build.upload.main;
24+
2225
const files = fileURLToPath(new URL('./files', import.meta.url).href);
2326
const tmp = builder.getBuildDirectory('cloudflare-workers-tmp');
2427

@@ -51,13 +54,18 @@ export default function () {
5154

5255
await esbuild.build({
5356
entryPoints: [`${tmp}/entry.js`],
54-
outfile: `${entrypoint}/index.js`,
55-
bundle: true,
57+
outfile: `${entrypoint}/${main_path}`,
5658
target: 'es2020',
57-
platform: 'browser'
59+
platform: 'browser',
60+
bundle: true,
61+
external: ['__STATIC_CONTENT_MANIFEST'],
62+
format: 'esm'
5863
});
5964

60-
writeFileSync(`${entrypoint}/package.json`, JSON.stringify({ main: 'index.js' }));
65+
writeFileSync(
66+
`${entrypoint}/package.json`,
67+
JSON.stringify({ main: main_path, type: 'module' })
68+
);
6169

6270
builder.log.minor('Copying assets...');
6371
builder.writeClient(bucket);
@@ -86,6 +94,24 @@ function validate_config(builder) {
8694
);
8795
}
8896

97+
// @ts-ignore
98+
const main_file = wrangler_config.build?.upload?.main;
99+
const main_file_ext = main_file?.split('.').slice(-1)[0];
100+
if (main_file_ext && main_file_ext !== 'mjs') {
101+
// @ts-ignore
102+
const upload_rules = wrangler_config.build?.upload?.rules;
103+
// @ts-ignore
104+
const matching_rule = upload_rules?.find(({ globs }) =>
105+
// @ts-ignore
106+
globs.find((glob) => glob.endsWith(`*.${main_file_ext}`))
107+
);
108+
if (!matching_rule) {
109+
throw new Error(
110+
'To support a build.upload.main value not ending in .mjs, an upload rule must be added to build.upload.rules. Consult https://developers.cloudflare.com/workers/cli-wrangler/configuration/#build'
111+
);
112+
}
113+
}
114+
89115
return wrangler_config;
90116
}
91117

@@ -104,6 +130,13 @@ function validate_config(builder) {
104130
route = ""
105131
zone_id = ""
106132
133+
[build]
134+
command = ""
135+
136+
[build.upload]
137+
format = "modules"
138+
main = "./worker.mjs"
139+
107140
[site]
108141
bucket = "./.cloudflare/assets"
109142
entry-point = "./.cloudflare/worker"`

0 commit comments

Comments
 (0)