@@ -4,14 +4,16 @@ import path from "node:path";
4
4
import { fileURLToPath } from "node:url" ;
5
5
6
6
import { type BuildOptions , getPackagePath } from "@opennextjs/aws/build/helper.js" ;
7
- import { getCrossPlatformPathRegex } from "@opennextjs/aws/utils/regex.js" ;
8
- import { build , Plugin } from "esbuild" ;
7
+ import { build } from "esbuild" ;
9
8
10
9
import { patchVercelOgLibrary } from "./patches/ast/patch-vercel-og-library.js" ;
11
10
import { patchWebpackRuntime } from "./patches/ast/webpack-runtime.js" ;
12
11
import * as patches from "./patches/index.js" ;
12
+ import { ContentUpdater } from "./patches/plugins/content-updater.js" ;
13
+ import { patchLoadInstrumentation } from "./patches/plugins/load-instrumentation.js" ;
13
14
import { handleOptionalDependencies } from "./patches/plugins/optional-deps.js" ;
14
15
import { fixRequire } from "./patches/plugins/require.js" ;
16
+ import { shimRequireHook } from "./patches/plugins/require-hook.js" ;
15
17
import { inlineRequirePagePlugin } from "./patches/plugins/require-page.js" ;
16
18
import { setWranglerExternal } from "./patches/plugins/wrangler-external.js" ;
17
19
import { normalizePath , patchCodeWithValidations } from "./utils/index.js" ;
@@ -58,6 +60,8 @@ export async function bundleServer(buildOpts: BuildOptions): Promise<void> {
58
60
const openNextServer = path . join ( outputPath , packagePath , `index.mjs` ) ;
59
61
const openNextServerBundle = path . join ( outputPath , packagePath , `handler.mjs` ) ;
60
62
63
+ const updater = new ContentUpdater ( ) ;
64
+
61
65
const result = await build ( {
62
66
entryPoints : [ openNextServer ] ,
63
67
bundle : true ,
@@ -77,11 +81,14 @@ export async function bundleServer(buildOpts: BuildOptions): Promise<void> {
77
81
// - ESBuild `node` platform: https://esbuild.github.io/api/#platform
78
82
conditions : [ ] ,
79
83
plugins : [
80
- createFixRequiresESBuildPlugin ( buildOpts ) ,
81
- inlineRequirePagePlugin ( buildOpts ) ,
84
+ shimRequireHook ( buildOpts ) ,
85
+ inlineRequirePagePlugin ( updater , buildOpts ) ,
82
86
setWranglerExternal ( ) ,
83
- fixRequire ( ) ,
87
+ fixRequire ( updater ) ,
84
88
handleOptionalDependencies ( optionalDependencies ) ,
89
+ patchLoadInstrumentation ( updater ) ,
90
+ // Apply updater updaters, must be the last plugin
91
+ updater . plugin ,
85
92
] ,
86
93
external : [ "./middleware/handler.mjs" ] ,
87
94
alias : {
@@ -192,7 +199,6 @@ export async function updateWorkerBundledCode(
192
199
( code ) => patches . inlineMiddlewareManifestRequire ( code , buildOpts ) ,
193
200
] ,
194
201
[ "exception bubbling" , patches . patchExceptionBubbling ] ,
195
- [ "`loadInstrumentationModule` function" , patches . patchLoadInstrumentationModule ] ,
196
202
[
197
203
"`patchAsyncStorage` call" ,
198
204
( code ) =>
@@ -210,21 +216,6 @@ export async function updateWorkerBundledCode(
210
216
await writeFile ( workerOutputFile , patchedCode ) ;
211
217
}
212
218
213
- function createFixRequiresESBuildPlugin ( options : BuildOptions ) : Plugin {
214
- return {
215
- name : "replaceRelative" ,
216
- setup ( build ) {
217
- // Note: we (empty) shim require-hook modules as they generate problematic code that uses requires
218
- build . onResolve (
219
- { filter : getCrossPlatformPathRegex ( String . raw `^\./require-hook$` , { escape : false } ) } ,
220
- ( ) => ( {
221
- path : path . join ( options . outputDir , "cloudflare-templates/shims/empty.js" ) ,
222
- } )
223
- ) ;
224
- } ,
225
- } ;
226
- }
227
-
228
219
/**
229
220
* Gets the path of the worker.js file generated by the build process
230
221
*
0 commit comments