1
- import { existsSync , readFileSync } from "node:fs" ;
1
+ import { existsSync , lstatSync , readFileSync } from "node:fs" ;
2
2
import { tmpdir } from "node:os" ;
3
3
import { join , resolve as resolvePath } from "node:path" ;
4
4
import { cwd } from "node:process" ;
@@ -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,55 @@ 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
+ const traverseModuleGraphResult = await traverseModuleGraph (
253
+ {
254
+ file : resolvePath ( join ( _workerPath , "index.js" ) ) ,
255
+ directory : resolvePath ( _workerPath ) ,
256
+ format : "modules" ,
257
+ moduleRoot : resolvePath ( _workerPath ) ,
258
+ } ,
259
+ [
260
+ {
261
+ type : "ESModule" ,
262
+ globs : [ "**/*.js" ] ,
263
+ } ,
264
+ ]
265
+ ) ;
266
+
267
+ const outfile = join ( tmpdir ( ) , `./bundledWorker-${ Math . random ( ) } .mjs` ) ;
268
+ const bundleResult = await buildRawWorker ( {
269
+ workerScriptPath : _workerPath ,
270
+ bundle : false ,
271
+ outfile,
272
+ directory,
273
+ local : false ,
274
+ sourcemap : true ,
275
+ watch : false ,
276
+ onEnd : ( ) => { } ,
277
+ betaD1Shims : d1Databases ,
278
+ nodejsCompat,
279
+ } ) ;
280
+
281
+ workerBundle = {
282
+ modules : ( traverseModuleGraphResult ?. modules ??
283
+ bundleResult ?. modules ) as BundleResult [ "modules" ] ,
284
+ dependencies : ( bundleResult ?. dependencies ??
285
+ traverseModuleGraphResult ?. dependencies ) as BundleResult [ "dependencies" ] ,
286
+ resolvedEntryPointPath : ( bundleResult ?. resolvedEntryPointPath ??
287
+ traverseModuleGraphResult ?. resolvedEntryPointPath ) as BundleResult [ "resolvedEntryPointPath" ] ,
288
+ bundleType : ( bundleResult ?. bundleType ??
289
+ traverseModuleGraphResult ?. bundleType ) as BundleResult [ "bundleType" ] ,
290
+ stop : bundleResult ?. stop ,
291
+ sourceMapPath : bundleResult ?. sourceMapPath ,
292
+ } ;
293
+ } else if ( _workerJS ) {
247
294
if ( bundle ) {
248
295
const outfile = join ( tmpdir ( ) , `./bundledWorker-${ Math . random ( ) } .mjs` ) ;
249
296
workerBundle = await buildRawWorker ( {
250
- workerScriptPath,
297
+ workerScriptPath : _workerPath ,
251
298
outfile,
252
- directory : directory ?? "." ,
299
+ directory,
253
300
local : false ,
254
301
sourcemap : true ,
255
302
watch : false ,
@@ -258,13 +305,13 @@ export async function publish({
258
305
nodejsCompat,
259
306
} ) ;
260
307
} 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
308
+ await checkRawWorker ( _workerPath , ( ) => { } ) ;
309
+ // TODO: Let users configure this in the future.
263
310
workerBundle = {
264
311
modules : [ ] ,
265
312
dependencies : { } ,
266
313
stop : undefined ,
267
- resolvedEntryPointPath : workerScriptPath ,
314
+ resolvedEntryPointPath : _workerPath ,
268
315
bundleType : "esm" ,
269
316
} ;
270
317
}
0 commit comments