-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: patch getBuildId using ast-grep (#401)
- Loading branch information
Showing
8 changed files
with
120 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
export * from "./investigated/index.js"; | ||
export * from "./to-investigate/index.js"; | ||
export * from "./to-investigate/inline-middleware-manifest.js"; |
31 changes: 31 additions & 0 deletions
31
packages/cloudflare/src/cli/build/patches/plugins/build-id.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/** | ||
* Inline `getBuildId` as it relies on `readFileSync` that is not supported by workerd. | ||
*/ | ||
|
||
import { getCrossPlatformPathRegex } from "@opennextjs/aws/utils/regex.js"; | ||
|
||
import { patchCode } from "../ast/util.js"; | ||
import type { ContentUpdater } from "./content-updater.js"; | ||
|
||
export function inlineBuildId(updater: ContentUpdater) { | ||
return updater.updateContent( | ||
"inline-build-id", | ||
{ | ||
filter: getCrossPlatformPathRegex(String.raw`/next/dist/server/next-server\.js$`, { escape: false }), | ||
contentFilter: /getBuildId\(/, | ||
}, | ||
async ({ contents }) => patchCode(contents, rule) | ||
); | ||
} | ||
|
||
export const rule = ` | ||
rule: | ||
kind: method_definition | ||
has: | ||
field: name | ||
regex: ^getBuildId$ | ||
fix: |- | ||
getBuildId() { | ||
return process.env.NEXT_BUILD_ID; | ||
} | ||
`; |
85 changes: 85 additions & 0 deletions
85
packages/cloudflare/src/cli/build/patches/plugins/find-dir.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import { describe, expect, test } from "vitest"; | ||
|
||
import { patchCode } from "../ast/util.js"; | ||
import { rule } from "./build-id.js"; | ||
|
||
describe("getBuildId", () => { | ||
test("patch", () => { | ||
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. | ||
} | ||
loadEnvConfig({ dev, forceReload, silent }) { | ||
(0, _env.loadEnvConfig)(this.dir, dev, silent ? { | ||
info: ()=>{}, | ||
error: ()=>{} | ||
} : _log, forceReload); | ||
} | ||
async hasPage(pathname) { | ||
var _this_nextConfig_i18n; | ||
return !!(0, _require.getMaybePagePath)(pathname, this.distDir, (_this_nextConfig_i18n = this.nextConfig.i18n) == null ? void 0 : _this_nextConfig_i18n.locales, this.enabledDirectories.app); | ||
} | ||
getBuildId() { | ||
const buildIdFile = (0, _path.join)(this.distDir, _constants.BUILD_ID_FILE); | ||
try { | ||
return _fs.default.readFileSync(buildIdFile, "utf8").trim(); | ||
} catch (err) { | ||
if (err.code === "ENOENT") { | ||
throw new Error(\`Could not find a production build in the '\${this.distDir}' directory. Try building your app with 'next build' before starting the production server. https://nextjs.org/docs/messages/production-start-no-build-id\`); | ||
} | ||
throw err; | ||
} | ||
} | ||
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 | ||
}; | ||
} | ||
// ... | ||
}`; | ||
|
||
expect(patchCode(code, rule)).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. | ||
} | ||
loadEnvConfig({ dev, forceReload, silent }) { | ||
(0, _env.loadEnvConfig)(this.dir, dev, silent ? { | ||
info: ()=>{}, | ||
error: ()=>{} | ||
} : _log, forceReload); | ||
} | ||
async hasPage(pathname) { | ||
var _this_nextConfig_i18n; | ||
return !!(0, _require.getMaybePagePath)(pathname, this.distDir, (_this_nextConfig_i18n = this.nextConfig.i18n) == null ? void 0 : _this_nextConfig_i18n.locales, this.enabledDirectories.app); | ||
} | ||
getBuildId() { | ||
return process.env.NEXT_BUILD_ID; | ||
} | ||
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 | ||
}; | ||
} | ||
// ... | ||
}" | ||
`); | ||
}); | ||
}); |
2 changes: 1 addition & 1 deletion
2
packages/cloudflare/src/cli/build/patches/plugins/load-manifest.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 0 additions & 2 deletions
2
packages/cloudflare/src/cli/build/patches/to-investigate/index.ts
This file was deleted.
Oops, something went wrong.
File renamed without changes.
11 changes: 0 additions & 11 deletions
11
packages/cloudflare/src/cli/build/patches/to-investigate/patch-read-file.ts
This file was deleted.
Oops, something went wrong.