@sveltejs/adapter-cloudflare-workers
Advanced tools
Comparing version 1.0.0-next.39 to 1.0.0-next.40
98
index.js
import { existsSync, readFileSync, writeFileSync } from 'fs'; | ||
import { posix } from 'path'; | ||
import { posix, dirname } from 'path'; | ||
import { execSync } from 'child_process'; | ||
@@ -8,2 +8,11 @@ import esbuild from 'esbuild'; | ||
/** | ||
* @typedef {{ | ||
* main: string; | ||
* site: { | ||
* bucket: string; | ||
* } | ||
* }} WranglerConfig | ||
*/ | ||
/** @type {import('.')} */ | ||
@@ -15,18 +24,9 @@ export default function (options = {}) { | ||
async adapt(builder) { | ||
const { site, build } = validate_config(builder); | ||
const { main, site } = 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'); | ||
builder.rimraf(bucket); | ||
builder.rimraf(entrypoint); | ||
builder.rimraf(site.bucket); | ||
builder.rimraf(dirname(main)); | ||
@@ -64,3 +64,3 @@ builder.log.info('Installing worker dependencies...'); | ||
entryPoints: [`${tmp}/entry.js`], | ||
outfile: `${entrypoint}/${main_path}`, | ||
outfile: main, | ||
bundle: true, | ||
@@ -71,11 +71,6 @@ external: ['__STATIC_CONTENT_MANIFEST', ...(options?.external || [])], | ||
writeFileSync( | ||
`${entrypoint}/package.json`, | ||
JSON.stringify({ main: main_path, type: 'module' }) | ||
); | ||
builder.log.minor('Copying assets...'); | ||
builder.writeClient(bucket); | ||
builder.writeStatic(bucket); | ||
builder.writePrerendered(bucket); | ||
builder.writeClient(site.bucket); | ||
builder.writeStatic(site.bucket); | ||
builder.writePrerendered(site.bucket); | ||
} | ||
@@ -85,9 +80,15 @@ }; | ||
/** @param {import('@sveltejs/kit').Builder} builder */ | ||
/** | ||
* @param {import('@sveltejs/kit').Builder} builder | ||
* @returns {WranglerConfig} | ||
*/ | ||
function validate_config(builder) { | ||
if (existsSync('wrangler.toml')) { | ||
/** @type {WranglerConfig} */ | ||
let wrangler_config; | ||
try { | ||
wrangler_config = toml.parse(readFileSync('wrangler.toml', 'utf-8')); | ||
wrangler_config = /** @type {WranglerConfig} */ ( | ||
toml.parse(readFileSync('wrangler.toml', 'utf-8')) | ||
); | ||
} catch (err) { | ||
@@ -98,4 +99,3 @@ err.message = `Error parsing wrangler.toml: ${err.message}`; | ||
// @ts-ignore | ||
if (!wrangler_config.site || !wrangler_config.site.bucket) { | ||
if (!wrangler_config.site?.bucket) { | ||
throw new Error( | ||
@@ -106,32 +106,8 @@ 'You must specify site.bucket in wrangler.toml. Consult https://developers.cloudflare.com/workers/platform/sites/configuration' | ||
// @ts-ignore | ||
if (!wrangler_config.build || !wrangler_config.build.upload) { | ||
if (!wrangler_config.main) { | ||
throw new Error( | ||
'You must specify build.upload options in wrangler.toml. Consult https://github.com/sveltejs/kit/tree/master/packages/adapter-cloudflare-workers' | ||
'You must specify main option in wrangler.toml. Consult https://github.com/sveltejs/kit/tree/master/packages/adapter-cloudflare-workers' | ||
); | ||
} | ||
// @ts-ignore | ||
if (wrangler_config.build.upload.format !== 'modules') { | ||
throw new Error('build.upload.format in wrangler.toml must be "modules"'); | ||
} | ||
// @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; | ||
@@ -149,18 +125,12 @@ } | ||
name = "<your-site-name>" | ||
type = "javascript" | ||
account_id = "<your-account-id>" | ||
workers_dev = true | ||
route = "" | ||
zone_id = "" | ||
route = "<your-route>" | ||
[build] | ||
command = "" | ||
main = "./.cloudflare/worker.js" | ||
site.bucket = "./.cloudflare/public" | ||
[build.upload] | ||
format = "modules" | ||
main = "./worker.mjs" | ||
build.command = "npm run build" | ||
[site] | ||
bucket = "./.cloudflare/assets" | ||
entry-point = "./.cloudflare/worker"` | ||
compatibility_date = "2021-11-12" | ||
workers_dev = true` | ||
.replace(/^\t+/gm, '') | ||
@@ -167,0 +137,0 @@ .trim() |
{ | ||
"name": "@sveltejs/adapter-cloudflare-workers", | ||
"version": "1.0.0-next.39", | ||
"version": "1.0.0-next.40", | ||
"repository": { | ||
@@ -5,0 +5,0 @@ "type": "git", |
@@ -5,2 +5,4 @@ # adapter-cloudflare-workers | ||
**Requires [Wrangler v2](https://developers.cloudflare.com/workers/wrangler/get-started/).** Wrangler v1 is no longer supported. | ||
_**Comparisons**_ | ||
@@ -7,0 +9,0 @@ |
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
95
11490
199