-
Notifications
You must be signed in to change notification settings - Fork 552
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
_worker.js/
directory support in Pages (#2966)
* `_worker.js/` directory support in Pages * Refactor some of the shared no bundle logic and address misc PR comments
- Loading branch information
1 parent
98e6630
commit e351afc
Showing
22 changed files
with
438 additions
and
142 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
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 { | ||
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
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.