1
- import { existsSync , readFileSync } from "node:fs" ;
1
+ import { existsSync , lstatSync , readFileSync } from "node:fs" ;
2
2
import { tmpdir } from "node:os" ;
3
- import { join , resolve as resolvePath } from "node:path" ;
3
+ import { join , resolve , resolve as resolvePath } from "node:path" ;
4
4
import { cwd } from "node:process" ;
5
5
import { File , FormData } from "undici" ;
6
6
import { fetchResult } from "../../cfetch" ;
@@ -17,6 +17,7 @@ import {
17
17
} from "../../pages/functions/buildWorker" ;
18
18
import { validateRoutes } from "../../pages/functions/routes-validation" ;
19
19
import { upload } from "../../pages/upload" ;
20
+ import traverseModuleGraph from "../../traverse-module-graph" ;
20
21
import { createUploadWorkerBundleContents } from "./create-worker-bundle-contents" ;
21
22
import type { BundleResult } from "../../bundle" ;
22
23
import type { Project , Deployment } from "@cloudflare/types" ;
@@ -95,9 +96,10 @@ export async function publish({
95
96
_redirects : string | undefined ,
96
97
_routesGenerated : string | undefined ,
97
98
_routesCustom : string | undefined ,
99
+ _workerJSDirectory = false ,
98
100
_workerJS : string | undefined ;
99
101
100
- const workerScriptPath = resolvePath ( directory , "_worker.js" ) ;
102
+ const _workerPath = resolvePath ( directory , "_worker.js" ) ;
101
103
102
104
try {
103
105
_headers = readFileSync ( join ( directory , "_headers" ) , "utf-8" ) ;
@@ -116,7 +118,10 @@ export async function publish({
116
118
} catch { }
117
119
118
120
try {
119
- _workerJS = readFileSync ( workerScriptPath , "utf-8" ) ;
121
+ _workerJSDirectory = lstatSync ( _workerPath ) . isDirectory ( ) ;
122
+ if ( ! _workerJSDirectory ) {
123
+ _workerJS = readFileSync ( _workerPath , "utf-8" ) ;
124
+ }
120
125
} catch { }
121
126
122
127
// Grab the bindings from the API, we need these for shims and other such hacky inserts
@@ -243,13 +248,28 @@ export async function publish({
243
248
* When using a _worker.js file, the entire /functions directory is ignored
244
249
* – this includes its routing and middleware characteristics.
245
250
*/
246
- if ( _workerJS ) {
251
+ if ( _workerJSDirectory ) {
252
+ workerBundle = await traverseModuleGraph (
253
+ {
254
+ file : resolve ( join ( _workerPath , "index.js" ) ) ,
255
+ directory : resolve ( _workerPath ) ,
256
+ format : "modules" ,
257
+ moduleRoot : resolve ( _workerPath ) ,
258
+ } ,
259
+ [
260
+ {
261
+ type : "ESModule" ,
262
+ globs : [ "**/*.js" ] ,
263
+ } ,
264
+ ]
265
+ ) ;
266
+ } else if ( _workerJS ) {
247
267
if ( bundle ) {
248
268
const outfile = join ( tmpdir ( ) , `./bundledWorker-${ Math . random ( ) } .mjs` ) ;
249
269
workerBundle = await buildRawWorker ( {
250
- workerScriptPath,
270
+ workerScriptPath : _workerPath ,
251
271
outfile,
252
- directory : directory ?? "." ,
272
+ directory,
253
273
local : false ,
254
274
sourcemap : true ,
255
275
watch : false ,
@@ -258,13 +278,13 @@ export async function publish({
258
278
nodejsCompat,
259
279
} ) ;
260
280
} else {
261
- await checkRawWorker ( workerScriptPath , ( ) => { } ) ;
262
- // TODO: Replace this with the cool new no-bundle stuff when that lands: https://github.com/cloudflare/workers-sdk/pull/2769
281
+ await checkRawWorker ( _workerPath , ( ) => { } ) ;
282
+ // TODO: Let users configure this in the future.
263
283
workerBundle = {
264
284
modules : [ ] ,
265
285
dependencies : { } ,
266
286
stop : undefined ,
267
- resolvedEntryPointPath : workerScriptPath ,
287
+ resolvedEntryPointPath : _workerPath ,
268
288
bundleType : "esm" ,
269
289
} ;
270
290
}
0 commit comments