Skip to content

Commit

Permalink
feat: publish --do-not-build
Browse files Browse the repository at this point in the history
This adds a `--do-not-build` flag to `wrangler publish`. We've had a bunch of people asking ot 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 committed Jun 19, 2022
1 parent f7d362e commit 56ad9a9
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 14 deletions.
15 changes: 15 additions & 0 deletions packages/wrangler/src/__tests__/publish.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5661,6 +5661,21 @@ addEventListener('fetch', event => {});`
`);
});
});

describe.only("--do-not-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 --do-not-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
6 changes: 6 additions & 0 deletions packages/wrangler/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1306,6 +1306,11 @@ function createCLIParser(argv: string[]) {
type: "string",
requiresArg: true,
})
.option("do-not-build", {
describe: "Skip internal build steps and directly publish script",
type: "boolean",
default: false,
})
.option("outdir", {
describe: "Output directory for the bundled worker",
type: "string",
Expand Down Expand Up @@ -1476,6 +1481,7 @@ function createCLIParser(argv: string[]) {
isWorkersSite: Boolean(args.site || config.site),
outDir: args.outdir,
dryRun: args.dryRun,
doNotBuild: args["do-not-build"],
});
}
);
Expand Down
48 changes: 34 additions & 14 deletions packages/wrangler/src/publish.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ type Props = {
nodeCompat: boolean | undefined;
outDir: string | undefined;
dryRun: boolean | undefined;
doNotBuild: boolean | undefined;
};

type RouteObject = ZoneIdRoute | ZoneNameRoute | CustomDomainRoute;
Expand Down Expand Up @@ -332,20 +333,39 @@ export default async function publish(props: Props): Promise<void> {
);
}
try {
const { modules, resolvedEntryPointPath, bundleType } = await bundleWorker(
props.entry,
typeof destination === "string" ? destination : destination.path,
{
serveAssetsFromWorker:
!props.isWorkersSite && Boolean(props.assetPaths),
jsxFactory,
jsxFragment,
rules: props.rules,
tsconfig: props.tsconfig ?? config.tsconfig,
minify,
nodeCompat,
}
);
if (props.doNotBuild) {
// if we're not building, let's just copy the entry to the destination directory
const destinationDir =
typeof destination === "string" ? destination : destination.path;
mkdirSync(destinationDir, { recursive: true });
writeFileSync(
path.join(destinationDir, path.basename(props.entry.file)),
readFileSync(props.entry.file, "utf-8")
);
}

const { modules, resolvedEntryPointPath, bundleType } = props.doNotBuild
? // we can skip the whole bundling step and mock a bundle here
({
modules: [],
resolvedEntryPointPath: props.entry.file,
bundleType: props.entry.format === "modules" ? "esm" : "commonjs",
stop: undefined,
} as Awaited<ReturnType<typeof bundleWorker>>)
: await bundleWorker(
props.entry,
typeof destination === "string" ? destination : destination.path,
{
serveAssetsFromWorker:
!props.isWorkersSite && Boolean(props.assetPaths),
jsxFactory,
jsxFragment,
rules: props.rules,
tsconfig: props.tsconfig ?? config.tsconfig,
minify,
nodeCompat,
}
);

const content = readFileSync(resolvedEntryPointPath, {
encoding: "utf-8",
Expand Down

0 comments on commit 56ad9a9

Please sign in to comment.