Skip to content

Commit

Permalink
feat: publish --no-build (#1300)
Browse files Browse the repository at this point in the history
This adds a `--no-build` flag to `wrangler publish`. We've had a bunch of people asking to be able to upload a worker directly, without any modifications. While there are tradeoffs to this approach (any linked modules etc won't work), we understand that people who need this functionality are aware of it (and the usecases that have presented themselves all seem to match this).
  • Loading branch information
threepointone authored Jun 28, 2022
1 parent 1bab3f6 commit dcffc93
Show file tree
Hide file tree
Showing 9 changed files with 615 additions and 496 deletions.
7 changes: 7 additions & 0 deletions .changeset/spotty-onions-exist.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"wrangler": patch
---

feat: `publish --no-build`

This adds a `--no-build` flag to `wrangler publish`. We've had a bunch of people asking to be able to upload a worker directly, without any modifications. While there are tradeoffs to this approach (any linked modules etc won't work), we understand that people who need this functionality are aware of it (and the usecases that have presented themselves all seem to match this).
373 changes: 187 additions & 186 deletions packages/wrangler/src/__tests__/dev.test.tsx

Large diffs are not rendered by default.

13 changes: 13 additions & 0 deletions packages/wrangler/src/__tests__/publish.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6020,6 +6020,19 @@ addEventListener('fetch', event => {});`
`);
});
});

describe("--no-build", () => {
it("should not transform the source code before publishing it", async () => {
writeWranglerToml();
const scriptContent = `
import X from '@cloudflare/no-such-package'; // let's add an import that doesn't exist
const xyz = 123; // a statement that would otherwise be compiled out
`;
fs.writeFileSync("index.js", scriptContent);
await runWrangler("publish index.js --no-build --dry-run --outdir dist");
expect(fs.readFileSync("dist/index.js", "utf-8")).toMatch(scriptContent);
});
});
});

