Skip to content

Commit

Permalink
fix: update adapter-cloudflare-workers to build module package see #4…
Browse files Browse the repository at this point in the history
  • Loading branch information
dominikg committed Mar 21, 2022
1 parent b9c1724 commit b1ff9fe
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 9 deletions.
5 changes: 3 additions & 2 deletions .changeset/few-walls-obey.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#### add the following to your wrangler.toml
```toml
[build.upload]
format = "modules"
[build.upload]
format = "modules"
main = "./worker.mjs"
```
8 changes: 6 additions & 2 deletions packages/adapter-cloudflare-workers/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ Then configure your sites build directory and your account-details in the config
```toml
account_id = 'YOUR ACCOUNT_ID'
zone_id = 'YOUR ZONE_ID' # optional, if you don't specify this a workers.dev subdomain will be used.
site = {bucket = "./build", entry-point = "./workers-site"}

type = "javascript"

Expand All @@ -62,7 +61,12 @@ type = "javascript"
command = ""

[build.upload]
format = "service-worker"
format = "modules"
main = "./worker.mjs"

[site]
bucket = "./.cloudflare/assets"
entry-point = "./.cloudflare/worker"
```

It's recommended that you add the `build` and `workers-site` folders (or whichever other folders you specify) to your `.gitignore`.
Expand Down
43 changes: 38 additions & 5 deletions packages/adapter-cloudflare-workers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,17 @@ export default function () {
name: '@sveltejs/adapter-cloudflare-workers',

async adapt(builder) {
const { site } = validate_config(builder);
const { site, build } = validate_config(builder);

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

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

// @ts-ignore
const main_path = build.upload.main;

const files = fileURLToPath(new URL('./files', import.meta.url).href);
const tmp = builder.getBuildDirectory('cloudflare-workers-tmp');

Expand Down Expand Up @@ -51,13 +54,18 @@ export default function () {

await esbuild.build({
entryPoints: [`${tmp}/entry.js`],
outfile: `${entrypoint}/index.js`,
bundle: true,
outfile: `${entrypoint}/${main_path}`,
target: 'es2020',
platform: 'browser'
platform: 'browser',
bundle: true,
external: ['__STATIC_CONTENT_MANIFEST'],
format: 'esm'
});

writeFileSync(`${entrypoint}/package.json`, JSON.stringify({ main: 'index.js' }));
writeFileSync(
`${entrypoint}/package.json`,
JSON.stringify({ main: main_path, type: 'module' })
);

builder.log.minor('Copying assets...');
builder.writeClient(bucket);
Expand Down Expand Up @@ -86,6 +94,24 @@ function validate_config(builder) {
);
}

// @ts-ignore
const main_file = wrangler_config.build?.upload?.main;
const main_file_ext = main_file?.split('.').slice(-1)[0];
if (main_file_ext && main_file_ext !== 'mjs') {
// @ts-ignore
const upload_rules = wrangler_config.build?.upload?.rules;
// @ts-ignore
const matching_rule = upload_rules?.find(({ globs }) =>
// @ts-ignore
globs.find((glob) => glob.endsWith(`*.${main_file_ext}`))
);
if (!matching_rule) {
throw new Error(
'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'
);
}
}

return wrangler_config;
}

Expand All @@ -104,6 +130,13 @@ function validate_config(builder) {
route = ""
zone_id = ""
[build]
command = ""
[build.upload]
format = "modules"
main = "./worker.mjs"
[site]
bucket = "./.cloudflare/assets"
entry-point = "./.cloudflare/worker"`
Expand Down

0 comments on commit b1ff9fe

Please sign in to comment.