@sveltejs/adapter-cloudflare-workers
Advanced tools
Comparing version 2.0.3 to 2.1.0
import { Adapter } from '@sveltejs/kit'; | ||
import './ambient.js'; | ||
export default function plugin(options?: { config?: string }): Adapter; | ||
export default function plugin(options?: AdapterOptions): Adapter; | ||
export interface AdapterOptions { | ||
config?: string; | ||
} |
88
index.js
@@ -14,5 +14,21 @@ import { existsSync, readFileSync, writeFileSync } from 'node:fs'; | ||
* } | ||
* compatibility_flags?: string[]; | ||
* }} WranglerConfig | ||
*/ | ||
// 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} */ | ||
@@ -24,3 +40,3 @@ export default function ({ config = 'wrangler.toml' } = {}) { | ||
async adapt(builder) { | ||
const { main, site } = validate_config(builder, config); | ||
const { main, site, compatibility_flags } = validate_config(builder, config); | ||
@@ -66,17 +82,61 @@ const files = fileURLToPath(new URL('./files', import.meta.url).href); | ||
await esbuild.build({ | ||
platform: 'browser', | ||
conditions: ['worker', 'browser'], | ||
sourcemap: 'linked', | ||
target: 'es2022', | ||
entryPoints: [`${tmp}/entry.js`], | ||
outfile: main, | ||
bundle: true, | ||
external: ['__STATIC_CONTENT_MANIFEST', 'cloudflare:*'], | ||
format: 'esm', | ||
loader: { | ||
'.wasm': 'copy' | ||
const external = ['__STATIC_CONTENT_MANIFEST', 'cloudflare:*']; | ||
if (compatibility_flags && compatibility_flags.includes('nodejs_compat')) { | ||
external.push(...compatible_node_modules.map((id) => `node:${id}`)); | ||
} | ||
try { | ||
const result = await esbuild.build({ | ||
platform: 'browser', | ||
conditions: ['worker', 'browser'], | ||
sourcemap: 'linked', | ||
target: 'es2022', | ||
entryPoints: [`${tmp}/entry.js`], | ||
outfile: main, | ||
bundle: true, | ||
external, | ||
alias: Object.fromEntries(compatible_node_modules.map((id) => [id, `node:${id}`])), | ||
format: 'esm', | ||
loader: { | ||
'.wasm': 'copy' | ||
}, | ||
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' | ||
}` | ||
); | ||
} | ||
builder.log.minor('Copying assets...'); | ||
@@ -83,0 +143,0 @@ const bucket_dir = `${site.bucket}${builder.config.kit.paths.base}`; |
{ | ||
"name": "@sveltejs/adapter-cloudflare-workers", | ||
"version": "2.0.3", | ||
"version": "2.1.0", | ||
"description": "SvelteKit adapter that creates a Cloudflare Workers site using a function for dynamic server rendering", | ||
@@ -36,3 +36,3 @@ "repository": { | ||
"typescript": "^5.3.3", | ||
"@sveltejs/kit": "^2.3.3" | ||
"@sveltejs/kit": "^2.4.0" | ||
}, | ||
@@ -39,0 +39,0 @@ "peerDependencies": { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
12069
304