/** Write mock assets to the file system so they can be uploaded. */
Expand Down
320 changes: 170 additions & 150 deletions packages/wrangler/src/dev.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ interface DevArgs {
config?: string;
script?: string;
name?: string;
build?: boolean;
format?: string;
env?: string;
"compatibility-date"?: string;
Expand Down Expand Up @@ -59,156 +60,174 @@ interface DevArgs {
}

export function devOptions(yargs: Argv): Argv<DevArgs> {
return yargs
.positional("script", {
describe: "The path to an entry point for your worker",
type: "string",
})
.option("name", {
describe: "Name of the worker",
type: "string",
requiresArg: true,
})
.option("format", {
choices: ["modules", "service-worker"] as const,
describe: "Choose an entry type",
deprecated: true,
})
.option("env", {
describe: "Perform on a specific environment",
type: "string",
requiresArg: true,
alias: "e",
})
.option("compatibility-date", {
describe: "Date to use for compatibility checks",
type: "string",
requiresArg: true,
})
.option("compatibility-flags", {
describe: "Flags to use for compatibility checks",
alias: "compatibility-flag",
type: "string",
requiresArg: true,
array: true,
})
.option("latest", {
describe: "Use the latest version of the worker runtime",
type: "boolean",
default: true,
})
.option("ip", {
describe: "IP address to listen on, defaults to `localhost`",
type: "string",
requiresArg: true,
})
.option("port", {
describe: "Port to listen on",
type: "number",
})
.option("inspector-port", {
describe: "Port for devtools to connect to",
type: "number",
})
.option("routes", {
describe: "Routes to upload",
alias: "route",
type: "string",
requiresArg: true,
array: true,
})
.option("host", {
type: "string",
requiresArg: true,
describe: "Host to forward requests to, defaults to the zone of project",
})
.option("local-protocol", {
describe: "Protocol to listen to requests on, defaults to http.",
choices: ["http", "https"] as const,
})
.options("local-upstream", {
type: "string",
describe:
"Host to act as origin in local mode, defaults to dev.host or route",
})
.option("experimental-public", {
describe: "Static assets to be served",
type: "string",
requiresArg: true,
deprecated: true,
hidden: true,
})
.option("assets", {
describe: "Static assets to be served",
type: "string",
requiresArg: true,
})
.option("site", {
describe: "Root folder of static assets for Workers Sites",
type: "string",
requiresArg: true,
})
.option("site-include", {
describe:
"Array of .gitignore-style patterns that match file or directory names from the sites directory. Only matched items will be uploaded.",
type: "string",
requiresArg: true,
array: true,
})
.option("site-exclude", {
describe:
"Array of .gitignore-style patterns that match file or directory names from the sites directory. Matched items will not be uploaded.",
type: "string",
requiresArg: true,
array: true,
})
.option("upstream-protocol", {
describe: "Protocol to forward requests to host on, defaults to https.",
choices: ["http", "https"] as const,
})
.option("jsx-factory", {
describe: "The function that is called for each JSX element",
type: "string",
requiresArg: true,
})
.option("jsx-fragment", {
describe: "The function that is called for each JSX fragment",
type: "string",
requiresArg: true,
})
.option("tsconfig", {
describe: "Path to a custom tsconfig.json file",
type: "string",
requiresArg: true,
})
.option("local", {
alias: "l",
describe: "Run on my machine",
type: "boolean",
default: false, // I bet this will a point of contention. We'll revisit it.
})
.option("minify", {
describe: "Minify the script",
type: "boolean",
})
.option("node-compat", {
describe: "Enable node.js compatibility",
type: "boolean",
})
.option("experimental-enable-local-persistence", {
describe: "Enable persistence for this session (only for local mode)",
type: "boolean",
})
.option("inspect", {
describe: "Enable dev tools",
type: "boolean",
deprecated: true,
})
.option("legacy-env", {
type: "boolean",
describe: "Use legacy environments",
hidden: true,
});
return (
yargs
.positional("script", {
describe: "The path to an entry point for your worker",
type: "string",
})
.option("name", {
describe: "Name of the worker",
type: "string",
requiresArg: true,
})
// We want to have a --no-build flag, but yargs requires that
// we also have a --build flag (that it adds the --no to by itself)
// So we make a --build flag, but hide it, and then add a --no-build flag
// that's visible to the user but doesn't "do" anything.
.option("build", {
describe: "Run wrangler's compilation step before publishing",
type: "boolean",
default: true,
hidden: true,
})
.option("no-build", {
describe: "Skip internal build steps and directly publish script",
type: "boolean",
default: false,
})
.option("format", {
choices: ["modules", "service-worker"] as const,
describe: "Choose an entry type",
deprecated: true,
})
.option("env", {
describe: "Perform on a specific environment",
type: "string",
requiresArg: true,
alias: "e",
})
.option("compatibility-date", {
describe: "Date to use for compatibility checks",
type: "string",
requiresArg: true,
})
.option("compatibility-flags", {
describe: "Flags to use for compatibility checks",
alias: "compatibility-flag",
type: "string",
requiresArg: true,
array: true,
})
.option("latest", {
describe: "Use the latest version of the worker runtime",
type: "boolean",
default: true,
})
.option("ip", {
describe: "IP address to listen on, defaults to `localhost`",
type: "string",
requiresArg: true,
})
.option("port", {
describe: "Port to listen on",
type: "number",
})
.option("inspector-port", {
describe: "Port for devtools to connect to",
type: "number",
})
.option("routes", {
describe: "Routes to upload",
alias: "route",
type: "string",
requiresArg: true,
array: true,
})
.option("host", {
type: "string",
requiresArg: true,
describe:
"Host to forward requests to, defaults to the zone of project",
})
.option("local-protocol", {
describe: "Protocol to listen to requests on, defaults to http.",
choices: ["http", "https"] as const,
})
.options("local-upstream", {
type: "string",
describe:
"Host to act as origin in local mode, defaults to dev.host or route",
})
.option("experimental-public", {
describe: "Static assets to be served",
type: "string",
requiresArg: true,
deprecated: true,
hidden: true,
})
.option("assets", {
describe: "Static assets to be served",
type: "string",
requiresArg: true,
})
.option("site", {
describe: "Root folder of static assets for Workers Sites",
type: "string",
requiresArg: true,
})
.option("site-include", {
describe:
"Array of .gitignore-style patterns that match file or directory names from the sites directory. Only matched items will be uploaded.",
type: "string",
requiresArg: true,
array: true,
})
.option("site-exclude", {
describe:
"Array of .gitignore-style patterns that match file or directory names from the sites directory. Matched items will not be uploaded.",
type: "string",
requiresArg: true,
array: true,
})
.option("upstream-protocol", {
describe: "Protocol to forward requests to host on, defaults to https.",
choices: ["http", "https"] as const,
})
.option("jsx-factory", {
describe: "The function that is called for each JSX element",
type: "string",
requiresArg: true,
})
.option("jsx-fragment", {
describe: "The function that is called for each JSX fragment",
type: "string",
requiresArg: true,
})
.option("tsconfig", {
describe: "Path to a custom tsconfig.json file",
type: "string",
requiresArg: true,
})
.option("local", {
alias: "l",
describe: "Run on my machine",
type: "boolean",
default: false, // I bet this will a point of contention. We'll revisit it.
})
.option("minify", {
describe: "Minify the script",
type: "boolean",
})
.option("node-compat", {
describe: "Enable node.js compatibility",
type: "boolean",
})
.option("experimental-enable-local-persistence", {
describe: "Enable persistence for this session (only for local mode)",
type: "boolean",
})
.option("inspect", {
describe: "Enable dev tools",
type: "boolean",
deprecated: true,
})
.option("legacy-env", {
type: "boolean",
describe: "Use legacy environments",
hidden: true,
})
);
}

export async function devHandler(args: ArgumentsCamelCase<DevArgs>) {
Expand Down Expand Up @@ -412,6 +431,7 @@ export async function devHandler(args: ArgumentsCamelCase<DevArgs>) {
return (
<Dev
name={getScriptName({ name: args.name, env: args.env }, config)}
noBuild={!args.build}
entry={entry}
env={args.env}
zone={zoneId}
Expand Down
4 changes: 3 additions & 1 deletion packages/wrangler/src/dev/dev.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ import type { AssetPaths } from "../sites";
import type { CfWorkerInit } from "../worker";

export type DevProps = {
name?: string;
name: string | undefined;
noBuild: boolean;
entry: Entry;
port: number;
ip: string;
Expand Down Expand Up @@ -149,6 +150,7 @@ function DevSession(props: DevSessionProps) {
minify: props.minify,
nodeCompat: props.nodeCompat,
define: props.define,
noBuild: props.noBuild,
});

return props.local ? (
Expand Down
Loading

0 comments on commit dcffc93

Please sign in to comment.