Skip to content

Commit f837514

Browse files
authored
refactor: merge dropBabel into patchNextServer (#894)
1 parent 0e25eb1 commit f837514

File tree

6 files changed

+866
-686
lines changed

6 files changed

+866
-686
lines changed

packages/open-next/src/build/createServerBundle.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,6 @@ async function generateBundle(
206206
patches.patchEnvVars,
207207
patches.patchBackgroundRevalidation,
208208
patches.patchUseCacheForISR,
209-
patches.patchDropBabel,
210209
...additionalCodePatches,
211210
]);
212211

packages/open-next/src/build/patch/patches/dropBabel.ts

Lines changed: 0 additions & 88 deletions
This file was deleted.

packages/open-next/src/build/patch/patches/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,3 @@ export {
77
} from "./patchFetchCacheISR.js";
88
export { patchFetchCacheSetMissingWaitUntil } from "./patchFetchCacheWaitUntil.js";
99
export { patchBackgroundRevalidation } from "./patchBackgroundRevalidation.js";
10-
export { patchDropBabel } from "./dropBabel.js";
Lines changed: 82 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,8 @@
1+
import { getCrossPlatformPathRegex } from "utils/regex.js";
12
import { createPatchCode } from "../astCodePatcher.js";
23
import 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
346
export const disablePreloadingRule = `
357
rule:
368
kind: statement_block
@@ -49,7 +21,7 @@ fix:
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
5325
export const removeMiddlewareManifestRule = `
5426
rule:
5527
kind: statement_block
@@ -62,23 +34,93 @@ fix:
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: /runMiddleware\(/,
87+
patchCode: createPatchCode(createEmptyBodyRule("runMiddleware")),
88+
},
89+
},
90+
// Empty the body of `NextServer#runEdgeFunction`
91+
{
92+
field: {
93+
pathFilter,
94+
contentFilter: /runEdgeFunction\(/,
95+
patchCode: createPatchCode(createEmptyBodyRule("runEdgeFunction")),
96+
},
97+
},
98+
// Drop `error-inspect` that pulls babel
99+
{
100+
field: {
101+
pathFilter,
102+
contentFilter: /error-inspect/,
103+
patchCode: createPatchCode(errorInspectRule),
104+
},
105+
},
106+
];
107+
65108
export 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: /next-server\.js$/,
73-
contentFilter: /process\.env\.NEXT_MINIMAL/,
74-
patchCode: createPatchCode(minimalRule),
114+
pathFilter,
115+
contentFilter: /imageOptimizer\(/,
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: /next-server\.js$/,
123+
pathFilter,
82124
contentFilter: /this\.nextConfig\.experimental\.preloadEntriesOnStart/,
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: /next-server\.js$/,
133+
pathFilter,
92134
contentFilter: /getMiddlewareManifest/,
93135
patchCode: createPatchCode(removeMiddlewareManifestRule),
94136
},
95137
},
138+
...babelPatches,
96139
],
97140
};

0 commit comments

Comments
 (0)