@@ -14,10 +14,10 @@ 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" ;
20
- import traverseModuleGraph from "../../traverse-module-graph" ;
21
21
import { createUploadWorkerBundleContents } from "./create-worker-bundle-contents" ;
22
22
import type { BundleResult } from "../../bundle" ;
23
23
import type { Project , Deployment } from "@cloudflare/types" ;
@@ -66,7 +66,7 @@ interface PagesPublishOptions {
66
66
67
67
/**
68
68
* Whether to run bundling on `_worker.js` before deploying.
69
- * Default: false
69
+ * Default: true
70
70
*/
71
71
bundle ?: boolean ;
72
72
@@ -96,9 +96,11 @@ export async function publish({
96
96
_redirects : string | undefined ,
97
97
_routesGenerated : string | undefined ,
98
98
_routesCustom : string | undefined ,
99
- _workerJSDirectory = false ,
99
+ _workerJSIsDirectory = false ,
100
100
_workerJS : string | undefined ;
101
101
102
+ bundle = bundle ?? true ;
103
+
102
104
const _workerPath = resolvePath ( directory , "_worker.js" ) ;
103
105
104
106
try {
@@ -118,8 +120,8 @@ export async function publish({
118
120
} catch { }
119
121
120
122
try {
121
- _workerJSDirectory = lstatSync ( _workerPath ) . isDirectory ( ) ;
122
- if ( ! _workerJSDirectory ) {
123
+ _workerJSIsDirectory = lstatSync ( _workerPath ) . isDirectory ( ) ;
124
+ if ( ! _workerJSIsDirectory ) {
123
125
_workerJS = readFileSync ( _workerPath , "utf-8" ) ;
124
126
}
125
127
} catch { }
@@ -245,51 +247,16 @@ export async function publish({
245
247
* Advanced Mode
246
248
* https://developers.cloudflare.com/pages/platform/functions/#advanced-mode
247
249
*
248
- * 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
249
251
* – this includes its routing and middleware characteristics.
250
252
*/
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 ,
253
+ if ( _workerJSIsDirectory ) {
254
+ workerBundle = await traverseAndBuildWorkerJSDirectory ( {
255
+ workerJSDirectory : _workerPath ,
256
+ buildOutputDirectory : directory ,
257
+ d1Databases,
278
258
nodejsCompat,
279
259
} ) ;
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
260
} else if ( _workerJS ) {
294
261
if ( bundle ) {
295
262
const outfile = join ( tmpdir ( ) , `./bundledWorker-${ Math . random ( ) } .mjs` ) ;
@@ -317,7 +284,7 @@ export async function publish({
317
284
}
318
285
}
319
286
320
- if ( _workerJS || _workerJSDirectory ) {
287
+ if ( _workerJS || _workerJSIsDirectory ) {
321
288
const workerBundleContents = await createUploadWorkerBundleContents (
322
289
workerBundle as BundleResult
323
290
) ;
@@ -351,7 +318,7 @@ export async function publish({
351
318
* Pages Functions
352
319
* https://developers.cloudflare.com/pages/platform/functions/
353
320
*/
354
- if ( builtFunctions && ! _workerJS ) {
321
+ if ( builtFunctions && ! _workerJS && ! _workerJSIsDirectory ) {
355
322
const workerBundleContents = await createUploadWorkerBundleContents (
356
323
workerBundle as BundleResult
357
324
) ;
0 commit comments