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" ;
@@ -14,6 +14,7 @@ import {
14
14
import {
15
15
buildRawWorker ,
16
16
checkRawWorker ,
17
+ traverseAndBuildWorkerJSDirectory ,
17
18
} from "../../pages/functions/buildWorker" ;
18
19
import { validateRoutes } from "../../pages/functions/routes-validation" ;
19
20
import { upload } from "../../pages/upload" ;
@@ -65,7 +66,7 @@ interface PagesPublishOptions {
65
66
66
67
/**
67
68
* Whether to run bundling on `_worker.js` before deploying.
68
- * Default: false
69
+ * Default: true
69
70
*/
70
71
bundle ?: boolean ;
71
72
@@ -95,9 +96,12 @@ export async function publish({
95
96
_redirects : string | undefined ,
96
97
_routesGenerated : string | undefined ,
97
98
_routesCustom : string | undefined ,
99
+ _workerJSIsDirectory = false ,
98
100
_workerJS : string | undefined ;
99
101
100
- const workerScriptPath = resolvePath ( directory , "_worker.js" ) ;
102
+ bundle = bundle ?? true ;
103
+
104
+ const _workerPath = resolvePath ( directory , "_worker.js" ) ;
101
105
102
106
try {
103
107
_headers = readFileSync ( join ( directory , "_headers" ) , "utf-8" ) ;
@@ -116,7 +120,10 @@ export async function publish({
116
120
} catch { }
117
121
118
122
try {
119
- _workerJS = readFileSync ( workerScriptPath , "utf-8" ) ;
123
+ _workerJSIsDirectory = lstatSync ( _workerPath ) . isDirectory ( ) ;
124
+ if ( ! _workerJSIsDirectory ) {
125
+ _workerJS = readFileSync ( _workerPath , "utf-8" ) ;
126
+ }
120
127
} catch { }
121
128
122
129
// Grab the bindings from the API, we need these for shims and other such hacky inserts
@@ -240,16 +247,23 @@ export async function publish({
240
247
* Advanced Mode
241
248
* https://developers.cloudflare.com/pages/platform/functions/#advanced-mode
242
249
*
243
- * When using a _worker.js file, the entire /functions directory is ignored
250
+ * When using a _worker.js file or _worker.js/ directory , the entire /functions directory is ignored
244
251
* – this includes its routing and middleware characteristics.
245
252
*/
246
- if ( _workerJS ) {
253
+ if ( _workerJSIsDirectory ) {
254
+ workerBundle = await traverseAndBuildWorkerJSDirectory ( {
255
+ workerJSDirectory : _workerPath ,
256
+ buildOutputDirectory : directory ,
257
+ d1Databases,
258
+ nodejsCompat,
259
+ } ) ;
260
+ } else if ( _workerJS ) {
247
261
if ( bundle ) {
248
262
const outfile = join ( tmpdir ( ) , `./bundledWorker-${ Math . random ( ) } .mjs` ) ;
249
263
workerBundle = await buildRawWorker ( {
250
- workerScriptPath,
264
+ workerScriptPath : _workerPath ,
251
265
outfile,
252
- directory : directory ?? "." ,
266
+ directory,
253
267
local : false ,
254
268
sourcemap : true ,
255
269
watch : false ,
@@ -258,17 +272,19 @@ export async function publish({
258
272
nodejsCompat,
259
273
} ) ;
260
274
} 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
275
+ await checkRawWorker ( _workerPath , ( ) => { } ) ;
276
+ // TODO: Let users configure this in the future.
263
277
workerBundle = {
264
278
modules : [ ] ,
265
279
dependencies : { } ,
266
280
stop : undefined ,
267
- resolvedEntryPointPath : workerScriptPath ,
281
+ resolvedEntryPointPath : _workerPath ,
268
282
bundleType : "esm" ,
269
283
} ;
270
284
}
285
+ }
271
286
287
+ if ( _workerJS || _workerJSIsDirectory ) {
272
288
const workerBundleContents = await createUploadWorkerBundleContents (
273
289
workerBundle as BundleResult
274
290
) ;
@@ -302,7 +318,7 @@ export async function publish({
302
318
* Pages Functions
303
319
* https://developers.cloudflare.com/pages/platform/functions/
304
320
*/
305
- if ( builtFunctions && ! _workerJS ) {
321
+ if ( builtFunctions && ! _workerJS && ! _workerJSIsDirectory ) {
306
322
const workerBundleContents = await createUploadWorkerBundleContents (
307
323
workerBundle as BundleResult
308
324
) ;
0 commit comments