Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow --experimental-local to be used with module workers #1934

Merged
merged 1 commit into from
Sep 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/curvy-laws-develop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"wrangler": patch
---

Allow `--experimental-local` to be used with module workers
34 changes: 32 additions & 2 deletions packages/wrangler/src/dev/local.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
import assert from "node:assert";
import { fork } from "node:child_process";
import { realpathSync } from "node:fs";
import { writeFile } from "node:fs/promises";
import { readFile, writeFile } from "node:fs/promises";
import path from "node:path";
import { npxImport } from "npx-import";
import { useState, useEffect, useRef } from "react";
import onExit from "signal-exit";
import { registerWorker } from "../dev-registry";
import useInspector from "../inspect";
import { logger } from "../logger";
import { DEFAULT_MODULE_RULES } from "../module-collection";
import {
DEFAULT_MODULE_RULES,
ModuleTypeToRuleType,
} from "../module-collection";
import { getBasePath } from "../paths";
import { waitForPortToBeAvailable } from "../proxy";
import type { Config } from "../config";
Expand Down Expand Up @@ -218,6 +222,32 @@ function useLocalWorker({
// to collect them again
};

if (format === "modules") {
// Manually specify all modules from the bundle. If we didn't do this,
// Miniflare 3 would try collect them automatically again itself.

// Resolve entrypoint relative to the temporary directory, ensuring
// path doesn't start with `..`, which causes issues in `workerd`.
// Also ensures other modules with relative names can be resolved.
const root = path.dirname(bundle.path);

assert.strictEqual(bundle.type, "esm");
options.modules = [
// Entrypoint
{
type: "ESModule",
path: path.relative(root, bundle.path),
contents: await readFile(bundle.path, "utf-8"),
},
// Misc (WebAssembly, etc, ...)
...bundle.modules.map((module) => ({
type: ModuleTypeToRuleType[module.type ?? "esm"],
path: module.name,
contents: module.content,
})),
];
}

logger.log("⎔ Starting an experimental local server...");

if (Miniflare === undefined) {
Expand Down
9 changes: 9 additions & 0 deletions packages/wrangler/src/module-collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ import type { Config, ConfigModuleRuleType } from "./config";
import type { CfModule, CfModuleType, CfScriptFormat } from "./worker";
import type esbuild from "esbuild";

function flipObject<
K extends string | number | symbol,
V extends string | number | symbol
>(obj: Record<K, V>): Record<V, K> {
return Object.fromEntries(Object.entries(obj).map(([k, v]) => [v, k]));
}

const RuleTypeToModuleType: Record<ConfigModuleRuleType, CfModuleType> = {
ESModule: "esm",
CommonJS: "commonjs",
Expand All @@ -15,6 +22,8 @@ const RuleTypeToModuleType: Record<ConfigModuleRuleType, CfModuleType> = {
Text: "text",
};

export const ModuleTypeToRuleType = flipObject(RuleTypeToModuleType);

// This is a combination of an esbuild plugin and a mutable array
// that we use to collect module references from source code.
// There will be modules that _shouldn't_ be inlined directly into
Expand Down