-
Notifications
You must be signed in to change notification settings - Fork 177
perf: drop babel to reduce the server bundle size
#891
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@opennextjs/aws": patch | ||
| --- | ||
|
|
||
| perf: drop `babel` to reduce the server bundle size | ||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,88 @@ | ||||||
| /** | ||||||
| * Patches to avoid pulling babel (~4MB). | ||||||
| * | ||||||
| * Details: | ||||||
| * - empty `NextServer#runMiddleware` and `NextServer#runEdgeFunction` that are not used | ||||||
| * - drop `next/dist/server/node-environment-extensions/error-inspect.js` | ||||||
| */ | ||||||
|
|
||||||
| import { getCrossPlatformPathRegex } from "utils/regex"; | ||||||
| import { patchCode } from "../astCodePatcher"; | ||||||
| import type { CodePatcher } from "../codePatcher"; | ||||||
|
|
||||||
| export const patchDropBabel: CodePatcher = { | ||||||
| name: "patch-drop-babel", | ||||||
| patches: [ | ||||||
| // Empty the body of `NextServer#runMiddleware` | ||||||
| { | ||||||
| field: { | ||||||
| pathFilter: getCrossPlatformPathRegex( | ||||||
| String.raw`/next/dist/server/next-server\.js$`, | ||||||
| { | ||||||
| escape: false, | ||||||
| }, | ||||||
| ), | ||||||
| contentFilter: /runMiddleware\(/, | ||||||
| patchCode: async ({ code }) => | ||||||
| patchCode(code, createEmptyBodyRule("runMiddleware")), | ||||||
| }, | ||||||
| }, | ||||||
| // Empty the body of `NextServer#runEdgeFunction` | ||||||
| { | ||||||
| field: { | ||||||
| pathFilter: getCrossPlatformPathRegex( | ||||||
| String.raw`/next/dist/server/next-server\.js$`, | ||||||
| { | ||||||
| escape: false, | ||||||
| }, | ||||||
| ), | ||||||
| contentFilter: /runMiddleware\(/, | ||||||
|
||||||
| contentFilter: /runMiddleware\(/, | |
| contentFilter: /runEdgeFunction\(/, |
Outdated
Copilot
AI
Jun 5, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Passing the rule name as a string prevents the actual errorInspectRule from being applied. Use the variable errorInspectRule instead of the literal string.
| patchCode: async ({ code }) => patchCode(code, "errorInspectRule"), | |
| patchCode: async ({ code }) => patchCode(code, errorInspectRule), |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,194 @@ | ||
| import { patchCode } from "@opennextjs/aws/build/patch/astCodePatcher.js"; | ||
| import { | ||
| createEmptyBodyRule, | ||
| errorInspectRule, | ||
| } from "@opennextjs/aws/build/patch/patches/dropBabel.js"; | ||
| import { describe, expect, test } from "vitest"; | ||
|
|
||
| describe("babel-drop", () => { | ||
| test("Drop body", () => { | ||
| const code = ` | ||
| class NextNodeServer extends _baseserver.default { | ||
| constructor(options){ | ||
| // Initialize super class | ||
| super(options); | ||
| this.handleNextImageRequest = async (req, res, parsedUrl) => { /* ... */ }; | ||
| } | ||
| async handleUpgrade() { | ||
| // The web server does not support web sockets, it's only used for HMR in | ||
| // development. | ||
| } | ||
| getEnabledDirectories(dev) { | ||
| const dir = dev ? this.dir : this.serverDistDir; | ||
| return { | ||
| app: (0, _findpagesdir.findDir)(dir, "app") ? true : false, | ||
| pages: (0, _findpagesdir.findDir)(dir, "pages") ? true : false | ||
| }; | ||
| } | ||
| /** | ||
| * This method gets all middleware matchers and execute them when the request | ||
| * matches. It will make sure that each middleware exists and is compiled and | ||
| * ready to be invoked. The development server will decorate it to add warns | ||
| * and errors with rich traces. | ||
| */ async runMiddleware(params) { | ||
| if (process.env.NEXT_MINIMAL) { | ||
| throw new Error('invariant: runMiddleware should not be called in minimal mode'); | ||
| } | ||
| // Middleware is skipped for on-demand revalidate requests | ||
| if ((0, _apiutils.checkIsOnDemandRevalidate)(params.request, this.renderOpts.previewProps).isOnDemandRevalidate) { | ||
| return { | ||
| response: new Response(null, { | ||
| headers: { | ||
| 'x-middleware-next': '1' | ||
| } | ||
| }) | ||
| }; | ||
| } | ||
| // ... | ||
| } | ||
| async runEdgeFunction(params) { | ||
| if (process.env.NEXT_MINIMAL) { | ||
| throw new Error('Middleware is not supported in minimal mode.'); | ||
| } | ||
| let edgeInfo; | ||
| const { query, page, match } = params; | ||
| if (!match) await this.ensureEdgeFunction({ | ||
| page, | ||
| appPaths: params.appPaths, | ||
| url: params.req.url | ||
| }); | ||
| // ... | ||
| } | ||
| // ... | ||
| }`; | ||
|
|
||
| expect( | ||
| patchCode(code, createEmptyBodyRule("runMiddleware")), | ||
| ).toMatchInlineSnapshot(` | ||
| "class NextNodeServer extends _baseserver.default { | ||
| constructor(options){ | ||
| // Initialize super class | ||
| super(options); | ||
| this.handleNextImageRequest = async (req, res, parsedUrl) => { /* ... */ }; | ||
| } | ||
| async handleUpgrade() { | ||
| // The web server does not support web sockets, it's only used for HMR in | ||
| // development. | ||
| } | ||
| getEnabledDirectories(dev) { | ||
| const dir = dev ? this.dir : this.serverDistDir; | ||
| return { | ||
| app: (0, _findpagesdir.findDir)(dir, "app") ? true : false, | ||
| pages: (0, _findpagesdir.findDir)(dir, "pages") ? true : false | ||
| }; | ||
| } | ||
| /** | ||
| * This method gets all middleware matchers and execute them when the request | ||
| * matches. It will make sure that each middleware exists and is compiled and | ||
| * ready to be invoked. The development server will decorate it to add warns | ||
| * and errors with rich traces. | ||
| */ async runMiddleware(params) { | ||
| throw new Error("runMiddleware should not be called with OpenNext"); | ||
| } | ||
| async runEdgeFunction(params) { | ||
| if (process.env.NEXT_MINIMAL) { | ||
| throw new Error('Middleware is not supported in minimal mode.'); | ||
| } | ||
| let edgeInfo; | ||
| const { query, page, match } = params; | ||
| if (!match) await this.ensureEdgeFunction({ | ||
| page, | ||
| appPaths: params.appPaths, | ||
| url: params.req.url | ||
| }); | ||
| // ... | ||
| } | ||
| // ... | ||
| }" | ||
| `); | ||
|
|
||
| expect( | ||
| patchCode(code, createEmptyBodyRule("runEdgeFunction")), | ||
| ).toMatchInlineSnapshot(` | ||
| "class NextNodeServer extends _baseserver.default { | ||
| constructor(options){ | ||
| // Initialize super class | ||
| super(options); | ||
| this.handleNextImageRequest = async (req, res, parsedUrl) => { /* ... */ }; | ||
| } | ||
| async handleUpgrade() { | ||
| // The web server does not support web sockets, it's only used for HMR in | ||
| // development. | ||
| } | ||
| getEnabledDirectories(dev) { | ||
| const dir = dev ? this.dir : this.serverDistDir; | ||
| return { | ||
| app: (0, _findpagesdir.findDir)(dir, "app") ? true : false, | ||
| pages: (0, _findpagesdir.findDir)(dir, "pages") ? true : false | ||
| }; | ||
| } | ||
| /** | ||
| * This method gets all middleware matchers and execute them when the request | ||
| * matches. It will make sure that each middleware exists and is compiled and | ||
| * ready to be invoked. The development server will decorate it to add warns | ||
| * and errors with rich traces. | ||
| */ async runMiddleware(params) { | ||
| if (process.env.NEXT_MINIMAL) { | ||
| throw new Error('invariant: runMiddleware should not be called in minimal mode'); | ||
| } | ||
| // Middleware is skipped for on-demand revalidate requests | ||
| if ((0, _apiutils.checkIsOnDemandRevalidate)(params.request, this.renderOpts.previewProps).isOnDemandRevalidate) { | ||
| return { | ||
| response: new Response(null, { | ||
| headers: { | ||
| 'x-middleware-next': '1' | ||
| } | ||
| }) | ||
| }; | ||
| } | ||
| // ... | ||
| } | ||
| async runEdgeFunction(params) { | ||
| throw new Error("runEdgeFunction should not be called with OpenNext"); | ||
| } | ||
| // ... | ||
| }" | ||
| `); | ||
| }); | ||
|
|
||
| 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 | ||
| }" | ||
| `); | ||
| }); | ||
| }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The changeset only lists
@opennextjs/awsbut thepatchDropBabelchanges live in the Open Next package; the affected package should be included here so the release picks up your updates.