Skip to content

Commit c50c133

Browse files
committed
refactor
1 parent f2bcf13 commit c50c133

File tree

9 files changed

+63
-56
lines changed

9 files changed

+63
-56
lines changed

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@ import { type CodePatcher, applyCodePatches } from "./patch/codePatcher.js";
2020
import {
2121
patchFetchCacheForISR,
2222
patchUnstableCacheForISR,
23-
} from "./patch/patchFetchCacheISR.js";
24-
import { patchFetchCacheSetMissingWaitUntil } from "./patch/patchFetchCacheWaitUntil.js";
25-
import { patchEnvVars, patchNextServer } from "./patch/patchNextServer.js";
23+
patchNextServer,
24+
patchEnvVars,
25+
patchFetchCacheSetMissingWaitUntil,
26+
} from "./patch/patches/index.js";
2627

2728
interface CodeCustomization {
2829
// These patches are meant to apply on user and next generated code
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export * from "./patchEnvVar.js";
2+
export * from "./patchNextServer.js";
3+
export * from "./patchFetchCacheISR.js";
4+
export * from "./patchFetchCacheWaitUntil.js";
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { createPatchCode } from "../astCodePatcher.js";
2+
import type { CodePatcher } from "../codePatcher";
3+
4+
export const envVarRuleCreator = (envVar: string, value: string) => `
5+
rule:
6+
kind: member_expression
7+
pattern: process.env.${envVar}
8+
inside:
9+
kind: if_statement
10+
stopBy: end
11+
fix:
12+
'${value}'
13+
`;
14+
15+
export const patchEnvVars: CodePatcher = {
16+
name: "patch-env-vars",
17+
patches: [
18+
{
19+
versions: ">=15.0.0",
20+
field: {
21+
pathFilter: /module\.compiled\.js$/,
22+
contentFilter: /process\.env\.NEXT_RUNTIME/,
23+
patchCode: createPatchCode(envVarRuleCreator("NEXT_RUNTIME", '"node"')),
24+
},
25+
},
26+
{
27+
versions: ">=15.0.0",
28+
field: {
29+
pathFilter:
30+
/(module\.compiled|react\/index|react\/jsx-runtime|react-dom\/index)\.js$/,
31+
contentFilter: /process\.env\.NODE_ENV/,
32+
patchCode: createPatchCode(
33+
envVarRuleCreator("NODE_ENV", '"production"'),
34+
),
35+
},
36+
},
37+
{
38+
versions: ">=15.0.0",
39+
field: {
40+
pathFilter: /module\.compiled\.js$/,
41+
contentFilter: /process\.env\.TURBOPACK/,
42+
patchCode: createPatchCode(envVarRuleCreator("TURBOPACK", "false")),
43+
},
44+
},
45+
],
46+
};

packages/open-next/src/build/patch/patchFetchCacheISR.ts renamed to packages/open-next/src/build/patch/patches/patchFetchCacheISR.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Lang } from "@ast-grep/napi";
22
import { getCrossPlatformPathRegex } from "utils/regex.js";
3-
import { createPatchCode } from "./astCodePatcher.js";
4-
import type { CodePatcher } from "./codePatcher";
3+
import { createPatchCode } from "../astCodePatcher.js";
4+
import type { CodePatcher } from "../codePatcher.js";
55

66
export const fetchRule = `
77
rule:

packages/open-next/src/build/patch/patchFetchCacheWaitUntil.ts renamed to packages/open-next/src/build/patch/patches/patchFetchCacheWaitUntil.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { getCrossPlatformPathRegex } from "utils/regex.js";
2-
import { createPatchCode } from "./astCodePatcher.js";
3-
import type { CodePatcher } from "./codePatcher";
2+
import { createPatchCode } from "../astCodePatcher.js";
3+
import type { CodePatcher } from "../codePatcher.js";
44

55
export const rule = `
66
rule:

packages/open-next/src/build/patch/patchNextServer.ts renamed to packages/open-next/src/build/patch/patches/patchNextServer.ts

Lines changed: 2 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { createPatchCode } from "./astCodePatcher.js";
2-
import type { CodePatcher } from "./codePatcher";
1+
import { createPatchCode } from "../astCodePatcher.js";
2+
import type { CodePatcher } from "../codePatcher.js";
33

44
export const minimalRule = `
55
rule:
@@ -60,17 +60,6 @@ fix:
6060
'{return null;}'
6161
`;
6262

63-
export const envVarRuleCreator = (envVar: string, value: string) => `
64-
rule:
65-
kind: member_expression
66-
pattern: process.env.${envVar}
67-
inside:
68-
kind: if_statement
69-
stopBy: end
70-
fix:
71-
'${value}'
72-
`;
73-
7463
export const patchNextServer: CodePatcher = {
7564
name: "patch-next-server",
7665
patches: [
@@ -100,36 +89,3 @@ export const patchNextServer: CodePatcher = {
10089
},
10190
],
10291
};
103-
104-
export const patchEnvVars: CodePatcher = {
105-
name: "patch-env-vars",
106-
patches: [
107-
{
108-
versions: ">=15.0.0",
109-
field: {
110-
pathFilter: /module\.compiled\.js$/,
111-
contentFilter: /process\.env\.NEXT_RUNTIME/,
112-
patchCode: createPatchCode(envVarRuleCreator("NEXT_RUNTIME", '"node"')),
113-
},
114-
},
115-
{
116-
versions: ">=15.0.0",
117-
field: {
118-
pathFilter:
119-
/(module\.compiled|react\/index|react\/jsx-runtime|react-dom\/index)\.js$/,
120-
contentFilter: /process\.env\.NODE_ENV/,
121-
patchCode: createPatchCode(
122-
envVarRuleCreator("NODE_ENV", '"production"'),
123-
),
124-
},
125-
},
126-
{
127-
versions: ">=15.0.0",
128-
field: {
129-
pathFilter: /module\.compiled\.js$/,
130-
contentFilter: /process\.env\.TURBOPACK/,
131-
patchCode: createPatchCode(envVarRuleCreator("TURBOPACK", "false")),
132-
},
133-
},
134-
],
135-
};

packages/tests-unit/tests/build/patch/patchFetchCacheISR.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { patchCode } from "@opennextjs/aws/build/patch/astCodePatcher.js";
22
import {
33
fetchRule,
44
unstable_cacheRule,
5-
} from "@opennextjs/aws/build/patch/patchFetchCacheISR.js";
5+
} from "@opennextjs/aws/build/patch/patches/patchFetchCacheISR.js";
66
import { describe } from "vitest";
77

88
const unstable_cacheCode = `

packages/tests-unit/tests/build/patch/patchFetchCacheWaitUntil.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { describe, expect, test } from "vitest";
22

33
import { patchCode } from "@opennextjs/aws/build/patch/astCodePatcher.js";
4-
import { rule } from "@opennextjs/aws/build/patch/patchFetchCacheWaitUntil.js";
4+
import { rule } from "@opennextjs/aws/build/patch/patches/patchFetchCacheWaitUntil.js";
55

66
describe("patchFetchCacheSetMissingWaitUntil", () => {
77
test("on minified code", () => {

packages/tests-unit/tests/build/patch/patchNextServer.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {
33
removeMiddlewareManifestRule,
44
minimalRule,
55
disablePreloadingRule,
6-
} from "@opennextjs/aws/build/patch/patchNextServer.js";
6+
} from "@opennextjs/aws/build/patch/patches/patchNextServer.js";
77
import { it, describe } from "vitest";
88

99
const nextServerGetMiddlewareManifestCode = `

0 commit comments

Comments
 (0)