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

feat(workerd): use vite/module-runner from peer dep #143

Merged
merged 4 commits into from
Oct 27, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions packages/workerd/src/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { fileURLToPath } from "url";
import { readFileSync } from "node:fs";
import { fileURLToPath } from "node:url";
import { DefaultMap, tinyassert } from "@hiogawa/utils";
import { webToNodeHandler } from "@hiogawa/utils-node";
import {
Expand Down Expand Up @@ -84,13 +85,29 @@ export async function createWorkerdDevEnvironment(
config: ResolvedConfig,
pluginOptions: WorkerdEnvironmentOptions,
) {
const workerPath = fileURLToPath(new URL("./worker.js", import.meta.url));
const workerContent = readFileSync(workerPath, "utf-8");
const viteModuleRunnerPath = fileURLToPath(
import.meta.resolve("vite/module-runner"),
);
const viteModuleRunnerContent = readFileSync(
viteModuleRunnerPath,
"utf-8",
// avoid new AsyncFunction during import side effect
).replace(`new AsyncFunction("a", "b", body).toString()`, `""`);
Comment on lines +96 to +97
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Otherwise we get:

service core:user:: Uncaught Error: EvalError: Code generation from strings disallowed for this context
  at vite/module-runner:21:37
  at vite/module-runner:24:3
error when starting dev server:
MiniflareCoreError [ERR_RUNTIME_FAILURE]: The Workers runtime failed to start. There is likely additional logging output above.
    at #assembleAndUpdateConfig (/home/hiroshi/code/personal/vite-environment-examples/node_modules/.pnpm/miniflare@3.20240925.0/node_modules/miniflare/dist/src/index.js:9828:13)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async Mutex.runWith (/home/hiroshi/code/personal/vite-environment-examples/node_modules/.pnpm/miniflare@3.20240925.0/node_modules/miniflare/dist/src/index.js:3627:16)
    at async #waitForReady (/home/hiroshi/code/personal/vite-environment-examples/node_modules/.pnpm/miniflare@3.20240925.0/node_modules/miniflare/dist/src/index.js:9885:5)
    at async Miniflare2._getProxyClient (/home/hiroshi/code/personal/vite-environment-examples/node_modules/.pnpm/miniflare@3.20240925.0/node_modules/miniflare/dist/src/index.js:10009:5)
    at async #getProxy (/home/hiroshi/code/personal/vite-environment-examples/node_modules/.pnpm/miniflare@3.20240925.0/node_modules/miniflare/dist/src/index.js:10069:25)
    at async createWorkerdDevEnvironment (file:///home/hiroshi/code/personal/vite-environment-examples/packages/workerd/dist/index.js:190:14)
    at async _createServer (file:///home/hiroshi/code/personal/vite-environment-examples/node_modules/.pnpm/vite@https+++pkg.pr.new+vite@70bb8de_@types+node@20.14.11_terser@5.31.3/node_modules/vite/dist/node/chunks/dep-DsfahqHZ.js:51979:26)
    at async CAC.<anonymous> (file:///home/hiroshi/code/personal/vite-environment-examples/node_modules/.pnpm/vite@https+++pkg.pr.new+vite@70bb8de_@types+node@20.14.11_terser@5.31.3/node_modules/vite/dist/node/cli.js:745:20)


// setup miniflare with a durable object script to run vite module runner
let runnerWorkerOptions: WorkerOptions = {
modulesRoot: "/",
modules: [
{
type: "ESModule",
path: fileURLToPath(new URL("./worker.js", import.meta.url)),
path: "__vite_worker__",
contents: workerContent,
},
{
type: "ESModule",
path: "vite/module-runner",
contents: viteModuleRunnerContent,
},
],
durableObjects: {
Expand Down
2 changes: 1 addition & 1 deletion packages/workerd/tsup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export default [
entry: ["src/worker.ts"],
format: ["esm"],
platform: "browser",
noExternal: [/.*/],
external: ["cloudflare:workers"],
}),
defineConfig({
entry: ["src/index.ts"],
Expand Down