Skip to content
This repository was archived by the owner on Mar 13, 2025. It is now read-only.

Commit 7f98be6

Browse files
committed
Add --root option for resolving default configs
Also combine `log` and `compat` into `PluginContext` passed to plugins
1 parent 9519ccc commit 7f98be6

File tree

29 files changed

+320
-285
lines changed

29 files changed

+320
-285
lines changed

packages/cache/src/plugin.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import {
2-
Compatibility,
32
Log,
43
MiniflareError,
54
Option,
65
OptionType,
76
Plugin,
7+
PluginContext,
88
SetupResult,
99
StorageFactory,
1010
} from "@miniflare/shared";
@@ -123,17 +123,17 @@ export class CachePlugin extends Plugin<CacheOptions> implements CacheOptions {
123123

124124
#caches?: CacheStorage;
125125

126-
constructor(log: Log, compat: Compatibility, options?: CacheOptions) {
127-
super(log, compat);
126+
constructor(ctx: PluginContext, options?: CacheOptions) {
127+
super(ctx);
128128
this.assignOptions(options);
129129
}
130130

131131
setup(storageFactory: StorageFactory): SetupResult {
132132
this.#caches = new CacheStorage(
133133
this,
134-
this.log,
134+
this.ctx.log,
135135
storageFactory,
136-
this.compat.isEnabled("formdata_parser_supports_files")
136+
this.ctx.compat.isEnabled("formdata_parser_supports_files")
137137
);
138138
return { globals: { caches: this.#caches } };
139139
}

packages/cache/test/plugin.spec.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
Compatibility,
1111
LogLevel,
1212
NoOpLog,
13+
PluginContext,
1314
StoredValueMeta,
1415
} from "@miniflare/shared";
1516
import {
@@ -27,6 +28,8 @@ import { testResponse } from "./helpers";
2728

2829
const log = new NoOpLog();
2930
const compat = new Compatibility();
31+
const rootPath = process.cwd();
32+
const ctx: PluginContext = { log, compat, rootPath };
3033

3134
test("CacheStorage: provides default cache", async (t) => {
3235
const factory = new MemoryStorageFactory();
@@ -137,11 +140,10 @@ test("CachePlugin: logs options", (t) => {
137140
});
138141

139142
test("CachePlugin: setup: includes CacheStorage in globals", async (t) => {
140-
const log = new NoOpLog();
141143
const map = new Map<string, StoredValueMeta<CachedMeta>>();
142144
const factory = new MemoryStorageFactory({ ["map:default"]: map });
143145

144-
let plugin = new CachePlugin(log, compat, { cachePersist: "map" });
146+
let plugin = new CachePlugin(ctx, { cachePersist: "map" });
145147
let result = plugin.setup(factory);
146148
let caches = result.globals?.caches;
147149
t.true(caches instanceof CacheStorage);
@@ -150,7 +152,7 @@ test("CachePlugin: setup: includes CacheStorage in globals", async (t) => {
150152
await caches.default.put("http://localhost:8787/", testResponse());
151153
t.true(map.has("http://localhost:8787/"));
152154

153-
plugin = new CachePlugin(log, compat, { cache: false });
155+
plugin = new CachePlugin(ctx, { cache: false });
154156
result = plugin.setup(factory);
155157
caches = result.globals?.caches;
156158
t.true(caches instanceof CacheStorage);
@@ -163,7 +165,7 @@ test("CachePlugin: setup: Responses parse files in FormData as File objects only
163165
const formData = new FormData();
164166
formData.append("file", new File(["test"], "test.txt"));
165167

166-
let plugin = new CachePlugin(log, new Compatibility());
168+
let plugin = new CachePlugin(ctx);
167169
let caches: CacheStorage = plugin.setup(factory).globals?.caches;
168170
await caches.default.put("http://localhost", testResponse(formData));
169171
let cache = await caches.open("test");
@@ -177,7 +179,7 @@ test("CachePlugin: setup: Responses parse files in FormData as File objects only
177179
const compat = new Compatibility(undefined, [
178180
"formdata_parser_supports_files",
179181
]);
180-
plugin = new CachePlugin(log, compat);
182+
plugin = new CachePlugin({ log, compat, rootPath });
181183
caches = plugin.setup(factory).globals?.caches;
182184
cache = await caches.open("test");
183185

packages/cli-parser/test/help.spec.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ test("buildHelp: generates correctly formatted help text", (t) => {
3232
Core Options:
3333
-h, --help Show help [boolean]
3434
-v, --version Show version number [boolean]
35+
--root Path to resolve default config files [string]
36+
relative to
3537
-c, --wrangler-config Path to wrangler.toml [string]
3638
--wrangler-env Environment in wrangler.toml to use [string]
3739
--package Path to package.json [string]

packages/core/src/index.ts

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
Log,
99
Mutex,
1010
Options,
11+
PluginContext,
1112
PluginEntries,
1213
PluginOptions,
1314
PluginOptionsUnion,
@@ -182,7 +183,6 @@ export interface MiniflareCoreContext {
182183
scriptRunner?: ScriptRunner;
183184
scriptRequired?: boolean;
184185
scriptRunForModuleExports?: boolean;
185-
defaultConfigPath?: string;
186186
}
187187

188188
export class ReloadEvent<Plugins extends PluginSignatures> extends Event {
@@ -209,9 +209,9 @@ export class MiniflareCore<
209209
readonly #scriptRunner?: ScriptRunner;
210210
readonly #scriptRequired?: boolean;
211211
readonly #scriptRunForModuleExports?: boolean;
212-
readonly #defaultConfigPath: string;
213212

214213
#compat?: Compatibility;
214+
#previousRootPath?: string;
215215
#instances?: PluginInstances<Plugins>;
216216

217217
#wranglerConfigPath?: string;
@@ -243,7 +243,6 @@ export class MiniflareCore<
243243
this.#scriptRunner = ctx.scriptRunner;
244244
this.#scriptRequired = ctx.scriptRequired;
245245
this.#scriptRunForModuleExports = ctx.scriptRunForModuleExports;
246-
this.#defaultConfigPath = ctx.defaultConfigPath ?? "wrangler.toml";
247246

248247
this.#initPromise = this.#init().then(() => this.#reload());
249248
}
@@ -288,12 +287,14 @@ export class MiniflareCore<
288287
const previous = this.#previousOptions;
289288
let options = this.#overrides;
290289

290+
const rootPath = options.CorePlugin.rootPath ?? process.cwd();
291+
291292
// Merge in wrangler config if defined
292293
const originalConfigPath = options.CorePlugin.wranglerConfigPath;
293294
const configEnv = options.CorePlugin.wranglerConfigEnv;
294295
let configPath =
295296
originalConfigPath === true
296-
? this.#defaultConfigPath
297+
? path.join(rootPath, "wrangler.toml")
297298
: originalConfigPath;
298299
if (configPath) {
299300
configPath = path.resolve(configPath);
@@ -330,14 +331,23 @@ export class MiniflareCore<
330331
this.#watching ??= options.CorePlugin.watch ?? false;
331332

332333
// Build compatibility manager, rebuild all plugins if compatibility data
333-
// has changed
334+
// or root path has changed
334335
const { compatibilityDate, compatibilityFlags } = options.CorePlugin;
335-
let compatUpdate = false;
336+
let ctxUpdate =
337+
this.#previousRootPath && this.#previousRootPath !== rootPath;
338+
this.#previousRootPath = rootPath;
336339
if (this.#compat) {
337-
compatUpdate = this.#compat.update(compatibilityDate, compatibilityFlags);
340+
if (this.#compat.update(compatibilityDate, compatibilityFlags)) {
341+
ctxUpdate = true;
342+
}
338343
} else {
339344
this.#compat = new Compatibility(compatibilityDate, compatibilityFlags);
340345
}
346+
const ctx: PluginContext = {
347+
log: this.log,
348+
compat: this.#compat,
349+
rootPath,
350+
};
341351

342352
// Create plugin instances and run beforeSetup hooks, recreating any plugins
343353
// with changed options
@@ -347,7 +357,7 @@ export class MiniflareCore<
347357
for (const [name, plugin] of this.#plugins) {
348358
if (
349359
previous !== undefined &&
350-
!compatUpdate &&
360+
!ctxUpdate &&
351361
_deepEqual(previous[name], options[name])
352362
) {
353363
continue;
@@ -360,7 +370,7 @@ export class MiniflareCore<
360370
await existingInstance.dispose();
361371
}
362372

363-
const instance = new plugin(this.log, this.#compat, options[name]);
373+
const instance = new plugin(ctx, options[name]);
364374
this.#instances[name] = instance as any;
365375
if (await this.#runBeforeSetup(name)) ranBeforeSetup = true;
366376
}
@@ -371,7 +381,7 @@ export class MiniflareCore<
371381
for (const [name] of this.#plugins) {
372382
if (
373383
previous !== undefined &&
374-
!compatUpdate &&
384+
!ctxUpdate &&
375385
_deepEqual(previous[name], options[name]) &&
376386
// Make sure if we ran any beforeSetups and this plugin previously
377387
// returned scripts, that we rerun its setup

packages/core/src/plugins/bindings.ts

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
import fs from "fs/promises";
22
import path from "path";
33
import {
4-
Compatibility,
54
Context,
6-
Log,
75
Option,
86
OptionType,
97
Plugin,
8+
PluginContext,
109
SetupResult,
1110
} from "@miniflare/shared";
1211
import dotenv from "dotenv";
@@ -80,13 +79,8 @@ export class BindingsPlugin
8079
})
8180
wasmBindings?: Record<string, string>;
8281

83-
constructor(
84-
log: Log,
85-
compat: Compatibility,
86-
options?: BindingsOptions,
87-
private readonly defaultEnvPath: string = ".env"
88-
) {
89-
super(log, compat);
82+
constructor(ctx: PluginContext, options?: BindingsOptions) {
83+
super(ctx);
9084
this.assignOptions(options);
9185
}
9286

@@ -104,7 +98,10 @@ export class BindingsPlugin
10498
Object.assign(bindings, this[kWranglerBindings]);
10599

106100
// Load bindings from .env file
107-
const envPath = this.envPath === true ? this.defaultEnvPath : this.envPath;
101+
const envPath =
102+
this.envPath === true
103+
? path.join(this.ctx.rootPath, ".env")
104+
: this.envPath;
108105
if (envPath) {
109106
try {
110107
Object.assign(

packages/core/src/plugins/build.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,11 @@ import { fileURLToPath } from "url";
55
import {
66
Awaitable,
77
BeforeSetupResult,
8-
Compatibility,
9-
Log,
108
MiniflareError,
119
Option,
1210
OptionType,
1311
Plugin,
12+
PluginContext,
1413
WranglerConfig,
1514
} from "@miniflare/shared";
1615

@@ -48,8 +47,8 @@ export class BuildPlugin extends Plugin<BuildOptions> implements BuildOptions {
4847
})
4948
buildWatchPaths?: string[];
5049

51-
constructor(log: Log, compat: Compatibility, options?: BuildOptions) {
52-
super(log, compat);
50+
constructor(ctx: PluginContext, options?: BuildOptions) {
51+
super(ctx);
5352
this.assignOptions(options);
5453
}
5554

@@ -75,7 +74,7 @@ export class BuildPlugin extends Plugin<BuildOptions> implements BuildOptions {
7574
return reject(error);
7675
}
7776

78-
this.log.info("Build succeeded");
77+
this.ctx.log.info("Build succeeded");
7978
resolve({ watch: this.buildWatchPaths });
8079
});
8180
});

packages/core/src/plugins/core.ts

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,14 @@ import {
1919
import { URL, URLSearchParams } from "url";
2020
import { TextEncoder } from "util";
2121
import {
22-
Compatibility,
2322
CompatibilityFlag,
2423
Context,
25-
Log,
2624
ModuleRule,
2725
ModuleRuleType,
2826
Option,
2927
OptionType,
3028
Plugin,
29+
PluginContext,
3130
ProcessedModuleRule,
3231
STRING_SCRIPT_PATH,
3332
SetupResult,
@@ -68,6 +67,7 @@ function proxyStringFormDataFiles<
6867
export interface CoreOptions {
6968
script?: string;
7069
scriptPath?: string;
70+
rootPath?: string;
7171
packagePath?: boolean | string;
7272
wranglerConfigPath?: boolean | string;
7373
wranglerConfigEnv?: string;
@@ -102,6 +102,13 @@ export class CorePlugin extends Plugin<CoreOptions> implements CoreOptions {
102102
})
103103
scriptPath?: string;
104104

105+
@Option({
106+
type: OptionType.STRING,
107+
name: "root",
108+
description: "Path to resolve default config files relative to",
109+
})
110+
rootPath?: string;
111+
105112
@Option({
106113
type: OptionType.STRING,
107114
name: "wrangler-config",
@@ -225,19 +232,16 @@ export class CorePlugin extends Plugin<CoreOptions> implements CoreOptions {
225232

226233
readonly #globals: Context;
227234

228-
constructor(
229-
log: Log,
230-
compat: Compatibility,
231-
options?: CoreOptions,
232-
private readonly defaultPackagePath = "package.json"
233-
) {
234-
super(log, compat);
235+
constructor(ctx: PluginContext, options?: CoreOptions) {
236+
super(ctx);
235237
this.assignOptions(options);
236238

237239
// Make sure the kFormDataFiles flag is set correctly when constructing
238240
let CompatRequest = Request;
239241
let CompatResponse = Response;
240-
const formDataFiles = compat.isEnabled("formdata_parser_supports_files");
242+
const formDataFiles = ctx.compat.isEnabled(
243+
"formdata_parser_supports_files"
244+
);
241245
if (!formDataFiles) {
242246
CompatRequest = proxyStringFormDataFiles(CompatRequest);
243247
CompatResponse = proxyStringFormDataFiles(CompatResponse);
@@ -261,7 +265,7 @@ export class CorePlugin extends Plugin<CoreOptions> implements CoreOptions {
261265
TextDecoder,
262266
TextEncoder,
263267

264-
fetch: createCompatFetch(compat),
268+
fetch: createCompatFetch(ctx.compat),
265269
Headers,
266270
Request: CompatRequest,
267271
Response: CompatResponse,
@@ -363,7 +367,9 @@ export class CorePlugin extends Plugin<CoreOptions> implements CoreOptions {
363367
// from package.json
364368
if (scriptPath === undefined) {
365369
const packagePath =
366-
this.packagePath === true ? this.defaultPackagePath : this.packagePath;
370+
this.packagePath === true
371+
? path.join(this.ctx.rootPath, "package.json")
372+
: this.packagePath;
367373
if (packagePath) {
368374
try {
369375
const pkg = JSON.parse(await fs.readFile(packagePath, "utf8"));

0 commit comments

Comments
 (0)