@@ -11,14 +11,17 @@ export default function () {
11
11
name : '@sveltejs/adapter-cloudflare-workers' ,
12
12
13
13
async adapt ( builder ) {
14
- const { site } = validate_config ( builder ) ;
14
+ const { site, build } = validate_config ( builder ) ;
15
15
16
16
// @ts -ignore
17
17
const { bucket } = site ;
18
18
19
19
// @ts -ignore
20
20
const entrypoint = site [ 'entry-point' ] || 'workers-site' ;
21
21
22
+ // @ts -ignore
23
+ const main_path = build . upload . main ;
24
+
22
25
const files = fileURLToPath ( new URL ( './files' , import . meta. url ) . href ) ;
23
26
const tmp = builder . getBuildDirectory ( 'cloudflare-workers-tmp' ) ;
24
27
@@ -51,13 +54,18 @@ export default function () {
51
54
52
55
await esbuild . build ( {
53
56
entryPoints : [ `${ tmp } /entry.js` ] ,
54
- outfile : `${ entrypoint } /index.js` ,
55
- bundle : true ,
57
+ outfile : `${ entrypoint } /${ main_path } ` ,
56
58
target : 'es2020' ,
57
- platform : 'browser'
59
+ platform : 'browser' ,
60
+ bundle : true ,
61
+ external : [ '__STATIC_CONTENT_MANIFEST' ] ,
62
+ format : 'esm'
58
63
} ) ;
59
64
60
- writeFileSync ( `${ entrypoint } /package.json` , JSON . stringify ( { main : 'index.js' } ) ) ;
65
+ writeFileSync (
66
+ `${ entrypoint } /package.json` ,
67
+ JSON . stringify ( { main : main_path , type : 'module' } )
68
+ ) ;
61
69
62
70
builder . log . minor ( 'Copying assets...' ) ;
63
71
builder . writeClient ( bucket ) ;
@@ -86,6 +94,24 @@ function validate_config(builder) {
86
94
) ;
87
95
}
88
96
97
+ // @ts -ignore
98
+ const main_file = wrangler_config . build ?. upload ?. main ;
99
+ const main_file_ext = main_file ?. split ( '.' ) . slice ( - 1 ) [ 0 ] ;
100
+ if ( main_file_ext && main_file_ext !== 'mjs' ) {
101
+ // @ts -ignore
102
+ const upload_rules = wrangler_config . build ?. upload ?. rules ;
103
+ // @ts -ignore
104
+ const matching_rule = upload_rules ?. find ( ( { globs } ) =>
105
+ // @ts -ignore
106
+ globs . find ( ( glob ) => glob . endsWith ( `*.${ main_file_ext } ` ) )
107
+ ) ;
108
+ if ( ! matching_rule ) {
109
+ throw new Error (
110
+ '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'
111
+ ) ;
112
+ }
113
+ }
114
+
89
115
return wrangler_config ;
90
116
}
91
117
@@ -104,6 +130,13 @@ function validate_config(builder) {
104
130
route = ""
105
131
zone_id = ""
106
132
133
+ [build]
134
+ command = ""
135
+
136
+ [build.upload]
137
+ format = "modules"
138
+ main = "./worker.mjs"
139
+
107
140
[site]
108
141
bucket = "./.cloudflare/assets"
109
142
entry-point = "./.cloudflare/worker"`
0 commit comments