-
Notifications
You must be signed in to change notification settings - Fork 733
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
_worker.js/
directory support in Pages
#2966
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 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,5 @@ | ||
--- | ||
"wrangler": minor | ||
--- | ||
|
||
feat: Add support for the undocumented `_worker.js/` directory in Pages | ||
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 |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"name": "pages-workerjs-directory", | ||
"version": "0.0.0", | ||
"private": true, | ||
"sideEffects": false, | ||
"scripts": { | ||
"check:type": "tsc", | ||
"dev": "npx wrangler pages dev public --port 8794", | ||
"test": "npx vitest", | ||
"test:ci": "npx vitest" | ||
}, | ||
"devDependencies": { | ||
"undici": "^5.9.1" | ||
}, | ||
"engines": { | ||
"node": ">=16.13" | ||
} | ||
} |
Binary file not shown.
23 changes: 23 additions & 0 deletions
23
fixtures/pages-workerjs-directory/public/_worker.js/index.js
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,23 @@ | ||
import staticMod from "./static.js"; | ||
import add from "./add.wasm"; | ||
|
||
export default { | ||
GregBrimble marked this conversation as resolved.
Show resolved
Hide resolved
|
||
async fetch(request, env) { | ||
const { pathname } = new URL(request.url); | ||
|
||
if (pathname === "/wasm") { | ||
const addModule = await WebAssembly.instantiate(add); | ||
return new Response(addModule.exports.add(1, 2).toString()); | ||
} | ||
|
||
if (pathname === "/static") { | ||
return new Response(staticMod); | ||
} | ||
|
||
if (pathname !== "/") { | ||
return new Response((await import(`./${pathname.slice(1)}`)).default); | ||
} | ||
|
||
return env.ASSETS.fetch(request); | ||
}, | ||
}; |
1 change: 1 addition & 0 deletions
1
fixtures/pages-workerjs-directory/public/_worker.js/other-script.js
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 @@ | ||
export default "test"; |
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 @@ | ||
export default "static"; |
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 @@ | ||
<h1>Hello, world!</h1> |
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,45 @@ | ||
import { execSync } from "node:child_process"; | ||
import { readFileSync } from "node:fs"; | ||
import { tmpdir } from "node:os"; | ||
import path, { join, resolve } from "node:path"; | ||
import { fetch } from "undici"; | ||
import { describe, it } from "vitest"; | ||
import { runWranglerPagesDev } from "../../shared/src/run-wrangler-long-lived"; | ||
|
||
describe.concurrent("Pages _worker.js/ directory", () => { | ||
it("should support non-bundling with 'dev'", async ({ expect }) => { | ||
const { ip, port, stop } = await runWranglerPagesDev( | ||
resolve(__dirname, ".."), | ||
"public", | ||
["--port=0"] | ||
); | ||
await expect( | ||
fetch(`http://${ip}:${port}/`).then((resp) => resp.text()) | ||
).resolves.toContain("Hello, world!"); | ||
await expect( | ||
fetch(`http://${ip}:${port}/wasm`).then((resp) => resp.text()) | ||
).resolves.toContain("3"); | ||
await expect( | ||
fetch(`http://${ip}:${port}/static`).then((resp) => resp.text()) | ||
).resolves.toContain("static"); | ||
await expect( | ||
fetch(`http://${ip}:${port}/other-script`).then((resp) => resp.text()) | ||
).resolves.toContain("test"); | ||
await stop(); | ||
}); | ||
|
||
it("should bundle", async ({ expect }) => { | ||
const dir = tmpdir(); | ||
const file = join(dir, "./_worker.bundle"); | ||
|
||
execSync( | ||
`npx wrangler pages functions build --build-output-directory public --outfile ${file} --bindings="{\\"d1_databases\\":{\\"FOO\\":{}}}"`, | ||
{ | ||
cwd: path.resolve(__dirname, ".."), | ||
} | ||
); | ||
|
||
expect(readFileSync(file, "utf-8")).toContain("D1_ERROR"); | ||
expect(readFileSync(file, "utf-8")).toContain('"static"'); | ||
}); | ||
}); |
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,12 @@ | ||
{ | ||
"compilerOptions": { | ||
"target": "ES2020", | ||
"esModuleInterop": true, | ||
"module": "CommonJS", | ||
"lib": ["ES2020"], | ||
"types": ["node"], | ||
"moduleResolution": "node", | ||
"noEmit": true | ||
}, | ||
"include": ["tests", "../../node-types.d.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 |
---|---|---|
|
@@ -449,4 +449,74 @@ export default { | |
hello.js:2:36: ERROR: Could not resolve \\"node:async_hooks\\"" | ||
`); | ||
}); | ||
|
||
it("should compile a _worker.js/ directory", async () => { | ||
mkdirSync("public"); | ||
mkdirSync("public/_worker.js"); | ||
writeFileSync( | ||
"public/_worker.js/index.js", | ||
` | ||
import { cat } from "./cat.js"; | ||
|
||
export default { | ||
async fetch(request, env) { | ||
return new Response("Hello from _worker.js/index.js" + cat); | ||
}, | ||
};` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
); | ||
writeFileSync( | ||
"public/_worker.js/cat.js", | ||
` | ||
export const cat = "cat";` | ||
); | ||
|
||
await runWrangler(`pages functions build --outfile=public/_worker.bundle`); | ||
|
||
expect(existsSync("public/_worker.bundle")).toBe(true); | ||
expect(std.out).toMatchInlineSnapshot(` | ||
"🚧 'wrangler pages <command>' is a beta command. Please report any issues to https://github.com/cloudflare/workers-sdk/issues/new/choose | ||
✨ Compiled Worker successfully" | ||
`); | ||
|
||
const workerBundleContents = readFileSync("public/_worker.bundle", "utf-8"); | ||
const workerBundleWithConstantData = replaceRandomWithConstantData( | ||
workerBundleContents, | ||
[ | ||
[/------formdata-undici-0.[0-9]*/g, "------formdata-undici-0.test"], | ||
[/bundledWorker-0.[0-9]*.mjs/g, "bundledWorker-0.test.mjs"], | ||
[/bundledWorker-0.[0-9]*.map/g, "bundledWorker-0.test.map"], | ||
] | ||
); | ||
|
||
expect(workerBundleWithConstantData).toMatchInlineSnapshot(` | ||
"------formdata-undici-0.test | ||
Content-Disposition: form-data; name=\\"metadata\\" | ||
|
||
{\\"main_module\\":\\"bundledWorker-0.test.mjs\\"} | ||
------formdata-undici-0.test | ||
Content-Disposition: form-data; name=\\"bundledWorker-0.test.mjs\\"; filename=\\"bundledWorker-0.test.mjs\\" | ||
Content-Type: application/javascript+module | ||
|
||
import { cat } from \\"./cat.js\\"; | ||
var worker_default = { | ||
async fetch(request, env) { | ||
return new Response(\\"Hello from _worker.js/index.js\\" + cat); | ||
} | ||
}; | ||
export { | ||
worker_default as default | ||
}; | ||
//# sourceMappingURL=bundledWorker-0.test.mjs.map | ||
|
||
------formdata-undici-0.test | ||
Content-Disposition: form-data; name=\\"cat.js\\"; filename=\\"cat.js\\" | ||
Content-Type: application/javascript+module | ||
|
||
|
||
export const cat = \\"cat\\"; | ||
------formdata-undici-0.test--" | ||
`); | ||
|
||
expect(std.err).toMatchInlineSnapshot(`""`); | ||
}); | ||
}); |
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
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
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.
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.
Should it be documented?