Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .changeset/lovely-elephants-reflect.md
Original file line number Diff line number Diff line change
@@ -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
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 Down Expand Up @@ -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", () => {
Expand All @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand All @@ -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"
);
Expand Down
8 changes: 8 additions & 0 deletions packages/wrangler/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,14 @@ export async function main(argv: string[]): Promise<void> {
);
}

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();
Expand Down
14 changes: 13 additions & 1 deletion packages/wrangler/src/publish.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,25 @@ 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 (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);
}

Expand Down