From cf70bd0725abfc06bec7f69f0367b58b46e26b0f Mon Sep 17 00:00:00 2001 From: Pete Bacon Darwin Date: Mon, 24 Jan 2022 21:52:38 +0000 Subject: [PATCH] fix: warn if the `site.entry-point` configuration is found during publishing Also updates the message and adds a test for the error when there is no entry-point specified. Fixes #282 --- .changeset/lovely-elephants-reflect.md | 9 ++ .../wrangler/src/__tests__/publish.test.ts | 123 ++++++++++++++---- packages/wrangler/src/index.tsx | 8 ++ packages/wrangler/src/publish.ts | 14 +- 4 files changed, 129 insertions(+), 25 deletions(-) create mode 100644 .changeset/lovely-elephants-reflect.md diff --git a/.changeset/lovely-elephants-reflect.md b/.changeset/lovely-elephants-reflect.md new file mode 100644 index 0000000000000..74516e7dfed7c --- /dev/null +++ b/.changeset/lovely-elephants-reflect.md @@ -0,0 +1,9 @@ +--- +"wrangler": patch +--- + +fix: warn if the `site.entry-point` configuration is found during publishing + +Also updates the message and adds a test for the error when there is no entry-point specified. + +Fixes #282 diff --git a/packages/wrangler/src/__tests__/publish.test.ts b/packages/wrangler/src/__tests__/publish.test.ts index be1273cb5ea7a..b7ed5897637d0 100644 --- a/packages/wrangler/src/__tests__/publish.test.ts +++ b/packages/wrangler/src/__tests__/publish.test.ts @@ -37,7 +37,7 @@ describe("publish", () => { }); it("should be able to use the `build.upload.main` config as the entry-point for ESM sources", async () => { - writeWranglerToml("./index.js"); + writeWranglerToml({ main: "./index.js" }); writeEsmWorkerSource(); mockUploadWorkerRequest(); mockSubDomainRequest(); @@ -57,6 +57,57 @@ describe("publish", () => { expect(stderr).toMatchInlineSnapshot(`""`); expect(error).toMatchInlineSnapshot(`undefined`); }); + + it("should warn if there is a `site.entry-point` configuration", async () => { + writeWranglerToml({ + entryPoint: "./index.js", + }); + writeEsmWorkerSource(); + mockUploadWorkerRequest(); + mockSubDomainRequest(); + + const { stdout, stderr, error, warnings } = await runWrangler( + "publish ./index.js" + ); + + expect(stripTimings(stdout)).toMatchInlineSnapshot(` + "Uploaded + test-name + (TIMINGS) + Deployed + test-name + (TIMINGS) + + test-name.test-sub-domain.workers.dev" + `); + expect(stderr).toMatchInlineSnapshot(`""`); + expect(error).toMatchInlineSnapshot(`undefined`); + expect(warnings).toMatchInlineSnapshot(` + "Deprecation notice: The \`site.entry-point\` config field is no longer used. + The entry-point should be specified via the command line (e.g. \`wrangler publish path/to/script\`) or the \`build.upload.main\` config field. + Please remove the \`site.entry-point\` field from the \`wrangler.toml\` file." + `); + }); + + it("should error if there is no entry-point specified", async () => { + writeWranglerToml(); + writeEsmWorkerSource(); + mockUploadWorkerRequest(); + mockSubDomainRequest(); + + const { stdout, stderr, error } = await runWrangler("publish"); + + expect(stripTimings(stdout)).toMatchInlineSnapshot(`""`); + expect(stderr).toMatchInlineSnapshot(` + "Missing entry-point: The entry-point should be specified via the command line (e.g. \`wrangler publish path/to/script\`) or the \`build.upload.main\` config field. + + %s + If you think this is a bug then please create an issue at https://github.com/cloudflare/wrangler2/issues/new." + `); + expect(error).toMatchInlineSnapshot( + `[Error: Missing entry-point: The entry-point should be specified via the command line (e.g. \`wrangler publish path/to/script\`) or the \`build.upload.main\` config field.]` + ); + }); }); describe("asset upload", () => { @@ -69,7 +120,7 @@ describe("publish", () => { title: "__test-name_sites_assets", id: "__test-name_sites_assets-id", }; - writeWranglerToml("./index.js", "assets"); + writeWranglerToml({ main: "./index.js", bucket: "assets" }); writeEsmWorkerSource(); writeAssets(assets); mockUploadWorkerRequest(); @@ -106,7 +157,7 @@ describe("publish", () => { title: "__test-name_sites_assets", id: "__test-name_sites_assets-id", }; - writeWranglerToml("./index.js", "assets"); + writeWranglerToml({ main: "./index.js", bucket: "assets" }); writeEsmWorkerSource(); writeAssets(assets); mockUploadWorkerRequest(); @@ -148,7 +199,7 @@ describe("publish", () => { title: "__test-name_sites_assets", id: "__test-name_sites_assets-id", }; - writeWranglerToml("./index.js", "assets"); + writeWranglerToml({ main: "./index.js", bucket: "assets" }); writeEsmWorkerSource(); writeAssets(assets); mockUploadWorkerRequest(); @@ -189,7 +240,7 @@ describe("publish", () => { title: "__test-name_sites_assets", id: "__test-name_sites_assets-id", }; - writeWranglerToml("./index.js", "assets"); + writeWranglerToml({ main: "./index.js", bucket: "assets" }); writeEsmWorkerSource(); writeAssets(assets); mockUploadWorkerRequest(); @@ -230,7 +281,11 @@ describe("publish", () => { title: "__test-name_sites_assets", id: "__test-name_sites_assets-id", }; - writeWranglerToml("./index.js", "assets", ["file-1.txt"]); + writeWranglerToml({ + main: "./index.js", + bucket: "assets", + include: ["file-1.txt"], + }); writeEsmWorkerSource(); writeAssets(assets); mockUploadWorkerRequest(); @@ -269,7 +324,11 @@ describe("publish", () => { title: "__test-name_sites_assets", id: "__test-name_sites_assets-id", }; - writeWranglerToml("./index.js", "assets", undefined, ["file-2.txt"]); + writeWranglerToml({ + main: "./index.js", + bucket: "assets", + exclude: ["file-2.txt"], + }); writeEsmWorkerSource(); writeAssets(assets); mockUploadWorkerRequest(); @@ -308,7 +367,11 @@ describe("publish", () => { title: "__test-name_sites_assets", id: "__test-name_sites_assets-id", }; - writeWranglerToml("./index.js", "assets", ["file-2.txt"]); + writeWranglerToml({ + main: "./index.js", + bucket: "assets", + include: ["file-2.txt"], + }); writeEsmWorkerSource(); writeAssets(assets); mockUploadWorkerRequest(); @@ -349,9 +412,11 @@ describe("publish", () => { title: "__test-name_sites_assets", id: "__test-name_sites_assets-id", }; - writeWranglerToml("./index.js", "assets", undefined, [ - "assets/file-1.txt", - ]); + writeWranglerToml({ + main: "./index.js", + bucket: "assets", + exclude: ["assets/file-1.txt"], + }); writeEsmWorkerSource(); writeAssets(assets); mockUploadWorkerRequest(); @@ -398,7 +463,7 @@ describe("publish", () => { title: "__test-name_sites_assets", id: "__test-name_sites_assets-id", }; - writeWranglerToml("./index.js", "assets"); + writeWranglerToml({ main: "./index.js", bucket: "assets" }); writeEsmWorkerSource(); writeAssets(assets); mockUploadWorkerRequest(); @@ -444,7 +509,7 @@ describe("publish", () => { title: "__test-name_sites_assets", id: "__test-name_sites_assets-id", }; - writeWranglerToml("./index.js", "assets"); + writeWranglerToml({ main: "./index.js", bucket: "assets" }); writeEsmWorkerSource(); writeAssets(assets); mockUploadWorkerRequest(); @@ -487,9 +552,11 @@ describe("publish", () => { title: "__test-name_sites_assets", id: "__test-name_sites_assets-id", }; - writeWranglerToml("./index.js", "assets", undefined, [ - "assets/file-1.txt", - ]); + writeWranglerToml({ + main: "./index.js", + bucket: "assets", + exclude: ["assets/file-1.txt"], + }); writeEsmWorkerSource(); writeAssets(assets); mockUploadWorkerRequest(); @@ -523,7 +590,7 @@ describe("publish", () => { title: "__test-name_sites_assets", id: "__test-name_sites_assets-id", }; - writeWranglerToml("./index.js", "assets"); + writeWranglerToml({ main: "./index.js", bucket: "assets" }); writeEsmWorkerSource(); writeAssets([longFilePathAsset]); mockUploadWorkerRequest(); @@ -550,22 +617,30 @@ describe("publish", () => { }); /** Write a mock wrangler.toml file to disk. */ -function writeWranglerToml( - main?: string, - bucket?: string, - include?: string[], - exclude?: string[] -) { +function writeWranglerToml({ + main, + bucket, + include, + exclude, + entryPoint, +}: { + main?: string; + bucket?: string; + include?: string[]; + exclude?: string[]; + entryPoint?: string; +} = {}) { fs.writeFileSync( "./wrangler.toml", [ `compatibility_date = "2022-01-12"`, `name = "test-name"`, main !== undefined ? `[build.upload]\nmain = "${main}"` : "", - bucket || include || exclude ? "[site]" : "", + bucket || include || exclude || entryPoint ? "[site]" : "", bucket !== undefined ? `bucket = "${bucket}"` : "", include !== undefined ? `include = ${JSON.stringify(include)}` : "", exclude !== undefined ? `exclude = ${JSON.stringify(exclude)}` : "", + entryPoint !== undefined ? `entry-point = "${entryPoint}"` : "", ].join("\n"), "utf-8" ); diff --git a/packages/wrangler/src/index.tsx b/packages/wrangler/src/index.tsx index 660be9b4f9391..275a1ede8281a 100644 --- a/packages/wrangler/src/index.tsx +++ b/packages/wrangler/src/index.tsx @@ -494,6 +494,14 @@ export async function main(argv: string[]): Promise { ); } + if (config.site?.["entry-point"]) { + console.warn( + "Deprecation notice: The `site.entry-point` config field is no longer used.\n" + + "The entry-point is specified via the command line (e.g. `wrangler dev path/to/script`).\n" + + "Please remove the `site.entry-point` field from the `wrangler.toml` file." + ); + } + if (!args.local) { // -- snip, extract -- const loggedIn = await loginOrRefreshIfRequired(); diff --git a/packages/wrangler/src/publish.ts b/packages/wrangler/src/publish.ts index 5f7d1e4ba604b..6c713f5c0a36a 100644 --- a/packages/wrangler/src/publish.ts +++ b/packages/wrangler/src/publish.ts @@ -78,13 +78,25 @@ export default async function publish(props: Props): Promise { 'You need to provide a name when publishing a worker. Either pass it as a cli arg with `--name ` or in your config file as `name = ""`' ); + if (config.site?.["entry-point"]) { + console.warn( + "Deprecation notice: The `site.entry-point` config field is no longer used.\n" + + "The entry-point should be specified via the command line (e.g. `wrangler publish path/to/script`) or the `build.upload.main` config field.\n" + + "Please remove the `site.entry-point` field from the `wrangler.toml` file." + ); + } + let file: string; if (props.script) { // If the script name comes from the command line it is relative to the current working directory. file = path.resolve(props.script); } else { // If the script name comes from the config, then it is relative to the wrangler.toml file. - assert(build?.upload?.main, "missing main file"); + if (build?.upload?.main === undefined) { + throw new Error( + "Missing entry-point: The entry-point should be specified via the command line (e.g. `wrangler publish path/to/script`) or the `build.upload.main` config field." + ); + } file = path.resolve(path.dirname(__path__), build.upload.main); }