Skip to content

Commit

Permalink
fix: warn if the site.entry-point configuration is found during pub…
Browse files Browse the repository at this point in the history
…lishing

Also adds a test for the error when there is no entry-point specified.

Fixes #282
  • Loading branch information
petebacondarwin committed Jan 25, 2022
1 parent b63efe6 commit c1e4d12
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 25 deletions.
7 changes: 7 additions & 0 deletions .changeset/lovely-elephants-reflect.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"wrangler": patch
---

fix: warn if the `site.entry-point` configuration is found during publishing

Fixes #282
123 changes: 99 additions & 24 deletions packages/wrangler/src/__tests__/publish.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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 (see \`wrangler publish --help\`) 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 (see \`wrangler publish --help\`) 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(
`[AssertionError: Missing entry-point: The entry-point should be specified via the command line (see \`wrangler publish --help\`) or the \`build.upload.main\` config field.]`
);
});
});

describe("asset upload", () => {
Expand All @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand All @@ -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"
);
Expand Down
13 changes: 12 additions & 1 deletion packages/wrangler/src/publish.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,24 @@ export default async function publish(props: Props): Promise<void> {
'You need to provide a name when publishing a worker. Either pass it as a cli arg with `--name <name>` or in your config file as `name = "<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 (see `wrangler publish --help`) 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");
assert(
build?.upload?.main,
"Missing entry-point: The entry-point should be specified via the command line (see `wrangler publish --help`) or the `build.upload.main` config field."
);
file = path.resolve(path.dirname(__path__), build.upload.main);
}

Expand Down

0 comments on commit c1e4d12

Please sign in to comment.