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

Commit 32fdf00

Browse files
committed
Respect workerd: and additionals when auto-collecting modules
Previously, attempting to use Workers Sites with `modules: true` would fail as the automatic module locator didn't know about `__STATIC_CONTENT_MANIFEST`. This change passes through additional modules to the locator. This change also adds support for internal `"workerd:"` modules to the locator (e.g. `workerd:rtti`).
1 parent d656e7f commit 32fdf00

File tree

3 files changed

+22
-6
lines changed

3 files changed

+22
-6
lines changed

packages/miniflare/src/plugins/core/index.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -374,10 +374,15 @@ export const CORE_PLUGIN: Plugin<
374374
sourceMapRegistry,
375375
}) {
376376
// Define regular user worker
377+
const additionalModuleNames = additionalModules.map(({ name }) => {
378+
assert(name !== undefined);
379+
return name;
380+
});
377381
const workerScript = getWorkerScript(
378382
sourceMapRegistry,
379383
options,
380-
workerIndex
384+
workerIndex,
385+
additionalModuleNames
381386
);
382387
// Add additional modules (e.g. "__STATIC_CONTENT_MANIFEST") if any
383388
if ("modules" in workerScript) {
@@ -561,7 +566,8 @@ export function getGlobalServices({
561566
function getWorkerScript(
562567
sourceMapRegistry: SourceMapRegistry,
563568
options: SourceOptions,
564-
workerIndex: number
569+
workerIndex: number,
570+
additionalModuleNames: string[]
565571
): { serviceWorkerScript: string } | { modules: Worker_Module[] } {
566572
const modulesRoot =
567573
("modulesRoot" in options ? options.modulesRoot : undefined) ?? "";
@@ -592,6 +598,7 @@ function getWorkerScript(
592598
const locator = new ModuleLocator(
593599
sourceMapRegistry,
594600
modulesRoot,
601+
additionalModuleNames,
595602
options.modulesRules
596603
);
597604
// If `script` and `scriptPath` are set, resolve modules in `script`

packages/miniflare/src/plugins/core/modules.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ export class ModuleLocator {
136136
constructor(
137137
private readonly sourceMapRegistry: SourceMapRegistry,
138138
private readonly modulesRoot: string,
139+
private readonly additionalModuleNames: string[],
139140
rules?: ModuleRule[]
140141
) {
141142
this.#compiledRules = compileModuleRules(rules);
@@ -256,8 +257,14 @@ ${dim(modulesConfig)}`;
256257
}
257258
const spec = specExpression.value;
258259

259-
// `node:` and `cloudflare:` imports don't need to be included explicitly
260-
if (spec.startsWith("node:") || spec.startsWith("cloudflare:")) {
260+
// `node:`, `cloudflare:` and `workerd:` imports don't need to be included
261+
// explicitly
262+
if (
263+
spec.startsWith("node:") ||
264+
spec.startsWith("cloudflare:") ||
265+
spec.startsWith("workerd:") ||
266+
this.additionalModuleNames.includes(spec)
267+
) {
261268
return;
262269
}
263270

packages/miniflare/test/index.spec.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -445,17 +445,19 @@ test("Miniflare: custom upstream as origin", async (t) => {
445445
t.is(await res.text(), "upstream: http://upstream/extra/path?a=1");
446446
});
447447

448-
test("Miniflare: `node:` and `cloudflare:` modules", async (t) => {
448+
test("Miniflare: `node:`, `cloudflare:` and `workerd:` modules", async (t) => {
449449
const mf = new Miniflare({
450450
modules: true,
451-
compatibilityFlags: ["nodejs_compat"],
451+
compatibilityFlags: ["nodejs_compat", "rtti_api"],
452452
script: `
453453
import assert from "node:assert";
454454
import { Buffer } from "node:buffer";
455455
import { connect } from "cloudflare:sockets";
456+
import rtti from "workerd:rtti";
456457
export default {
457458
fetch() {
458459
assert.strictEqual(typeof connect, "function");
460+
assert.strictEqual(typeof rtti, "object");
459461
return new Response(Buffer.from("test").toString("base64"))
460462
}
461463
}

0 commit comments

Comments
 (0)