Skip to content
Draft
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
7 changes: 7 additions & 0 deletions .changeset/gentle-animals-fail.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@cloudflare/vite-plugin": minor
---

Nest child environment build output in parent environment build output directory.

This ensures that a single output directory is used for the Worker without additional configuration.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import * as fs from "node:fs";
import * as path from "node:path";
import { test } from "vitest";
import { getTextResponse, isBuild } from "../../__test-utils__";
import { getTextResponse, isBuild, testDir } from "../../__test-utils__";

test.runIf(!isBuild)(
"can import module from child environment",
Expand All @@ -8,3 +10,15 @@ test.runIf(!isBuild)(
expect(response).toBe("Hello from the child environment");
}
);

test.runIf(isBuild)(
"nests child environment output in parent environment output directory",
({ expect }) => {
const childEnvironmentEntryPath = path.join(
testDir,
"dist/parent/child/child-environment-module.js"
);

expect(fs.existsSync(childEnvironmentEntryPath)).toBe(true);
}
);
Original file line number Diff line number Diff line change
@@ -1,7 +1,30 @@
import assert from "node:assert";
import * as path from "node:path";
import { cloudflare } from "@cloudflare/vite-plugin";
import { defineConfig } from "vite";

export default defineConfig({
environments: {
child: {
build: {
rollupOptions: {
input: path.resolve(__dirname, "src/child-environment-module.ts"),
},
},
},
},
builder: {
async buildApp(builder) {
const parentEnvironment = builder.environments.parent;
const childEnvironment = builder.environments.child;

assert(parentEnvironment, `No "parent" environment`);
assert(childEnvironment, `No "child" environment`);

await builder.build(parentEnvironment);
await builder.build(childEnvironment);
},
},
plugins: [
cloudflare({
inspectorPort: false,
Expand Down
17 changes: 12 additions & 5 deletions packages/vite-plugin-cloudflare/src/cloudflare-environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@ import {
VIRTUAL_WORKER_ENTRY,
WORKER_ENTRY_PATH_HEADER,
} from "./shared";
import { debuglog, getOutputDirectory, isRolldown } from "./utils";
import {
debuglog,
getChildOutputDirectory,
getOutputDirectory,
isRolldown,
} from "./utils";
import type { ExportTypes } from "./export-types";
import type {
ResolvedWorkerConfig,
Expand Down Expand Up @@ -196,18 +201,18 @@ export function createCloudflareEnvironmentOptions({
mode,
environmentName,
isEntryWorker,
isParentEnvironment,
parentEnvironmentOptions,
hasNodeJsCompat,
}: {
workerConfig: ResolvedWorkerConfig;
userConfig: vite.UserConfig;
mode: vite.ConfigEnv["mode"];
environmentName: string;
isEntryWorker: boolean;
isParentEnvironment: boolean;
hasNodeJsCompat: boolean;
parentEnvironmentOptions: vite.EnvironmentOptions | undefined;
}): vite.EnvironmentOptions {
const rollupOptions: vite.Rollup.RollupOptions = isParentEnvironment
const rollupOptions: vite.Rollup.RollupOptions = !parentEnvironmentOptions
? {
input: {
[MAIN_ENTRY_NAME]: VIRTUAL_WORKER_ENTRY,
Expand Down Expand Up @@ -254,7 +259,9 @@ export function createCloudflareEnvironmentOptions({
target,
emitAssets: true,
manifest: isEntryWorker,
outDir: getOutputDirectory(userConfig, environmentName),
outDir: parentEnvironmentOptions
? getChildOutputDirectory(parentEnvironmentOptions, environmentName)
: getOutputDirectory(userConfig, environmentName),
copyPublicDir: false,
ssr: true,
...(isRolldown
Expand Down
16 changes: 9 additions & 7 deletions packages/vite-plugin-cloudflare/src/plugins/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,14 +174,16 @@ function getEnvironmentsConfig(
environmentName ===
ctx.resolvedPluginConfig.entryWorkerEnvironmentName);

const parentEnvironmentOptions = createCloudflareEnvironmentOptions({
...sharedOptions,
environmentName,
isEntryWorker,
parentEnvironmentOptions: undefined,
});

const parentConfig = [
environmentName,
createCloudflareEnvironmentOptions({
...sharedOptions,
environmentName,
isEntryWorker,
isParentEnvironment: true,
}),
parentEnvironmentOptions,
] as const;

const childConfigs = childEnvironmentNames.map(
Expand All @@ -192,7 +194,7 @@ function getEnvironmentsConfig(
...sharedOptions,
environmentName: childEnvironmentName,
isEntryWorker: false,
isParentEnvironment: false,
parentEnvironmentOptions,
}),
] as const
);
Expand Down
11 changes: 11 additions & 0 deletions packages/vite-plugin-cloudflare/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import assert from "node:assert";
import * as nodePath from "node:path";
import * as util from "node:util";
import { createRequest, sendResponse } from "@remix-run/node-fetch-server";
Expand Down Expand Up @@ -43,6 +44,16 @@ export function getOutputDirectory(
);
}

export function getChildOutputDirectory(
parentEnvironmentOptions: vite.EnvironmentOptions,
childEnvironmentName: string
): string {
const parentOutDir = parentEnvironmentOptions.build?.outDir;
assert(parentOutDir, "Parent environment outDir is not defined");

return nodePath.join(parentOutDir, childEnvironmentName);
}

const postfixRE = /[?#].*$/;
export function cleanUrl(url: string): string {
return url.replace(postfixRE, "");
Expand Down
Loading