Skip to content

Commit

Permalink
Avoid uploading the 'functions' directory as part of 'wrangler pages …
Browse files Browse the repository at this point in the history
…publish'
  • Loading branch information
GregBrimble committed Oct 31, 2022
1 parent 2a81cae commit 5eab88c
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 10 deletions.
9 changes: 9 additions & 0 deletions .changeset/cool-nails-marry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
"wrangler": patch
---

fix: Don't upload `functions/` directory as part of `wrangler pages publish`

If the root directory of a project was the same as the build output directory, we were previously uploading the `functions/` directory as static assets. This PR now ensures that the `functions/` files are only used to create Pages Functions and are no longer uploaded as static assets.

Additionally, we also now _do_ upload `_worker.js`, `_headers`, `_redirects` and `_routes.json` if they aren't immediate children of the build output directory. Previously, we'd ignore all files with this name regardless of location. For example, if you have a `public/blog/how-to-use-pages/_headers` file (where `public` is your build output directory), we will now upload the `_headers` file as a static asset.
41 changes: 41 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/wrangler/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@
"jest-fetch-mock": "^3.0.3",
"jest-websocket-mock": "^2.3.0",
"mime": "^3.0.0",
"minimatch": "^5.1.0",
"msw": "^0.47.1",
"npx-import": "^1.1.3",
"open": "^8.4.0",
Expand Down
4 changes: 4 additions & 0 deletions packages/wrangler/src/is-interactive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
* or you're piping values from / to another process, etc
*/
export default function isInteractive(): boolean {
if (process.env.CF_PAGES === "1") {
return false;
}

try {
return Boolean(process.stdin.isTTY && process.stdout.isTTY);
} catch {
Expand Down
5 changes: 5 additions & 0 deletions packages/wrangler/src/pages/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ import {
} from "./constants";
import { RoutesValidationError } from "./functions/routes-validation";

/**
* Exit code for `pages project upload` when we know what went wrong and have thrown a good error.
*/
export const EXIT_CODE_UPLOAD_KNOWN_ERROR = 157;

/**
* Exit code for `pages functions build` when no routes are found.
*/
Expand Down
27 changes: 17 additions & 10 deletions packages/wrangler/src/pages/upload.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ import { dirname, join, relative, resolve, sep } from "node:path";
import { render, Text } from "ink";
import Spinner from "ink-spinner";
import { getType } from "mime";
import { Minimatch } from "minimatch";
import PQueue from "p-queue";
import prettyBytes from "pretty-bytes";
import React from "react";
import { fetchResult } from "../cfetch";
import { FatalError } from "../errors";
import isInteractive from "../is-interactive";
import { logger } from "../logger";
import {
BULK_UPLOAD_CONCURRENCY,
Expand All @@ -16,6 +18,7 @@ import {
MAX_CHECK_MISSING_ATTEMPTS,
MAX_UPLOAD_ATTEMPTS,
} from "./constants";
import { EXIT_CODE_UPLOAD_KNOWN_ERROR } from "./errors";
import { hashFile } from "./hash";
import { pagesBetaWarning } from "./utils";
import type { UploadPayloadFile, YargsOptionsToInterface } from "./types";
Expand Down Expand Up @@ -96,10 +99,11 @@ export const upload = async (
"_redirects",
"_headers",
"_routes.json",
".DS_Store",
"node_modules",
".git",
];
"functions",
"**/.DS_Store",
"**/node_modules",
"**/.git",
].map((pattern) => new Minimatch(pattern));

const directory = resolve(args.directory);

Expand All @@ -121,10 +125,13 @@ export const upload = async (
await Promise.all(
files.map(async (file) => {
const filepath = join(dir, file);
const relativeFilepath = relative(startingDir, filepath);
const filestat = await stat(filepath);

if (IGNORE_LIST.includes(file)) {
return;
for (const mm of IGNORE_LIST) {
if (mm.match(relativeFilepath)) {
return;
}
}

if (filestat.isSymbolicLink()) {
Expand All @@ -134,14 +141,14 @@ export const upload = async (
if (filestat.isDirectory()) {
fileMap = await walk(filepath, fileMap, startingDir);
} else {
const name = relative(startingDir, filepath).split(sep).join("/");
const name = relativeFilepath.split(sep).join("/");

if (filestat.size > 25 * 1024 * 1024) {
throw new FatalError(
`Error: Pages only supports files up to ${prettyBytes(
25 * 1024 * 1024
)} in size\n${name} is ${prettyBytes(filestat.size)} in size`,
1
EXIT_CODE_UPLOAD_KNOWN_ERROR
);
}

Expand All @@ -164,7 +171,7 @@ export const upload = async (
if (fileMap.size > 20000) {
throw new FatalError(
`Error: Pages only supports up to 20,000 files in a deployment. Ensure you have specified your build output directory correctly.`,
1
EXIT_CODE_UPLOAD_KNOWN_ERROR
);
}

Expand Down Expand Up @@ -390,7 +397,7 @@ function Progress({ done, total }: { done: number; total: number }) {
return (
<>
<Text>
<Spinner type="earth" />
{isInteractive() ? <Spinner type="earth" /> : null}
{` Uploading... (${done}/${total})\n`}
</Text>
</>
Expand Down

0 comments on commit 5eab88c

Please sign in to comment.