forked from sveltejs/kit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
114 lines (90 loc) · 2.93 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import fs from 'fs';
import { execSync } from 'child_process';
import esbuild from 'esbuild';
import toml from '@iarna/toml';
import { fileURLToPath } from 'url';
/**
* @typedef {import('esbuild').BuildOptions} BuildOptions
*/
/**
* @param {{
* esbuild?: (defaultOptions: BuildOptions) => Promise<BuildOptions> | BuildOptions;
* }} [options]
**/
export default function (options) {
/** @type {import('@sveltejs/kit').Adapter} */
const adapter = {
name: '@sveltejs/adapter-cloudflare-workers',
async adapt({ utils }) {
const { site } = validate_config(utils);
const bucket = site.bucket;
const entrypoint = site['entry-point'] || 'workers-site';
const files = fileURLToPath(new URL('./files', import.meta.url));
utils.rimraf(bucket);
utils.rimraf(entrypoint);
utils.log.info('Installing worker dependencies...');
utils.copy(`${files}/_package.json`, '.svelte-kit/cloudflare-workers/package.json');
// TODO would be cool if we could make this step unnecessary somehow
const stdout = execSync('npm install', { cwd: '.svelte-kit/cloudflare-workers' });
utils.log.info(stdout.toString());
utils.log.minor('Generating worker...');
utils.copy(`${files}/entry.js`, '.svelte-kit/cloudflare-workers/entry.js');
/** @type {BuildOptions} */
const defaultOptions = {
entryPoints: ['.svelte-kit/cloudflare-workers/entry.js'],
outfile: `${entrypoint}/index.js`,
bundle: true,
target: 'es2020',
platform: 'browser'
};
const buildOptions =
options && options.esbuild ? await options.esbuild(defaultOptions) : defaultOptions;
await esbuild.build(buildOptions);
fs.writeFileSync(`${entrypoint}/package.json`, JSON.stringify({ main: 'index.js' }));
utils.log.info('Prerendering static pages...');
await utils.prerender({
dest: bucket
});
utils.log.minor('Copying assets...');
utils.copy_static_files(bucket);
utils.copy_client_files(bucket);
}
};
return adapter;
}
function validate_config(utils) {
if (fs.existsSync('wrangler.toml')) {
let wrangler_config;
try {
wrangler_config = toml.parse(fs.readFileSync('wrangler.toml', 'utf-8'));
} catch (err) {
err.message = `Error parsing wrangler.toml: ${err.message}`;
throw err;
}
if (!wrangler_config.site || !wrangler_config.site.bucket) {
throw new Error(
'You must specify site.bucket in wrangler.toml. Consult https://developers.cloudflare.com/workers/platform/sites/configuration'
);
}
return wrangler_config;
}
utils.log.error(
'Consult https://developers.cloudflare.com/workers/platform/sites/configuration on how to setup your site'
);
utils.log(
`
Sample wrangler.toml:
name = "<your-site-name>"
type = "javascript"
account_id = "<your-account-id>"
workers_dev = true
route = ""
zone_id = ""
[site]
bucket = "./.cloudflare/assets"
entry-point = "./.cloudflare/worker"`
.replace(/^\t+/gm, '')
.trim()
);
throw new Error('Missing a wrangler.toml file');
}