-
Notifications
You must be signed in to change notification settings - Fork 734
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: correctly handle entry-point path when publishing
The `publish` command was failing when the entry-point was specified in the wrangler.toml file and the entry-point imported another file. This was because we were using the `metafile.inputs` to guess the entry-point file path. But the order in which the source-files were added to this object was not well defined, and so we could end up failing to find a match. This fix avoids this by using the fact that the `metadata.outputs` object will only contain one element that has the `entrypoint` property - and then using that as the entry-point path. For runtime safety, we now assert that there cannot be zero or multiple such elements. Fixes #252
- Loading branch information
1 parent
014a731
commit f9c1423
Showing
3 changed files
with
150 additions
and
21 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,11 @@ | ||
--- | ||
"wrangler": patch | ||
--- | ||
|
||
fix: correctly handle entry-point path when publishing | ||
|
||
The `publish` command was failing when the entry-point was specified in the wrangler.toml file and the entry-point imported another file. | ||
|
||
This was because we were using the `metafile.inputs` to guess the entry-point file path. But the order in which the source-files were added to this object was not well defined, and so we could end up failing to find a match. | ||
|
||
This fix avoids this by using the fact that the `metadata.outputs` object will only contain one element that has the `entrypoint` property - and then using that as the entry-point path. For runtime safety, we now assert that there cannot be zero or multiple such elements. |
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,102 @@ | ||
import * as fs from "node:fs"; | ||
import { setMockResponse } from "./mock-cfetch"; | ||
import { runInTempDir } from "./run-in-tmp"; | ||
import { runWrangler } from "./run-wrangler"; | ||
|
||
describe("publish", () => { | ||
runInTempDir(); | ||
|
||
it("should be able to use `index` with no extension as the entry-point", async () => { | ||
writeWranglerToml(); | ||
writeEsmWorkerSource(); | ||
mockUploadWorkerRequest(); | ||
mockSubDomainRequest(); | ||
|
||
const { stdout, stderr, error } = await runWrangler("publish ./index"); | ||
|
||
expect(stdout).toMatchInlineSnapshot(` | ||
"Uploaded | ||
test-name | ||
(0.00 sec) | ||
Deployed | ||
test-name | ||
(0.00 sec) | ||
test-name.test-sub-domain.workers.dev" | ||
`); | ||
expect(stderr).toMatchInlineSnapshot(`""`); | ||
expect(error).toMatchInlineSnapshot(`undefined`); | ||
}); | ||
|
||
it("should be able to use the `build.upload.main` config as the entry-point for ESM sources", async () => { | ||
writeWranglerToml("./index.js"); | ||
writeEsmWorkerSource(); | ||
mockUploadWorkerRequest(); | ||
mockSubDomainRequest(); | ||
|
||
const { stdout, stderr, error } = await runWrangler("publish"); | ||
|
||
expect(stdout).toMatchInlineSnapshot(` | ||
"Uploaded | ||
test-name | ||
(0.00 sec) | ||
Deployed | ||
test-name | ||
(0.00 sec) | ||
test-name.test-sub-domain.workers.dev" | ||
`); | ||
expect(stderr).toMatchInlineSnapshot(`""`); | ||
expect(error).toMatchInlineSnapshot(`undefined`); | ||
}); | ||
}); | ||
|
||
/** Write a mock wrangler.toml file to disk. */ | ||
function writeWranglerToml(main?: string) { | ||
fs.writeFileSync( | ||
"./wrangler.toml", | ||
[ | ||
`compatibility_date = "2022-01-12"`, | ||
`name = "test-name"`, | ||
main !== undefined ? `[build.upload]\nmain = "${main}"` : "", | ||
].join("\n"), | ||
"utf-8" | ||
); | ||
} | ||
|
||
/** Write a mock Worker script to disk. */ | ||
function writeEsmWorkerSource() { | ||
fs.writeFileSync( | ||
"index.js", | ||
[ | ||
`import { foo } from "./another";`, | ||
`export default {`, | ||
` async fetch(request) {`, | ||
` return new Response('Hello' + foo);`, | ||
` },`, | ||
`};`, | ||
].join("\n") | ||
); | ||
fs.writeFileSync("another.js", `export const foo = 100;`); | ||
} | ||
|
||
/** Create a mock handler for the request to upload a worker script. */ | ||
function mockUploadWorkerRequest(available_on_subdomain = true) { | ||
setMockResponse( | ||
"/accounts/:accountId/workers/scripts/:scriptName", | ||
"PUT", | ||
([_url, accountId, scriptName], _init, queryParams) => { | ||
expect(accountId).toEqual("some-account-id"); | ||
expect(scriptName).toEqual("test-name"); | ||
expect(queryParams.get("available_on_subdomains")).toEqual("true"); | ||
return { available_on_subdomain }; | ||
} | ||
); | ||
} | ||
|
||
/** Create a mock handler the request for the account's subdomain. */ | ||
function mockSubDomainRequest(subdomain = "test-sub-domain") { | ||
setMockResponse("/accounts/:accountId/workers/subdomain", () => { | ||
return { subdomain }; | ||
}); | ||
} |
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