Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/twenty-tools-stare.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@opennextjs/aws": patch
---

add(patches): Drop babel from getting pulled in by node-environment
1 change: 1 addition & 0 deletions packages/open-next/src/build/createServerBundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ async function generateBundle(
patches.patchEnvVars,
patches.patchBackgroundRevalidation,
patches.patchUseCacheForISR,
patches.patchNodeEnvironment,
...additionalCodePatches,
]);

Expand Down
1 change: 1 addition & 0 deletions packages/open-next/src/build/patch/patches/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ export {
} from "./patchFetchCacheISR.js";
export { patchFetchCacheSetMissingWaitUntil } from "./patchFetchCacheWaitUntil.js";
export { patchBackgroundRevalidation } from "./patchBackgroundRevalidation.js";
export { patchNodeEnvironment } from "./patchNodeEnvironment.js";
17 changes: 0 additions & 17 deletions packages/open-next/src/build/patch/patches/patchNextServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,6 @@ fix: |-
`;
}

/**
* Drops `require("./node-environment-extensions/error-inspect");`
*/
export const errorInspectRule = `
rule:
pattern: require("./node-environment-extensions/error-inspect");
fix: |-
// Removed by OpenNext
// require("./node-environment-extensions/error-inspect");
`;

const pathFilter = getCrossPlatformPathRegex(
String.raw`/next/dist/server/next-server\.js$`,
{
Expand Down Expand Up @@ -91,12 +80,6 @@ const babelPatches = [
contentFilter: /runEdgeFunction\(/,
patchCode: createPatchCode(createEmptyBodyRule("runEdgeFunction")),
},
// Drop `error-inspect` that pulls babel
{
pathFilter,
contentFilter: /error-inspect/,
patchCode: createPatchCode(errorInspectRule),
},
];

export const patchNextServer: CodePatcher = {
Expand Down
34 changes: 34 additions & 0 deletions packages/open-next/src/build/patch/patches/patchNodeEnvironment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { Lang } from "@ast-grep/napi";
import { getCrossPlatformPathRegex } from "utils/regex.js";
import { createPatchCode } from "../astCodePatcher.js";
import type { CodePatcher } from "../codePatcher.js";

/**
* Drops `require("./node-environment-extensions/error-inspect");`
*
* This is to avoid pulling babel (~4MB)
*/
export const rule = `
rule:
pattern: require("./node-environment-extensions/error-inspect");
fix: |-
// Removed by OpenNext
// require("./node-environment-extensions/error-inspect");
`;

export const patchNodeEnvironment: CodePatcher = {
name: "patch-node-environment-error-inspect",
patches: [
{
pathFilter: getCrossPlatformPathRegex(
String.raw`/next/dist/server/node-environment\.js$`,
{
escape: false,
},
),
contentFilter: /error-inspect/,
patchCode: createPatchCode(rule, Lang.JavaScript),
versions: ">=15.0.0",
},
],
};
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { patchCode } from "@opennextjs/aws/build/patch/astCodePatcher.js";
import {
createEmptyBodyRule,
disablePreloadingRule,
errorInspectRule,
removeMiddlewareManifestRule,
} from "@opennextjs/aws/build/patch/patches/patchNextServer.js";
import { describe, it } from "vitest";
Expand Down Expand Up @@ -943,41 +942,5 @@ class NextNodeServer extends _baseserver.default {
"
`);
});

test("Error Inspect", () => {
const code = `
// This file should be imported before any others. It sets up the environment
// for later imports to work properly.
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
require("./node-environment-baseline");
require("./node-environment-extensions/error-inspect");
require("./node-environment-extensions/random");
require("./node-environment-extensions/date");
require("./node-environment-extensions/web-crypto");
require("./node-environment-extensions/node-crypto");
//# sourceMappingURL=node-environment.js.map
}`;

expect(patchCode(code, errorInspectRule)).toMatchInlineSnapshot(`
"// This file should be imported before any others. It sets up the environment
// for later imports to work properly.
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
require("./node-environment-baseline");
// Removed by OpenNext
// require("./node-environment-extensions/error-inspect");
require("./node-environment-extensions/random");
require("./node-environment-extensions/date");
require("./node-environment-extensions/web-crypto");
require("./node-environment-extensions/node-crypto");
//# sourceMappingURL=node-environment.js.map
}"
`);
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { rule } from "@opennextjs/aws/build/patch/patches/patchNodeEnvironment";
import { computePatchDiff } from "./util.js";

test("nodeEnvironment", () => {
const code = `
// This file should be imported before any others. It sets up the environment
// for later imports to work properly.
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
require("./node-environment-baseline");
require("./node-environment-extensions/error-inspect");
require("./node-environment-extensions/random");
require("./node-environment-extensions/date");
require("./node-environment-extensions/web-crypto");
require("./node-environment-extensions/node-crypto");
if (process.env.NODE_ENV === 'development') {
require('./node-environment-extensions/console-dev');
}

//# sourceMappingURL=node-environment.js.map`;
expect(
computePatchDiff("node-environment.js", code, rule),
).toMatchInlineSnapshot(`
"Index: node-environment.js
===================================================================
--- node-environment.js
+++ node-environment.js
@@ -1,13 +1,13 @@
-
// This file should be imported before any others. It sets up the environment
// for later imports to work properly.
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
require("./node-environment-baseline");
-require("./node-environment-extensions/error-inspect");
+// Removed by OpenNext
+// require("./node-environment-extensions/error-inspect");
require("./node-environment-extensions/random");
require("./node-environment-extensions/date");
require("./node-environment-extensions/web-crypto");
require("./node-environment-extensions/node-crypto");
"
`);
});
Loading