-
Notifications
You must be signed in to change notification settings - Fork 73
refactor: patch the webpack runtime using ast-grep #351
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
4d9519f
refactor: patch the webpack runtime using ast-grep
vicb c391c21
Update packages/cloudflare/src/cli/build/bundle-server.ts
vicb 85b783f
Update packages/cloudflare/src/cli/build/patches/ast/webpack-runtime.ts
vicb 18ad6b1
fixup! restrict context
vicb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
64 changes: 64 additions & 0 deletions
64
packages/cloudflare/src/cli/build/patches/ast/webpack-runtime.spec.ts
This file contains hidden or 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,64 @@ | ||
import { describe, expect, test } from "vitest"; | ||
|
||
import { patchCode } from "./util.js"; | ||
import { buildInlineChunksRule } from "./webpack-runtime.js"; | ||
|
||
describe("webpack runtime", () => { | ||
test("patch runtime", () => { | ||
const code = ` | ||
/******/ // require() chunk loading for javascript | ||
/******/ __webpack_require__.f.require = (chunkId, promises) => { | ||
/******/ // "1" is the signal for "already loaded" | ||
/******/ if (!installedChunks[chunkId]) { | ||
/******/ if (658 != chunkId) { | ||
/******/ installChunk(require("./chunks/" + __webpack_require__.u(chunkId))); | ||
/******/ | ||
} else installedChunks[chunkId] = 1; | ||
/******/ | ||
} | ||
/******/ | ||
}; | ||
`; | ||
|
||
expect(patchCode(code, buildInlineChunksRule([1, 2, 3]))).toMatchInlineSnapshot(` | ||
"/******/ // require() chunk loading for javascript | ||
/******/ __webpack_require__.f.require = (chunkId, _) => { | ||
if (!installedChunks[chunkId]) { | ||
switch (chunkId) { | ||
case 1: installChunk(require("./chunks/1.js")); break; | ||
case 2: installChunk(require("./chunks/2.js")); break; | ||
case 3: installChunk(require("./chunks/3.js")); break; | ||
case 658: installedChunks[chunkId] = 1; break; | ||
default: throw new Error(\`Unknown chunk \${chunkId}\`); | ||
} | ||
} | ||
} | ||
; | ||
" | ||
`); | ||
}); | ||
|
||
test("patch minified runtime", () => { | ||
const code = ` | ||
t.f.require=(o,n)=>{e[o]||(658!=o?r(require("./chunks/"+t.u(o))):e[o]=1)} | ||
`; | ||
|
||
expect(patchCode(code, buildInlineChunksRule([1, 2, 3]))).toMatchInlineSnapshot( | ||
` | ||
"t.f.require=(o, _) => { | ||
if (!e[o]) { | ||
switch (o) { | ||
case 1: r(require("./chunks/1.js")); break; | ||
case 2: r(require("./chunks/2.js")); break; | ||
case 3: r(require("./chunks/3.js")); break; | ||
case 658: e[o] = 1; break; | ||
default: throw new Error(\`Unknown chunk \${o}\`); | ||
} | ||
} | ||
} | ||
|
||
" | ||
` | ||
); | ||
}); | ||
}); |
70 changes: 70 additions & 0 deletions
70
packages/cloudflare/src/cli/build/patches/ast/webpack-runtime.ts
This file contains hidden or 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,70 @@ | ||
/** | ||
* Inline dynamic requires in the webpack runtime. | ||
* | ||
* The webpack runtime has dynamic requires that would not be bundled by ESBuild: | ||
* | ||
* installChunk(require("./chunks/" + __webpack_require__.u(chunkId))); | ||
* | ||
* This patch unrolls the dynamic require for all the existing chunks: | ||
* | ||
* switch (chunkId) { | ||
* case ID1: installChunk(require("./chunks/ID1"); break; | ||
* case ID2: installChunk(require("./chunks/ID2"); break; | ||
* // ... | ||
* case SELF_ID: installedChunks[chunkId] = 1; break; | ||
* default: throw new Error(`Unknown chunk ${chunkId}`); | ||
* } | ||
*/ | ||
|
||
import { readdirSync, readFileSync, writeFileSync } from "node:fs"; | ||
import { join } from "node:path"; | ||
|
||
import { type BuildOptions, getPackagePath } from "@opennextjs/aws/build/helper.js"; | ||
|
||
import { patchCode } from "./util.js"; | ||
|
||
export function buildInlineChunksRule(chunks: number[]) { | ||
return ` | ||
rule: | ||
pattern: ($CHUNK_ID, $_PROMISES) => { $$$ } | ||
inside: {pattern: $_.$_.require = $$$_, stopBy: end} | ||
all: | ||
- has: {pattern: $INSTALL(require("./chunks/" + $$$)), stopBy: end} | ||
- has: {pattern: $SELF_ID != $CHUNK_ID, stopBy: end} | ||
- has: {pattern: "$INSTALLED_CHUNK[$CHUNK_ID] = 1", stopBy: end} | ||
fix: | | ||
($CHUNK_ID, _) => { | ||
if (!$INSTALLED_CHUNK[$CHUNK_ID]) { | ||
switch ($CHUNK_ID) { | ||
${chunks.map((chunk) => ` case ${chunk}: $INSTALL(require("./chunks/${chunk}.js")); break;`).join("\n")} | ||
case $SELF_ID: $INSTALLED_CHUNK[$CHUNK_ID] = 1; break; | ||
default: throw new Error(\`Unknown chunk \${$CHUNK_ID}\`); | ||
} | ||
} | ||
}`; | ||
} | ||
|
||
/** | ||
* Fixes the webpack-runtime.js file by removing its webpack dynamic requires. | ||
*/ | ||
export async function patchWebpackRuntime(buildOpts: BuildOptions) { | ||
const { outputDir } = buildOpts; | ||
|
||
const dotNextServerDir = join( | ||
outputDir, | ||
"server-functions/default", | ||
getPackagePath(buildOpts), | ||
".next/server" | ||
); | ||
|
||
const runtimeFile = join(dotNextServerDir, "webpack-runtime.js"); | ||
const runtimeCode = readFileSync(runtimeFile, "utf-8"); | ||
// Look for all the chunks. | ||
const chunks = readdirSync(join(dotNextServerDir, "chunks")) | ||
.filter((chunk) => /^\d+\.js$/.test(chunk)) | ||
.map((chunk) => { | ||
return Number(chunk.replace(/\.js$/, "")); | ||
}); | ||
|
||
writeFileSync(runtimeFile, patchCode(runtimeCode, buildInlineChunksRule(chunks))); | ||
} |
1 change: 0 additions & 1 deletion
1
packages/cloudflare/src/cli/build/patches/investigated/index.ts
This file contains hidden or 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,4 +1,3 @@ | ||
export * from "./copy-package-cli-files.js"; | ||
export * from "./patch-cache.js"; | ||
export * from "./patch-require.js"; | ||
export * from "./update-webpack-chunks-file/index.js"; |
30 changes: 0 additions & 30 deletions
30
...atches/investigated/update-webpack-chunks-file/get-chunk-installation-identifiers.test.ts
This file was deleted.
Oops, something went wrong.
102 changes: 0 additions & 102 deletions
102
...ild/patches/investigated/update-webpack-chunks-file/get-chunk-installation-identifiers.ts
This file was deleted.
Oops, something went wrong.
46 changes: 0 additions & 46 deletions
46
...d/update-webpack-chunks-file/get-file-content-with-updated-webpack-f-require-code.test.ts
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.