1+ import { getCrossPlatformPathRegex } from "utils/regex.js" ;
12import { createPatchCode } from "../astCodePatcher.js" ;
23import type { CodePatcher } from "../codePatcher.js" ;
34
4- // This rule will replace the `NEXT_MINIMAL` env variable with true in multiple places to avoid executing unwanted path (i.e. next middleware, edge functions and image optimization)
5- export const minimalRule = `
6- rule:
7- kind: member_expression
8- pattern: process.env.NEXT_MINIMAL
9- any:
10- - inside:
11- kind: parenthesized_expression
12- stopBy: end
13- inside:
14- kind: if_statement
15- any:
16- - inside:
17- kind: statement_block
18- inside:
19- kind: method_definition
20- any:
21- - has: {kind: property_identifier, field: name, regex: ^runEdgeFunction$}
22- - has: {kind: property_identifier, field: name, regex: ^runMiddleware$}
23- - has: {kind: property_identifier, field: name, regex: ^imageOptimizer$}
24- - has:
25- kind: statement_block
26- has:
27- kind: expression_statement
28- pattern: res.statusCode = 400;
29- fix:
30- 'true'
31- ` ;
32-
33- // This rule will disable the background preloading of route done by NextServer by default during the creation of NextServer
5+ // Disable the background preloading of route done by NextServer by default during the creation of NextServer
346export const disablePreloadingRule = `
357rule:
368 kind: statement_block
4921 '{}'
5022` ;
5123
52- // This rule is mostly for splitted edge functions so that we don't try to match them on the other non edge functions
24+ // Mostly for splitted edge functions so that we don't try to match them on the other non edge functions
5325export const removeMiddlewareManifestRule = `
5426rule:
5527 kind: statement_block
6234 '{return null;}'
6335` ;
6436
37+ /**
38+ * Swaps the body for a throwing implementation
39+ *
40+ * @param methodName The name of the method
41+ * @returns A rule to replace the body with a `throw`
42+ */
43+ export function createEmptyBodyRule ( methodName : string ) {
44+ return `
45+ rule:
46+ pattern:
47+ selector: method_definition
48+ context: "class { async ${ methodName } ($$$PARAMS) { $$$_ } }"
49+ fix: |-
50+ async ${ methodName } ($$$PARAMS) {
51+ throw new Error("${ methodName } should not be called with OpenNext");
52+ }
53+ ` ;
54+ }
55+
56+ /**
57+ * Drops `require("./node-environment-extensions/error-inspect");`
58+ */
59+ export const errorInspectRule = `
60+ rule:
61+ pattern: require("./node-environment-extensions/error-inspect");
62+ fix: |-
63+ // Removed by OpenNext
64+ // require("./node-environment-extensions/error-inspect");
65+ ` ;
66+
67+ const pathFilter = getCrossPlatformPathRegex (
68+ String . raw `/next/dist/server/next-server\.js$` ,
69+ {
70+ escape : false ,
71+ } ,
72+ ) ;
73+
74+ /**
75+ * Patches to avoid pulling babel (~4MB).
76+ *
77+ * Details:
78+ * - empty `NextServer#runMiddleware` and `NextServer#runEdgeFunction` that are not used
79+ * - drop `next/dist/server/node-environment-extensions/error-inspect.js`
80+ */
81+ const babelPatches = [
82+ // Empty the body of `NextServer#runMiddleware`
83+ {
84+ field : {
85+ pathFilter,
86+ contentFilter : / r u n M i d d l e w a r e \( / ,
87+ patchCode : createPatchCode ( createEmptyBodyRule ( "runMiddleware" ) ) ,
88+ } ,
89+ } ,
90+ // Empty the body of `NextServer#runEdgeFunction`
91+ {
92+ field : {
93+ pathFilter,
94+ contentFilter : / r u n E d g e F u n c t i o n \( / ,
95+ patchCode : createPatchCode ( createEmptyBodyRule ( "runEdgeFunction" ) ) ,
96+ } ,
97+ } ,
98+ // Drop `error-inspect` that pulls babel
99+ {
100+ field : {
101+ pathFilter,
102+ contentFilter : / e r r o r - i n s p e c t / ,
103+ patchCode : createPatchCode ( errorInspectRule ) ,
104+ } ,
105+ } ,
106+ ] ;
107+
65108export const patchNextServer : CodePatcher = {
66109 name : "patch-next-server" ,
67110 patches : [
68- // Skip executing next middleware, edge functions and image optimization inside NextServer
111+ // Empty the body of `NextServer#imageOptimizer` - unused in OpenNext
69112 {
70- versions : ">=15.0.0" ,
71113 field : {
72- pathFilter : / n e x t - s e r v e r \. j s $ / ,
73- contentFilter : / p r o c e s s \. e n v \. N E X T _ M I N I M A L / ,
74- patchCode : createPatchCode ( minimalRule ) ,
114+ pathFilter,
115+ contentFilter : / i m a g e O p t i m i z e r \( / ,
116+ patchCode : createPatchCode ( createEmptyBodyRule ( "imageOptimizer" ) ) ,
75117 } ,
76118 } ,
77- // Disable Next background preloading done at creation of `NetxServer `
119+ // Disable Next background preloading done at creation of `NextServer `
78120 {
79121 versions : ">=15.0.0" ,
80122 field : {
81- pathFilter : / n e x t - s e r v e r \. j s $ / ,
123+ pathFilter,
82124 contentFilter : / t h i s \. n e x t C o n f i g \. e x p e r i m e n t a l \. p r e l o a d E n t r i e s O n S t a r t / ,
83125 patchCode : createPatchCode ( disablePreloadingRule ) ,
84126 } ,
@@ -88,10 +130,11 @@ export const patchNextServer: CodePatcher = {
88130 // Next 12 and some version of 13 use the bundled middleware/edge function
89131 versions : ">=14.0.0" ,
90132 field : {
91- pathFilter : / n e x t - s e r v e r \. j s $ / ,
133+ pathFilter,
92134 contentFilter : / g e t M i d d l e w a r e M a n i f e s t / ,
93135 patchCode : createPatchCode ( removeMiddlewareManifestRule ) ,
94136 } ,
95137 } ,
138+ ...babelPatches ,
96139 ] ,
97140} ;
0 commit comments