Skip to content

Commit

Permalink
Allow output directories to be overridden (#110)
Browse files Browse the repository at this point in the history
* Made output directories overridable

* Added output directory tests
  • Loading branch information
jamesopstad authored Dec 13, 2024
1 parent 73ee5a1 commit 91ca74f
Show file tree
Hide file tree
Showing 10 changed files with 93 additions and 39 deletions.
3 changes: 0 additions & 3 deletions packages/vite-plugin-cloudflare/src/cloudflare-environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,6 @@ const cloudflareBuiltInModules = [

export function createCloudflareEnvironmentOptions(
workerConfig: WorkerConfig,
userConfig: vite.UserConfig,
): vite.EnvironmentOptions {
return {
resolve: {
Expand All @@ -143,8 +142,6 @@ export function createCloudflareEnvironmentOptions(
createEnvironment(name, config) {
return new vite.BuildEnvironment(name, config);
},
// This is a bit of a hack to make sure the user can't override the output directory at the environment level
outDir: userConfig.build?.outDir ?? 'dist',
ssr: true,
rollupOptions: {
// Note: vite starts dev pre-bundling crawling from either optimizeDeps.entries or rollupOptions.input
Expand Down
11 changes: 3 additions & 8 deletions packages/vite-plugin-cloudflare/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,7 @@ export function cloudflare(pluginConfig: PluginConfig = {}): vite.Plugin {
([environmentName, workerConfig]) => {
return [
environmentName,
createCloudflareEnvironmentOptions(
workerConfig,
userConfig,
),
createCloudflareEnvironmentOptions(workerConfig),
];
},
),
Expand Down Expand Up @@ -93,12 +90,10 @@ export function cloudflare(pluginConfig: PluginConfig = {}): vite.Plugin {
};
},
configEnvironment(name, options) {
if (resolvedPluginConfig.type === 'workers') {
if (resolvedPluginConfig.type === 'workers' && !options.build?.outDir) {
options.build = {
...options.build,
// Puts all environment builds in subdirectories of the same build directory
// TODO: allow the user to override this
outDir: path.join(options.build?.outDir ?? 'dist', name),
outDir: path.join('dist', name),
};
}
},
Expand Down
41 changes: 25 additions & 16 deletions packages/vite-plugin-cloudflare/src/miniflare-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,19 @@ export function getDevMiniflareOptions(
};
}

function getConfigPath(
viteConfig: vite.ResolvedConfig,
environmentName: string,
) {
const outDir = viteConfig.environments[environmentName]?.build.outDir;

assert(outDir, `No output directory for environment ${environmentName}`);

const configPath = path.resolve(viteConfig.root, outDir, 'wrangler.json');

return configPath;
}

function getEntryModule(main: string | undefined) {
assert(
main,
Expand All @@ -419,19 +432,19 @@ export function getPreviewMiniflareOptions(
vitePreviewServer: vite.PreviewServer,
): MiniflareOptions {
const viteConfig = vitePreviewServer.config;
// For now, we are enforcing that packages are always inside the same build directory
const buildDirectory = path.resolve(viteConfig.root, viteConfig.build.outDir);

const configPath =
resolvedPluginConfig.type === 'workers'
? path.join(
buildDirectory,
resolvedPluginConfig.entryWorkerEnvironmentName,
'wrangler.json',
)
: path.join(buildDirectory, 'wrangler.json');
const configs = [
unstable_readConfig({ config: configPath }, {}),
unstable_readConfig(
{
config: getConfigPath(
viteConfig,
resolvedPluginConfig.type === 'workers'
? resolvedPluginConfig.entryWorkerEnvironmentName
: 'client',
),
},
{},
),
...(resolvedPluginConfig.type === 'workers'
? Object.keys(resolvedPluginConfig.workers)
.filter(
Expand All @@ -442,11 +455,7 @@ export function getPreviewMiniflareOptions(
.map((environmentName) =>
unstable_readConfig(
{
config: path.join(
buildDirectory,
environmentName,
'wrangler.json',
),
config: getConfigPath(viteConfig, environmentName),
},
{},
),
Expand Down
1 change: 1 addition & 0 deletions playground/multi-worker/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
custom-output-directory
9 changes: 0 additions & 9 deletions playground/multi-worker/__tests__/basic.spec.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import * as fs from 'node:fs';
import * as path from 'node:path';
import { describe, expect, test } from 'vitest';
import { getJsonResponse, isBuild, rootDir } from '../../../__test-utils__';

describe.runIf(isBuild)('output directories', () => {
test('creates the correct output directories', () => {
expect(fs.existsSync(path.join(rootDir, 'dist', 'worker_a'))).toBe(true);
expect(fs.existsSync(path.join(rootDir, 'custom-output-directory'))).toBe(
true,
);
});
});

describe('multi-worker service bindings', async () => {
test('returns a response from another worker', async () => {
const result = await getJsonResponse('/fetch');
expect(result).toEqual({ result: { name: 'Worker B' } });
});
});
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
import * as fs from 'node:fs';
import * as path from 'node:path';
import { describe, expect, test } from 'vitest';
import { getJsonResponse } from '../../__test-utils__';
import { getJsonResponse, isBuild, rootDir } from '../../__test-utils__';

describe.runIf(isBuild)('output directories', () => {
test('creates the correct output directories', () => {
expect(fs.existsSync(path.join(rootDir, 'dist', 'worker_a'))).toBe(true);
expect(fs.existsSync(path.join(rootDir, 'dist', 'worker_b'))).toBe(true);
});
});

describe('multi-worker basic functionality', async () => {
test('entry worker returns a response', async () => {
const result = await getJsonResponse();
expect(result).toEqual({ name: 'Worker A' });
});
});

describe('multi-worker service bindings', async () => {
test('returns a response from another worker', async () => {
Expand Down
4 changes: 3 additions & 1 deletion playground/multi-worker/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
"type": "module",
"scripts": {
"build": "vite build --app",
"build:custom-output-directories": "vite build --app -c ./vite.config.custom-output-directories.ts",
"check:types": "tsc --build",
"dev": "vite dev",
"preview": "vite preview"
"preview": "vite preview",
"preview:custom-output-directories": "vite preview -c ./vite.config.custom-output-directories.ts"
},
"devDependencies": {
"@cloudflare/workers-types": "catalog:default",
Expand Down
6 changes: 5 additions & 1 deletion playground/multi-worker/tsconfig.node.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
{
"extends": ["@vite-plugin-cloudflare/typescript-config/base.json"],
"include": ["vite.config.ts", "__tests__"]
"include": [
"vite.config.ts",
"vite.config.custom-output-directories.ts",
"__tests__"
]
}
19 changes: 19 additions & 0 deletions playground/multi-worker/vite.config.custom-output-directories.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { cloudflare } from '@flarelabs-net/vite-plugin-cloudflare';
import { defineConfig } from 'vite';

export default defineConfig({
environments: {
worker_b: {
build: {
outDir: 'custom-output-directory',
},
},
},
plugins: [
cloudflare({
configPath: './worker-a/wrangler.toml',
auxiliaryWorkers: [{ configPath: './worker-b/wrangler.toml' }],
persistState: false,
}),
],
});

0 comments on commit 91ca74f

Please sign in to comment.