Skip to content

Commit

Permalink
WIP: _worker.js/ dir
Browse files Browse the repository at this point in the history
  • Loading branch information
GregBrimble committed Apr 5, 2023
1 parent 0a77990 commit 84a6fa4
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 27 deletions.
40 changes: 30 additions & 10 deletions packages/wrangler/src/api/pages/publish.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { existsSync, readFileSync } from "node:fs";
import { existsSync, lstatSync, readFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join, resolve as resolvePath } from "node:path";
import { join, resolve, resolve as resolvePath } from "node:path";
import { cwd } from "node:process";
import { File, FormData } from "undici";
import { fetchResult } from "../../cfetch";
Expand All @@ -17,6 +17,7 @@ import {
} from "../../pages/functions/buildWorker";
import { validateRoutes } from "../../pages/functions/routes-validation";
import { upload } from "../../pages/upload";
import traverseModuleGraph from "../../traverse-module-graph";
import { createUploadWorkerBundleContents } from "./create-worker-bundle-contents";
import type { BundleResult } from "../../bundle";
import type { Project, Deployment } from "@cloudflare/types";
Expand Down Expand Up @@ -95,9 +96,10 @@ export async function publish({
_redirects: string | undefined,
_routesGenerated: string | undefined,
_routesCustom: string | undefined,
_workerJSDirectory = false,
_workerJS: string | undefined;

const workerScriptPath = resolvePath(directory, "_worker.js");
const _workerPath = resolvePath(directory, "_worker.js");

try {
_headers = readFileSync(join(directory, "_headers"), "utf-8");
Expand All @@ -116,7 +118,10 @@ export async function publish({
} catch {}

try {
_workerJS = readFileSync(workerScriptPath, "utf-8");
_workerJSDirectory = lstatSync(_workerPath).isDirectory();
if (!_workerJSDirectory) {
_workerJS = readFileSync(_workerPath, "utf-8");
}
} catch {}

// Grab the bindings from the API, we need these for shims and other such hacky inserts
Expand Down Expand Up @@ -243,13 +248,28 @@ export async function publish({
* When using a _worker.js file, the entire /functions directory is ignored
* – this includes its routing and middleware characteristics.
*/
if (_workerJS) {
if (_workerJSDirectory) {
workerBundle = await traverseModuleGraph(

Check warning on line 252 in packages/wrangler/src/api/pages/publish.tsx

View check run for this annotation

Codecov / codecov/patch

packages/wrangler/src/api/pages/publish.tsx#L252

Added line #L252 was not covered by tests
{
file: "index.js",
directory: resolve(_workerPath),
format: "modules",
moduleRoot: resolve(_workerPath),
},
[
{
type: "ESModule",
globs: ["**/*.js"],
},
]
);
} else if (_workerJS) {
if (bundle) {
const outfile = join(tmpdir(), `./bundledWorker-${Math.random()}.mjs`);
workerBundle = await buildRawWorker({
workerScriptPath,
workerScriptPath: _workerPath,
outfile,
directory: directory ?? ".",
directory,
local: false,
sourcemap: true,
watch: false,
Expand All @@ -258,13 +278,13 @@ export async function publish({
nodejsCompat,
});
} else {
await checkRawWorker(workerScriptPath, () => {});
// TODO: Replace this with the cool new no-bundle stuff when that lands: https://github.com/cloudflare/workers-sdk/pull/2769
await checkRawWorker(_workerPath, () => {});
// TODO: Let users configure this in the future.
workerBundle = {
modules: [],
dependencies: {},
stop: undefined,
resolvedEntryPointPath: workerScriptPath,
resolvedEntryPointPath: _workerPath,
bundleType: "esm",
};
}
Expand Down
58 changes: 41 additions & 17 deletions packages/wrangler/src/pages/build.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
import { existsSync, mkdirSync, writeFileSync } from "node:fs";
import { basename, dirname, relative, resolve as resolvePath } from "node:path";
import { existsSync, lstatSync, mkdirSync, writeFileSync } from "node:fs";
import {
basename,
dirname,
relative,
resolve,
resolve as resolvePath,
} from "node:path";
import { createUploadWorkerBundleContents } from "../api/pages/create-worker-bundle-contents";
import { FatalError } from "../errors";
import { logger } from "../logger";
import * as metrics from "../metrics";
import traverseModuleGraph from "../traverse-module-graph";
import { buildFunctions } from "./buildFunctions";
import { isInPagesCI } from "./constants";
import {
Expand Down Expand Up @@ -208,21 +215,38 @@ export const Handler = async (args: PagesBuildArgs) => {
* and if we were able to resolve _worker.js
*/
if (workerScriptPath) {
/**
* `buildRawWorker` builds `_worker.js`, but doesn't give us the bundle
* we want to return, which includes the external dependencies (like wasm,
* binary, text). Let's output that build result to memory and only write
* to disk once we have the final bundle
*/
bundle = await buildRawWorker({
workerScriptPath,
outdir,
directory: buildOutputDirectory,
local: false,
sourcemap,
watch,
betaD1Shims: d1Databases,
});
if (lstatSync(workerScriptPath).isDirectory()) {
bundle = await traverseModuleGraph(

Check warning on line 219 in packages/wrangler/src/pages/build.ts

View check run for this annotation

Codecov / codecov/patch

packages/wrangler/src/pages/build.ts#L219

Added line #L219 was not covered by tests
{
file: "index.js",
directory: resolve(workerScriptPath),
format: "modules",
moduleRoot: resolve(workerScriptPath),
},
[
{
type: "ESModule",
globs: ["**/*.js"],
},
]
);
} else {
/**
* `buildRawWorker` builds `_worker.js`, but doesn't give us the bundle
* we want to return, which includes the external dependencies (like wasm,
* binary, text). Let's output that build result to memory and only write
* to disk once we have the final bundle
*/
bundle = await buildRawWorker({
workerScriptPath,
outdir,
directory: buildOutputDirectory,
local: false,
sourcemap,
watch,
betaD1Shims: d1Databases,
});
}
} else {
try {
/**
Expand Down

0 comments on commit 84a6fa4

Please sign in to comment.