Skip to content

Commit

Permalink
feat: remove esbuild step (#13132)
Browse files Browse the repository at this point in the history
  • Loading branch information
eltigerchino authored Dec 27, 2024
1 parent 528af75 commit c2b0d89
Show file tree
Hide file tree
Showing 4 changed files with 209 additions and 106 deletions.
5 changes: 5 additions & 0 deletions .changeset/famous-rules-lick.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/adapter-cloudflare': major
---

feat: remove esbuild step
89 changes: 5 additions & 84 deletions packages/adapter-cloudflare/index.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,8 @@
import { existsSync, writeFileSync } from 'node:fs';
import * as path from 'node:path';
import { fileURLToPath } from 'node:url';
import * as esbuild from 'esbuild';
import { getPlatformProxy } from 'wrangler';

// list from https://developers.cloudflare.com/workers/runtime-apis/nodejs/
const compatible_node_modules = [
'assert',
'async_hooks',
'buffer',
'crypto',
'diagnostics_channel',
'events',
'path',
'process',
'stream',
'string_decoder',
'util'
];

/** @type {import('./index.js').default} */
export default function (options = {}) {
return {
Expand Down Expand Up @@ -52,7 +36,7 @@ export default function (options = {}) {
const written_files = builder.writeClient(dest_dir);
builder.writePrerendered(dest_dir);

const relativePath = path.posix.relative(tmp, builder.getServerDirectory());
const relativePath = path.posix.relative(dest, builder.getServerDirectory());

writeFileSync(
`${tmp}/manifest.js`,
Expand All @@ -74,77 +58,14 @@ export default function (options = {}) {
});
}

builder.copy(`${files}/worker.js`, `${tmp}/_worker.js`, {
writeFileSync(`${dest}/.assetsignore`, generate_assetsignore(), { flag: 'a' });

builder.copy(`${files}/worker.js`, `${dest}/_worker.js`, {
replace: {
SERVER: `${relativePath}/index.js`,
MANIFEST: './manifest.js'
MANIFEST: `${path.posix.relative(dest, tmp)}/manifest.js`
}
});

const external = ['cloudflare:*', ...compatible_node_modules.map((id) => `node:${id}`)];

try {
const result = await esbuild.build({
platform: 'browser',
// https://github.com/cloudflare/workers-sdk/blob/a12b2786ce745f24475174bcec994ad691e65b0f/packages/wrangler/src/deployment-bundle/bundle.ts#L35-L36
conditions: ['workerd', 'worker', 'browser'],
sourcemap: 'linked',
target: 'es2022',
entryPoints: [`${tmp}/_worker.js`],
outfile: `${dest}/_worker.js`,
allowOverwrite: true,
format: 'esm',
bundle: true,
loader: {
'.wasm': 'copy',
'.woff': 'copy',
'.woff2': 'copy',
'.ttf': 'copy',
'.eot': 'copy',
'.otf': 'copy'
},
external,
alias: Object.fromEntries(compatible_node_modules.map((id) => [id, `node:${id}`])),
logLevel: 'silent'
});

if (result.warnings.length > 0) {
const formatted = await esbuild.formatMessages(result.warnings, {
kind: 'warning',
color: true
});

console.error(formatted.join('\n'));
}
} catch (error) {
for (const e of error.errors) {
for (const node of e.notes) {
const match =
/The package "(.+)" wasn't found on the file system but is built into node/.exec(
node.text
);

if (match) {
node.text = `Cannot use "${match[1]}" when deploying to Cloudflare.`;
}
}
}

const formatted = await esbuild.formatMessages(error.errors, {
kind: 'error',
color: true
});

console.error(formatted.join('\n'));

throw new Error(
`Bundling with esbuild failed with ${error.errors.length} ${
error.errors.length === 1 ? 'error' : 'errors'
}`
);
}

writeFileSync(`${dest}/.assetsignore`, generate_assetsignore(), { flag: 'a' });
},
async emulate() {
const proxy = await getPlatformProxy(options.platformProxy);
Expand Down
4 changes: 2 additions & 2 deletions packages/adapter-cloudflare/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"prepublishOnly": "pnpm build"
},
"dependencies": {
"@cloudflare/workers-types": "^4.20231121.0",
"@cloudflare/workers-types": "^4.20241106.0",
"esbuild": "^0.24.0",
"worktop": "0.8.0-next.18"
},
Expand All @@ -52,6 +52,6 @@
},
"peerDependencies": {
"@sveltejs/kit": "^2.0.0",
"wrangler": "^3.28.4"
"wrangler": "^3.87.0"
}
}
Loading

0 comments on commit c2b0d89

Please sign in to comment.