diff --git a/.changeset/lovely-elephants-reflect.md b/.changeset/lovely-elephants-reflect.md new file mode 100644 index 000000000000..74516e7dfed7 --- /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 9c3128d7834b..6956b448a24f 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(); @@ -105,6 +105,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", () => { @@ -117,7 +168,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(); @@ -154,7 +205,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(); @@ -196,7 +247,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(); @@ -237,7 +288,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(); @@ -278,7 +329,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(); @@ -317,7 +372,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(); @@ -356,7 +415,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(); @@ -397,9 +460,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(); @@ -446,7 +511,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(); @@ -492,7 +557,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(); @@ -535,9 +600,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(); @@ -571,7 +638,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(); @@ -598,22 +665,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 660be9b4f939..275a1ede8281 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 5f7d1e4ba604..6c713f5c0a36 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); }